vue-eslint-parser
Advanced tools
Comparing version 0.1.4 to 0.2.0
162
index.js
@@ -12,120 +12,5 @@ /** | ||
const path = require("path") | ||
const SAXParser = require("parse5").SAXParser | ||
const Parser = require("./lib/parser") | ||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
const LINE_TERMINATORS = /\r\n|\r|\n|\u2028|\u2029/g | ||
/** | ||
* Gets the specified parser. | ||
* If it's unspecified, this returns espree. | ||
* | ||
* @param {object} options - The option object. | ||
* @param {string} [options.parser] - The parser name to get. | ||
* @returns {object} The gotten parser. | ||
*/ | ||
function getParser(options) { | ||
return require(options.parser || "espree") | ||
} | ||
/** | ||
* Calculates the end location. | ||
* | ||
* @param {string} raw - The text of the target token. | ||
* @param {number} startLine - The start line of the target token. | ||
* @param {number} startColumn - The start column of the target token. | ||
* @returns {{line: number, column: number}} The end location. | ||
* @private | ||
*/ | ||
function calcLocEnd(raw, startLine, startColumn) { | ||
const lines = raw.split(LINE_TERMINATORS) | ||
const line = startLine + lines.length - 1 | ||
const column = (lines.length === 1) | ||
? startColumn + raw.length | ||
: lines[lines.length - 1].length | ||
return {line, column} | ||
} | ||
/** | ||
* Creates the token with the given parameters. | ||
* | ||
* @param {string} value - The token value to create. | ||
* @param {string} text - The whole text. | ||
* @param {object} location - The location object of `parse5` module. | ||
* @returns {object} The created token object. | ||
* @private | ||
*/ | ||
function createToken(value, text, location) { | ||
const type = "Punctuator" | ||
const start = location.startOffset | ||
const end = location.endOffset | ||
const line = location.line | ||
const column = location.col - 1 | ||
const range = [start, end] | ||
const raw = text.slice(start, end) | ||
const loc = { | ||
start: {line, column}, | ||
end: calcLocEnd(raw, line, column), | ||
} | ||
return {type, value, raw, start, end, range, loc} | ||
} | ||
/** | ||
* Extracts the text of the 1st script element in the given text. | ||
* | ||
* @param {string} originalText - The whole text to extract. | ||
* @returns {{text: string, offset: number}} The information of the 1st script. | ||
* @private | ||
*/ | ||
function extractFirstScript(originalText) { | ||
const parser = new SAXParser({locationInfo: true}) | ||
let inTemplate = 0 | ||
let startToken = null | ||
let endToken = null | ||
let text = "" | ||
let offset = 0 | ||
parser.on("startTag", (name, attrs, selfClosing, location) => { | ||
if (selfClosing) { | ||
return | ||
} | ||
if (name === "template") { | ||
inTemplate += 1 | ||
} | ||
if (inTemplate === 0 && name === "script") { | ||
startToken = createToken("<script>", originalText, location) | ||
} | ||
}) | ||
parser.on("endTag", (name, location) => { | ||
if (inTemplate > 0 && name === "template") { | ||
inTemplate -= 1 | ||
} | ||
if (startToken != null && name === "script") { | ||
endToken = createToken("</script>", originalText, location) | ||
parser.stop() | ||
} | ||
}) | ||
parser.on("text", (_, location) => { | ||
if (startToken != null) { | ||
const start = location.startOffset | ||
const countLines = location.line - 1 | ||
const lineTerminators = "\n".repeat(countLines) | ||
const spaces = " ".repeat(start - countLines) | ||
const scriptText = originalText.slice(start, location.endOffset) | ||
text = `${spaces}${lineTerminators}${scriptText}` | ||
offset = start | ||
} | ||
}) | ||
parser.end(originalText) | ||
return {startToken, endToken, text, offset} | ||
} | ||
//------------------------------------------------------------------------------ | ||
// Exports | ||
@@ -135,32 +20,21 @@ //------------------------------------------------------------------------------ | ||
/** | ||
* Parses the source code. | ||
* Provides the `parse` method for `.vue` files. | ||
* | ||
* If `options.filePath` is a `.vue` file, this extracts the first `<script>` | ||
* element then parses it. | ||
* | ||
* @memberof module:vue-eslint-parser | ||
* @function parse | ||
* @param {string} text - The source code to be parsed. | ||
* @param {object} options - The option object for espree. | ||
* @returns {ASTNode} The AST object as the result of parsing. | ||
* @module vue-eslint-parser | ||
*/ | ||
module.exports.parse = function parse(text, options) { | ||
const parser = getParser(options) | ||
if (path.extname(options.filePath || "unknown.js") !== ".vue") { | ||
return parser.parse(text, options) | ||
} | ||
const script = extractFirstScript(text) | ||
const ast = parser.parse(script.text, options) | ||
ast.start = script.offset | ||
if (script.startToken) { | ||
ast.tokens.unshift(script.startToken) | ||
} | ||
if (script.endToken) { | ||
ast.tokens.push(script.endToken) | ||
} | ||
return ast | ||
module.exports = { | ||
/** | ||
* Parses the source code. | ||
* | ||
* If `options.filePath` is a `.vue` file, this extracts the first `<script>` | ||
* element then parses it. | ||
* | ||
* @param {string} code - The source code to be parsed. | ||
* @param {object} options - The option object. | ||
* @returns {{ast: ASTNode, services: any}} The result of parsing. | ||
*/ | ||
parse(code, options) { | ||
const parser = new Parser(options) | ||
return parser.parseComponent(code) | ||
}, | ||
} |
{ | ||
"name": "vue-eslint-parser", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "The ESLint custom parser for `.vue` files.", | ||
@@ -9,7 +9,10 @@ "engines": { | ||
"main": "index.js", | ||
"files": [], | ||
"files": [ | ||
"lib" | ||
], | ||
"scripts": { | ||
"_mocha": "_mocha \"test/*.js\" --compilers js:babel-register --reporter progress --timeout 30000", | ||
"clean": "rimraf .nyc_output coverage", | ||
"coverage": "nyc report --reporter lcov && opener ./coverage/lcov-report/index.html", | ||
"lint": "eslint index.js \"test/*.js\"", | ||
"lint": "eslint index.js lib \"test/*.js\"", | ||
"postversion": "git push && git push --tags", | ||
@@ -19,4 +22,4 @@ "pretest": "npm run lint", | ||
"setup": "git submodule update --init && cd test/fixtures/eslint && npm install", | ||
"test": "nyc mocha \"test/*.js\" --compilers js:babel-register --reporter progress --timeout 30000", | ||
"watch": "mocha \"test/*.js\" --compilers js:babel-register --growl --reporter progress --watch", | ||
"test": "nyc npm run _mocha", | ||
"watch": "npm run _mocha -- --growl --watch", | ||
"codecov": "nyc report --reporter lcovonly && codecov" | ||
@@ -26,3 +29,4 @@ }, | ||
"espree": ">=3.3.2", | ||
"parse5": "^3.0.0" | ||
"parse5": "^3.0.0", | ||
"vue-template-compiler": "^2.1.6" | ||
}, | ||
@@ -29,0 +33,0 @@ "devDependencies": { |
@@ -19,3 +19,3 @@ # vue-eslint-parser | ||
- `vue-eslint-parser` requires ESLint 3.5.0 or later. | ||
- `vue-eslint-parser` requires ESLint 3.9.0 or later. | ||
@@ -35,3 +35,3 @@ ## :book: Usage | ||
```bash | ||
$ eslint "src/**.{js,vue}" | ||
$ eslint "src/**/*.{js,vue}" | ||
# or | ||
@@ -43,3 +43,3 @@ $ eslint src --ext .vue | ||
`parserOptions` is the same as what [espree](https://github.com/eslint/espree#usage), the default parser of ESLint, is supporting. | ||
`parserOptions` has the same properties as what [espree](https://github.com/eslint/espree#usage), the default parser of ESLint, is supporting. | ||
For example: | ||
@@ -52,4 +52,9 @@ | ||
"sourceType": "module", | ||
"ecmaVersion": 2017 | ||
// ... | ||
"ecmaVersion": 2017, | ||
"ecmaFeatures": { | ||
"globalReturn": false, | ||
"impliedStrict": false, | ||
"jsx": false, | ||
"experimentalObjectRestSpread": false | ||
} | ||
} | ||
@@ -59,4 +64,4 @@ } | ||
On the other hand, you can specify a custom parser to parse `<script>` tags. | ||
In this case, specify `parser` property. Other properties than `parser` would be given to the specified parser. | ||
Also, you can use `parser` property to specify a custom parser to parse `<script>` tags. | ||
Other properties than parser would be given to the specified parser. | ||
For example: | ||
@@ -75,12 +80,22 @@ | ||
```json | ||
{ | ||
"parser": "vue-eslint-parser", | ||
"parserOptions": { | ||
"parser": "typescript-eslint-parser" | ||
} | ||
} | ||
``` | ||
## :warning: Known Limitations | ||
- Those rules are warning code due to the outside of `<script>` tags. | ||
Please disable those rules for `.vue` files as necessary. | ||
- [eol-last](http://eslint.org/docs/rules/eol-last) | ||
- [linebreak-style](http://eslint.org/docs/rules/linebreak-style) | ||
- [max-len](http://eslint.org/docs/rules/max-len) | ||
- [max-lines](http://eslint.org/docs/rules/max-lines) | ||
- [no-trailing-spaces](http://eslint.org/docs/rules/no-trailing-spaces) | ||
- [unicode-bom](http://eslint.org/docs/rules/unicode-bom) | ||
Some rules make warnings due to the outside of `<script>` tags. | ||
Please disable those rules for `.vue` files as necessary. | ||
- [eol-last](http://eslint.org/docs/rules/eol-last) | ||
- [linebreak-style](http://eslint.org/docs/rules/linebreak-style) | ||
- [max-len](http://eslint.org/docs/rules/max-len) | ||
- [max-lines](http://eslint.org/docs/rules/max-lines) | ||
- [no-trailing-spaces](http://eslint.org/docs/rules/no-trailing-spaces) | ||
- [unicode-bom](http://eslint.org/docs/rules/unicode-bom) | ||
- Other rules which are using the source code text instead of AST might be confused as well. | ||
@@ -87,0 +102,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
14567
5
235
114
4
1
+ Addedvue-template-compiler@^2.1.6
+ Addedde-indent@1.0.2(transitive)
+ Addedhe@1.2.0(transitive)
+ Addedvue-template-compiler@2.7.16(transitive)