Skip to content

Commit

Permalink
Chore: refactoring and add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Dec 22, 2016
1 parent 63eb62f commit efc5fc7
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 175 deletions.
43 changes: 29 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,34 @@ $ npm install --save-dev eslint vue-eslint-parser
```

```bash
$ eslint "src/**.{js,vue}"
$ eslint "src/**/*.{js,vue}"
# or
$ eslint src --ext .vue
```

## :wrench: Options

`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:

```json
{
"parser": "vue-eslint-parser",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2017
// ...
"ecmaVersion": 2017,
"ecmaFeatures": {
"globalReturn": false,
"impliedStrict": false,
"jsx": false,
"experimentalObjectRestSpread": false
}
}
}
```

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:

```json
Expand All @@ -67,16 +72,26 @@ For example:
}
```

```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.

## :newspaper: Changelog
Expand Down
47 changes: 7 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,7 @@
// Requirements
//------------------------------------------------------------------------------

const path = require("path")
const parse = require("./lib/parse")

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
* 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")
}
const Parser = require("./lib/parser")

//------------------------------------------------------------------------------
// Exports
Expand All @@ -44,28 +27,12 @@ module.exports = {
* If `options.filePath` is a `.vue` file, this extracts the first `<script>`
* element then parses it.
*
* @param {string} text - The source code to be parsed.
* @param {object} options - The option object for espree.
* @returns {{ast: ASTNode}} The AST object as the result of parsing.
* @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(text, options) {
const parser = getParser(options)

if (path.extname(options.filePath || "unknown.js") !== ".vue") {
return parser.parse(text, options)
}

const script = parse(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
parse(code, options) {
const parser = new Parser(options)
return parser.parseComponent(code)
},
}
118 changes: 0 additions & 118 deletions lib/parse.js

This file was deleted.

Loading

0 comments on commit efc5fc7

Please sign in to comment.