Skip to content

Commit b16a6c1

Browse files
committed
Update: support custom persers
1 parent d66d80d commit b16a6c1

File tree

6 files changed

+181
-4
lines changed

6 files changed

+181
-4
lines changed

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,37 @@ $ eslint "src/**.{js,vue}"
3636
$ eslint src --ext .vue
3737
```
3838

39+
## :wrench: Options
40+
41+
`parserOptions` is the same as what [espree](https://github.com/eslint/espree#usage), the default parser of ESLint, is supporting.
42+
For example:
43+
44+
```json
45+
{
46+
"parser": "vue-eslint-parser",
47+
"parserOptions": {
48+
"sourceType": "module",
49+
"ecmaVersion": 2017
50+
// ...
51+
}
52+
}
53+
```
54+
55+
On the other hand, you can specify a custom parser to parse `<script>` tags.
56+
In this case, specify `parser` property. Other properties than `parser` would be given to the specified parser.
57+
For example:
58+
59+
```json
60+
{
61+
"parser": "vue-eslint-parser",
62+
"parserOptions": {
63+
"parser": "babel-eslint",
64+
"sourceType": "module",
65+
"allowImportExportEverywhere": false
66+
}
67+
}
68+
```
69+
3970
## :warning: Known Limitations
4071

4172
- [no-trailing-spaces](http://eslint.org/docs/rules/no-trailing-spaces) rule is warning lines outside of `<script>` tags as well.

index.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//------------------------------------------------------------------------------
1111

1212
const path = require("path")
13-
const espree = require("espree")
1413
const SAXParser = require("parse5").SAXParser
1514

1615
//------------------------------------------------------------------------------
@@ -19,6 +18,18 @@ const SAXParser = require("parse5").SAXParser
1918

2019
const LINE_TERMINATORS = /\r\n|\r|\n|\u2028|\u2029/g
2120

21+
/**
22+
* Gets the specified parser.
23+
* If it's unspecified, this returns espree.
24+
*
25+
* @param {object} options - The option object.
26+
* @param {string} [options.parser] - The parser name to get.
27+
* @returns {object} The gotten parser.
28+
*/
29+
function getParser(options) {
30+
return require(options.parser || "espree")
31+
}
32+
2233
/**
2334
* Calculates the end location.
2435
*
@@ -132,12 +143,14 @@ function extractFirstScript(originalText) {
132143
* @returns {ASTNode} The AST object as the result of parsing.
133144
*/
134145
module.exports.parse = function parse(text, options) {
146+
const parser = getParser(options)
147+
135148
if (path.extname(options.filePath || "unknown.js") !== ".vue") {
136-
return espree.parse(text, options)
149+
return parser.parse(text, options)
137150
}
138151

139152
const script = extractFirstScript(text)
140-
const ast = espree.parse(script.text, options)
153+
const ast = parser.parse(script.text, options)
141154

142155
ast.start = script.offset
143156
if (script.startToken) {

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
},
2525
"devDependencies": {
2626
"@types/node": "^6.0.52",
27+
"babel-eslint": "^7.1.1",
2728
"babel-preset-power-assert": "^1.0.0",
2829
"babel-register": "^6.18.0",
2930
"codecov": "^1.0.1",
@@ -34,7 +35,9 @@
3435
"nyc": "^10.0.0",
3536
"opener": "^1.4.2",
3637
"power-assert": "^1.4.2",
37-
"rimraf": "^2.5.4"
38+
"rimraf": "^2.5.4",
39+
"typescript": "^2.1.4",
40+
"typescript-eslint-parser": "^1.0.0"
3841
},
3942
"peerDependencies": {
4043
"eslint": ">=3.5.0"

test/fixtures/typed.vue

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<template>
2+
<p>{{ greeting }} World!</p>
3+
</template>
4+
5+
<script>
6+
interface Data {
7+
greeting: string
8+
}
9+
10+
export default {
11+
data(): Data {
12+
return {greeting: "Hello"}
13+
},
14+
}
15+
</script>
16+
17+
<style scoped>
18+
p {
19+
font-size: 2em;
20+
text-align: center;
21+
}
22+
</style>

test/fixtures/typed.vue.fixed

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<template>
2+
<p>{{ greeting }} World!</p>
3+
</template>
4+
5+
<script>
6+
interface Data {
7+
greeting: string
8+
}
9+
10+
export default {
11+
data(): Data {
12+
return {greeting: "Hello"};
13+
},
14+
};
15+
</script>
16+
17+
<style scoped>
18+
p {
19+
font-size: 2em;
20+
text-align: center;
21+
}
22+
</style>

test/index.js

+86
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,89 @@ describe("About fixtures/crlf.vue", () => {
198198
assert(messages.length === 0)
199199
})
200200
})
201+
202+
describe("About fixtures/typed.vue", () => {
203+
beforeEach(() => {
204+
fs.copySync(ORIGINAL_FIXTURE_DIR, FIXTURE_DIR)
205+
})
206+
afterEach(() => {
207+
fs.removeSync(FIXTURE_DIR)
208+
})
209+
210+
it("should notify no error with 'babel-eslint'", () => {
211+
const cli = new CLIEngine({
212+
cwd: FIXTURE_DIR,
213+
envs: ["es6", "node"],
214+
parser: PARSER_PATH,
215+
parserOptions: {
216+
parser: "babel-eslint",
217+
sourceType: "module",
218+
},
219+
rules: {semi: ["error", "never"]},
220+
useEslintrc: false,
221+
})
222+
const report = cli.executeOnFiles(["typed.vue"])
223+
const messages = report.results[0].messages
224+
225+
assert(messages.length === 0)
226+
})
227+
228+
it("should notify no error with 'typescript-eslint-parser'", () => {
229+
const cli = new CLIEngine({
230+
cwd: FIXTURE_DIR,
231+
envs: ["es6", "node"],
232+
parser: PARSER_PATH,
233+
parserOptions: {
234+
parser: "typescript-eslint-parser",
235+
sourceType: "module",
236+
},
237+
rules: {semi: ["error", "never"]},
238+
useEslintrc: false,
239+
})
240+
const report = cli.executeOnFiles(["typed.vue"])
241+
const messages = report.results[0].messages
242+
243+
assert(messages.length === 0)
244+
})
245+
246+
it("should fix 'semi' errors with --fix option with 'babel-eslint'", () => {
247+
const cli = new CLIEngine({
248+
cwd: FIXTURE_DIR,
249+
envs: ["es6", "node"],
250+
fix: true,
251+
parser: PARSER_PATH,
252+
parserOptions: {
253+
parser: "babel-eslint",
254+
sourceType: "module",
255+
},
256+
rules: {semi: ["error", "always"]},
257+
useEslintrc: false,
258+
})
259+
CLIEngine.outputFixes(cli.executeOnFiles(["typed.vue"]))
260+
261+
const actual = fs.readFileSync(path.join(FIXTURE_DIR, "typed.vue"), "utf8")
262+
const expected = fs.readFileSync(path.join(FIXTURE_DIR, "typed.vue.fixed"), "utf8")
263+
264+
assert(actual === expected)
265+
})
266+
267+
it("should fix 'semi' errors with --fix option with 'typescript-eslint-parser'", () => {
268+
const cli = new CLIEngine({
269+
cwd: FIXTURE_DIR,
270+
envs: ["es6", "node"],
271+
fix: true,
272+
parser: PARSER_PATH,
273+
parserOptions: { //
274+
parser: "typescript-eslint-parser",
275+
},
276+
rules: {semi: ["error", "always"]},
277+
useEslintrc: false,
278+
})
279+
CLIEngine.outputFixes(cli.executeOnFiles(["typed.vue"]))
280+
281+
const actual = fs.readFileSync(path.join(FIXTURE_DIR, "typed.vue"), "utf8")
282+
const expected = fs.readFileSync(path.join(FIXTURE_DIR, "typed.vue.fixed"), "utf8")
283+
284+
assert(actual === expected)
285+
})
286+
})

0 commit comments

Comments
 (0)