Skip to content

Commit 2859509

Browse files
committed
Fix: parserServices must exist always
1 parent dcfd2a2 commit 2859509

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

src/index.ts

+19-17
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,30 @@ export function parseForESLint(code: string, options: any): AST.ESLintExtendedPr
5656
tokens: true,
5757
}, options || {})
5858

59+
let result: AST.ESLintExtendedProgram
5960
if (!isVueFile(code, options)) {
60-
return parseScript(code, options)
61+
result = parseScript(code, options)
6162
}
63+
else {
64+
const tokenizer = new HTMLTokenizer(code)
65+
const rootAST = new HTMLParser(tokenizer, options).parse()
66+
const locationCalcurator = new LocationCalculator(tokenizer.gaps, tokenizer.lineTerminators)
67+
const script = rootAST.children.find(isScriptElement) as AST.VElement | undefined // https://github.com/Microsoft/TypeScript/issues/7657
68+
const template = rootAST.children.find(isTemplateElement) as AST.VElement | undefined
69+
const concreteInfo: AST.HasConcreteInfo = {
70+
tokens: rootAST.tokens,
71+
comments: rootAST.comments,
72+
errors: rootAST.errors,
73+
}
6274

63-
const tokenizer = new HTMLTokenizer(code)
64-
const rootAST = new HTMLParser(tokenizer, options).parse()
65-
const locationCalcurator = new LocationCalculator(tokenizer.gaps, tokenizer.lineTerminators)
66-
const script = rootAST.children.find(isScriptElement) as AST.VElement | undefined // https://github.com/Microsoft/TypeScript/issues/7657
67-
const template = rootAST.children.find(isTemplateElement) as AST.VElement | undefined
68-
const result = (script != null)
69-
? parseScriptElement(script, locationCalcurator, options)
70-
: parseScript("", options)
71-
const concreteInfo: AST.HasConcreteInfo = {
72-
tokens: rootAST.tokens,
73-
comments: rootAST.comments,
74-
errors: rootAST.errors,
75+
result = (script != null)
76+
? parseScriptElement(script, locationCalcurator, options)
77+
: parseScript("", options)
78+
result.ast.templateBody = (template != null)
79+
? Object.assign(template, concreteInfo)
80+
: undefined
7581
}
76-
const templateBody = (template != null)
77-
? Object.assign(template, concreteInfo)
78-
: undefined
7982

80-
result.ast.templateBody = templateBody
8183
result.services = Object.assign(result.services || {}, services.define(result.ast))
8284

8385
return result

test/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const assert = require("assert")
1313
const path = require("path")
1414
const fs = require("fs-extra")
1515
const parse = require("..").parse
16+
const parseForESLint = require("..").parseForESLint
1617
const CLIEngine = require("./fixtures/eslint").CLIEngine
1718

1819
//------------------------------------------------------------------------------
@@ -505,4 +506,14 @@ describe("Basic tests", () => {
505506
assert.equal(errors.length, 0)
506507
})
507508
})
509+
510+
describe("About parserServices", () => {
511+
it("should exist if the source code is a Vue SFC file.", () => {
512+
assert.notEqual(parseForESLint("test", {filePath: "test.vue"}).services, undefined)
513+
})
514+
515+
it("should exist even if the source code is not Vue SFC file.", () => {
516+
assert.notEqual(parseForESLint("test", {filePath: "test.js"}).services, undefined)
517+
})
518+
})
508519
})

0 commit comments

Comments
 (0)