Skip to content

Commit efc5fc7

Browse files
committed
Chore: refactoring and add more tests.
1 parent 63eb62f commit efc5fc7

File tree

10 files changed

+346
-175
lines changed

10 files changed

+346
-175
lines changed

README.md

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,34 @@ $ npm install --save-dev eslint vue-eslint-parser
3131
```
3232

3333
```bash
34-
$ eslint "src/**.{js,vue}"
34+
$ eslint "src/**/*.{js,vue}"
3535
# or
3636
$ eslint src --ext .vue
3737
```
3838

3939
## :wrench: Options
4040

41-
`parserOptions` is the same as what [espree](https://github.com/eslint/espree#usage), the default parser of ESLint, is supporting.
41+
`parserOptions` has the same properties as what [espree](https://github.com/eslint/espree#usage), the default parser of ESLint, is supporting.
4242
For example:
4343

4444
```json
4545
{
4646
"parser": "vue-eslint-parser",
4747
"parserOptions": {
4848
"sourceType": "module",
49-
"ecmaVersion": 2017
50-
// ...
49+
"ecmaVersion": 2017,
50+
"ecmaFeatures": {
51+
"globalReturn": false,
52+
"impliedStrict": false,
53+
"jsx": false,
54+
"experimentalObjectRestSpread": false
55+
}
5156
}
5257
}
5358
```
5459

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.
60+
Also, you can use `parser` property to specify a custom parser to parse `<script>` tags.
61+
Other properties than parser would be given to the specified parser.
5762
For example:
5863

5964
```json
@@ -67,16 +72,26 @@ For example:
6772
}
6873
```
6974

75+
```json
76+
{
77+
"parser": "vue-eslint-parser",
78+
"parserOptions": {
79+
"parser": "typescript-eslint-parser"
80+
}
81+
}
82+
```
83+
7084
## :warning: Known Limitations
7185

72-
- Those rules are warning code due to the outside of `<script>` tags.
73-
Please disable those rules for `.vue` files as necessary.
74-
- [eol-last](http://eslint.org/docs/rules/eol-last)
75-
- [linebreak-style](http://eslint.org/docs/rules/linebreak-style)
76-
- [max-len](http://eslint.org/docs/rules/max-len)
77-
- [max-lines](http://eslint.org/docs/rules/max-lines)
78-
- [no-trailing-spaces](http://eslint.org/docs/rules/no-trailing-spaces)
79-
- [unicode-bom](http://eslint.org/docs/rules/unicode-bom)
86+
Some rules make warnings due to the outside of `<script>` tags.
87+
Please disable those rules for `.vue` files as necessary.
88+
89+
- [eol-last](http://eslint.org/docs/rules/eol-last)
90+
- [linebreak-style](http://eslint.org/docs/rules/linebreak-style)
91+
- [max-len](http://eslint.org/docs/rules/max-len)
92+
- [max-lines](http://eslint.org/docs/rules/max-lines)
93+
- [no-trailing-spaces](http://eslint.org/docs/rules/no-trailing-spaces)
94+
- [unicode-bom](http://eslint.org/docs/rules/unicode-bom)
8095
- Other rules which are using the source code text instead of AST might be confused as well.
8196

8297
## :newspaper: Changelog

index.js

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,7 @@
99
// Requirements
1010
//------------------------------------------------------------------------------
1111

12-
const path = require("path")
13-
const parse = require("./lib/parse")
14-
15-
//------------------------------------------------------------------------------
16-
// Helpers
17-
//------------------------------------------------------------------------------
18-
19-
/**
20-
* Gets the specified parser.
21-
* If it's unspecified, this returns espree.
22-
*
23-
* @param {object} options - The option object.
24-
* @param {string} [options.parser] - The parser name to get.
25-
* @returns {object} The gotten parser.
26-
*/
27-
function getParser(options) {
28-
return require(options.parser || "espree")
29-
}
12+
const Parser = require("./lib/parser")
3013

3114
//------------------------------------------------------------------------------
3215
// Exports
@@ -44,28 +27,12 @@ module.exports = {
4427
* If `options.filePath` is a `.vue` file, this extracts the first `<script>`
4528
* element then parses it.
4629
*
47-
* @param {string} text - The source code to be parsed.
48-
* @param {object} options - The option object for espree.
49-
* @returns {{ast: ASTNode}} The AST object as the result of parsing.
30+
* @param {string} code - The source code to be parsed.
31+
* @param {object} options - The option object.
32+
* @returns {{ast: ASTNode, services: any}} The result of parsing.
5033
*/
51-
parse(text, options) {
52-
const parser = getParser(options)
53-
54-
if (path.extname(options.filePath || "unknown.js") !== ".vue") {
55-
return parser.parse(text, options)
56-
}
57-
58-
const script = parse(text)
59-
const ast = parser.parse(script.text, options)
60-
61-
ast.start = script.offset
62-
if (script.startToken) {
63-
ast.tokens.unshift(script.startToken)
64-
}
65-
if (script.endToken) {
66-
ast.tokens.push(script.endToken)
67-
}
68-
69-
return ast
34+
parse(code, options) {
35+
const parser = new Parser(options)
36+
return parser.parseComponent(code)
7037
},
7138
}

lib/parse.js

Lines changed: 0 additions & 118 deletions
This file was deleted.

0 commit comments

Comments
 (0)