Skip to content

Commit

Permalink
Update: 'parser:false' skip parsing '<script>' (refs vuejs/eslint-plu…
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Jul 21, 2018
1 parent f10d801 commit 9b947b1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,19 @@ For example:
"parser": "vue-eslint-parser",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2017,
"ecmaVersion": 2018,
"ecmaFeatures": {
"globalReturn": false,
"impliedStrict": false,
"jsx": false,
"experimentalObjectRestSpread": false
"jsx": false
}
}
}
```

Also, you can use `parser` property to specify a custom parser to parse `<script>` tags.
### parserOptions.parser

You can use `parserOptions.parser` property to specify a custom parser to parse `<script>` tags.
Other properties than parser would be given to the specified parser.
For example:

Expand All @@ -87,6 +88,9 @@ For example:
- If you use with `babel-eslint`, use `babel-eslint@>=8.1.1`.
- If you use `typescript-eslint-parser`, the location of original nodes can be wrong. Waiting for `typescript-eslint-parser` to support [parseResult.visitorKeys](https://eslint.org/docs/developer-guide/working-with-plugins#working-with-custom-parsers).

If the `parserOptions.parser` is `false`, the `vue-eslint-parser` skips parsing `<script>` tags completely.
This is useful for people who use the language ESLint community doesn't provide custom parser implementation.

## 🎇 Usage for custom rules / plugins

- This parser provides `parserServices` to traverse `<template>`.
Expand Down
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export function parseForESLint(
if (!isVueFile(code, options)) {
result = parseScript(code, options)
} else {
const skipParsingScript = options.parser === false
const tokenizer = new HTMLTokenizer(code)
const rootAST = new HTMLParser(tokenizer, options).parse()
const locationCalcurator = new LocationCalculator(
Expand All @@ -112,10 +113,11 @@ export function parseForESLint(
? Object.assign(template, concreteInfo)
: undefined

result =
script != null
? parseScriptElement(script, locationCalcurator, options)
: parseScript("", options)
if (skipParsingScript || script == null) {
result = parseScript("", options)
} else {
result = parseScriptElement(script, locationCalcurator, options)
}

result.ast.templateBody = templateBody
}
Expand Down
40 changes: 40 additions & 0 deletions test/parser-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @author Toru Nagashima <https://github.com/mysticatea>
* See LICENSE file in root directory for full license.
*/
"use strict"

const assert = require("assert")
const { rules } = require("@mysticatea/eslint-plugin")
const parseForESLint = require("..").parseForESLint
const eslint = require("./fixtures/eslint")
const Linter = eslint.Linter

describe.only("parserOptions", () => {
describe("parser", () => {
const linter = new Linter()
linter.defineParser("vue-eslint-parser", { parseForESLint })
linter.defineRule(
"vue/valid-template-root",
rules["vue/valid-template-root"]
)

it("false then skip parsing '<script>'.", () => {
const code = `<template>Hello</template>
<script>This is syntax error</script>`
const config = {
parser: "vue-eslint-parser",
parserOptions: {
parser: false,
},
rules: {
"vue/valid-template-root": "error",
},
}
const messages = linter.verify(code, config, "test.vue")

assert.strictEqual(messages.length, 1)
assert.strictEqual(messages[0].ruleId, "vue/valid-template-root")
})
})
})

0 comments on commit 9b947b1

Please sign in to comment.