Skip to content

Commit 9b947b1

Browse files
committed
Update: 'parser:false' skip parsing '<script>' (refs vuejs/eslint-plugin-vue#446)
1 parent f10d801 commit 9b947b1

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,19 @@ For example:
4949
"parser": "vue-eslint-parser",
5050
"parserOptions": {
5151
"sourceType": "module",
52-
"ecmaVersion": 2017,
52+
"ecmaVersion": 2018,
5353
"ecmaFeatures": {
5454
"globalReturn": false,
5555
"impliedStrict": false,
56-
"jsx": false,
57-
"experimentalObjectRestSpread": false
56+
"jsx": false
5857
}
5958
}
6059
}
6160
```
6261

63-
Also, you can use `parser` property to specify a custom parser to parse `<script>` tags.
62+
### parserOptions.parser
63+
64+
You can use `parserOptions.parser` property to specify a custom parser to parse `<script>` tags.
6465
Other properties than parser would be given to the specified parser.
6566
For example:
6667

@@ -87,6 +88,9 @@ For example:
8788
- If you use with `babel-eslint`, use `babel-eslint@>=8.1.1`.
8889
- 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).
8990

91+
If the `parserOptions.parser` is `false`, the `vue-eslint-parser` skips parsing `<script>` tags completely.
92+
This is useful for people who use the language ESLint community doesn't provide custom parser implementation.
93+
9094
## 🎇 Usage for custom rules / plugins
9195

9296
- This parser provides `parserServices` to traverse `<template>`.

src/index.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export function parseForESLint(
9393
if (!isVueFile(code, options)) {
9494
result = parseScript(code, options)
9595
} else {
96+
const skipParsingScript = options.parser === false
9697
const tokenizer = new HTMLTokenizer(code)
9798
const rootAST = new HTMLParser(tokenizer, options).parse()
9899
const locationCalcurator = new LocationCalculator(
@@ -112,10 +113,11 @@ export function parseForESLint(
112113
? Object.assign(template, concreteInfo)
113114
: undefined
114115

115-
result =
116-
script != null
117-
? parseScriptElement(script, locationCalcurator, options)
118-
: parseScript("", options)
116+
if (skipParsingScript || script == null) {
117+
result = parseScript("", options)
118+
} else {
119+
result = parseScriptElement(script, locationCalcurator, options)
120+
}
119121

120122
result.ast.templateBody = templateBody
121123
}

test/parser-options.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @author Toru Nagashima <https://github.com/mysticatea>
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
"use strict"
6+
7+
const assert = require("assert")
8+
const { rules } = require("@mysticatea/eslint-plugin")
9+
const parseForESLint = require("..").parseForESLint
10+
const eslint = require("./fixtures/eslint")
11+
const Linter = eslint.Linter
12+
13+
describe.only("parserOptions", () => {
14+
describe("parser", () => {
15+
const linter = new Linter()
16+
linter.defineParser("vue-eslint-parser", { parseForESLint })
17+
linter.defineRule(
18+
"vue/valid-template-root",
19+
rules["vue/valid-template-root"]
20+
)
21+
22+
it("false then skip parsing '<script>'.", () => {
23+
const code = `<template>Hello</template>
24+
<script>This is syntax error</script>`
25+
const config = {
26+
parser: "vue-eslint-parser",
27+
parserOptions: {
28+
parser: false,
29+
},
30+
rules: {
31+
"vue/valid-template-root": "error",
32+
},
33+
}
34+
const messages = linter.verify(code, config, "test.vue")
35+
36+
assert.strictEqual(messages.length, 1)
37+
assert.strictEqual(messages[0].ruleId, "vue/valid-template-root")
38+
})
39+
})
40+
})

0 commit comments

Comments
 (0)