Skip to content

Commit e5b2cbb

Browse files
committed
Update: support normal js files
1 parent fd2b964 commit e5b2cbb

File tree

6 files changed

+67
-1
lines changed

6 files changed

+67
-1
lines changed

index.js

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

12+
const path = require("path")
1213
const espree = require("espree")
1314
const SAXParser = require("parse5").SAXParser
1415

@@ -55,7 +56,23 @@ function extractFirstScript(originalText) {
5556
// Exports
5657
//------------------------------------------------------------------------------
5758

59+
/**
60+
* Parses the source code.
61+
*
62+
* If `options.filePath` is a `.vue` file, this extracts the first `<script>`
63+
* element then parses it.
64+
*
65+
* @memberof module:vue-eslint-parser
66+
* @function parse
67+
* @param {string} text - The source code to be parsed.
68+
* @param {object} options - The option object for espree.
69+
* @returns {ASTNode} The AST object as the result of parsing.
70+
*/
5871
module.exports.parse = function parse(text, options) {
72+
if (path.extname(options.filePath || "unknown.js") !== ".vue") {
73+
return espree.parse(text, options)
74+
}
75+
5976
const script = extractFirstScript(text)
6077
const ast = espree.parse(script.text, options)
6178

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"rimraf": "^2.5.4"
4040
},
4141
"peerDependencies": {
42-
"eslint": ">=3"
42+
"eslint": ">=3.5.0"
4343
},
4444
"repository": {
4545
"type": "git",

test/fixtures/.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"parser": "../../index.js"
3+
}

test/fixtures/notvue.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("hello")

test/fixtures/notvue.js.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("hello");

test/index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,47 @@ describe("About fixtures/hello.vue", () => {
7373
assert(actual === expected)
7474
})
7575
})
76+
77+
describe("About fixtures/notvue.js", () => {
78+
beforeEach(() => {
79+
fs.copySync(ORIGINAL_FIXTURE_DIR, FIXTURE_DIR)
80+
})
81+
afterEach(() => {
82+
fs.removeSync(FIXTURE_DIR)
83+
})
84+
85+
it("should notify a 'semi' error", () => {
86+
const cli = new CLIEngine({
87+
cwd: FIXTURE_DIR,
88+
envs: ["es6", "node"],
89+
parser: PARSER_PATH,
90+
rules: {semi: "error"},
91+
useEslintrc: false,
92+
})
93+
const report = cli.executeOnFiles(["notvue.js"])
94+
const messages = report.results[0].messages
95+
96+
assert(messages.length === 1)
97+
assert(messages[0].ruleId === "semi")
98+
assert(messages[0].line === 1)
99+
assert(messages[0].column === 21)
100+
assert(messages[0].source === "console.log(\"hello\")")
101+
})
102+
103+
it("should fix a 'semi' error with --fix option", () => {
104+
const cli = new CLIEngine({
105+
cwd: FIXTURE_DIR,
106+
envs: ["es6", "node"],
107+
fix: true,
108+
parser: PARSER_PATH,
109+
rules: {semi: "error"},
110+
useEslintrc: false,
111+
})
112+
CLIEngine.outputFixes(cli.executeOnFiles(["notvue.js"]))
113+
114+
const actual = fs.readFileSync(path.join(FIXTURE_DIR, "notvue.js"), "utf8")
115+
const expected = fs.readFileSync(path.join(FIXTURE_DIR, "notvue.js.fixed"), "utf8")
116+
117+
assert(actual === expected)
118+
})
119+
})

0 commit comments

Comments
 (0)