Skip to content

Change to determine parser by extension when parsing non-vue files using multiple parser #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions src/common/parser-options.ts
Original file line number Diff line number Diff line change
@@ -47,42 +47,40 @@ export function isSFCFile(parserOptions: ParserOptions) {
}

/**
* Gets the script parser name from the given SFC document fragment.
* Gets the script parser name from the given parser lang.
*/
export function getScriptParser(
parser: boolean | string | Record<string, string | undefined> | undefined,
doc: VDocumentFragment | null,
block: "script" | "template",
getParserLang: () => string | null | Iterable<string | null>,
): string | undefined {
if (parser && typeof parser === "object") {
if (block === "template") {
const parserForTemplate = parser["<template>"]
if (typeof parserForTemplate === "string") {
return parserForTemplate
}
}
const lang = getScriptLang()
if (lang) {
const parserForLang = parser[lang]
const parserLang = getParserLang()
const parserLangs =
parserLang == null
? []
: typeof parserLang === "string"
? [parserLang]
: parserLang
for (const lang of parserLangs) {
const parserForLang = lang && parser[lang]
if (typeof parserForLang === "string") {
return parserForLang
}
}
return parser.js
}
return typeof parser === "string" ? parser : undefined
}

function getScriptLang() {
if (doc) {
const scripts = doc.children.filter(isScriptElement)
const script =
scripts.length === 2
? scripts.find(isScriptSetupElement)
: scripts[0]
if (script) {
return getLang(script)
}
export function getParserLangFromSFC(doc: VDocumentFragment): string | null {
if (doc) {
const scripts = doc.children.filter(isScriptElement)
const script =
(scripts.length === 2 && scripts.find(isScriptSetupElement)) ||
scripts[0]
if (script) {
return getLang(script)
}
return null
}
return null
}
16 changes: 12 additions & 4 deletions src/html/parser.ts
Original file line number Diff line number Diff line change
@@ -46,7 +46,11 @@ import type {
import { IntermediateTokenizer } from "./intermediate-tokenizer"
import type { Tokenizer } from "./tokenizer"
import type { ParserOptions } from "../common/parser-options"
import { isSFCFile, getScriptParser } from "../common/parser-options"
import {
isSFCFile,
getScriptParser,
getParserLangFromSFC,
} from "../common/parser-options"

const DIRECTIVE_NAME = /^(?:v-|[.:@#]).*[^.:@#]$/u
const DT_DD = /^d[dt]$/u
@@ -279,20 +283,24 @@ export class Parser {
this.popElementStackUntil(0)
propagateEndLocation(this.document)

const doc = this.document

const parserOptions = {
...this.baseParserOptions,
parser: getScriptParser(
this.baseParserOptions.parser,
this.document,
"template",
function* () {
yield "<template>"
yield getParserLangFromSFC(doc)
},
),
}
for (const proc of this.postProcessesForScript) {
proc(parserOptions)
}
this.postProcessesForScript = []

return this.document
return doc
}

/**
26 changes: 22 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ import { HTMLParser, HTMLTokenizer } from "./html"
import { parseScript, parseScriptElement } from "./script"
import * as services from "./parser-services"
import type { ParserOptions } from "./common/parser-options"
import { getScriptParser } from "./common/parser-options"
import { getScriptParser, getParserLangFromSFC } from "./common/parser-options"
import { parseScriptSetupElements } from "./script-setup"
import { LinesAndColumns } from "./common/lines-and-columns"
import type { VElement } from "./ast"
@@ -64,7 +64,20 @@ export function parseForESLint(
result = parseScript(code, {
...options,
ecmaVersion: options.ecmaVersion || DEFAULT_ECMA_VERSION,
parser: getScriptParser(options.parser, null, "script"),
parser: getScriptParser(options.parser, () => {
const ext = (
path
.extname(options.filePath || "unknown.js")
.toLowerCase() || ""
)
// remove dot
.slice(1)
if (/^[jt]sx$/u.test(ext)) {
return [ext, ext.slice(0, -1)]
}

return ext
}),
})
document = null
locationCalculator = null
@@ -94,7 +107,9 @@ export function parseForESLint(
? Object.assign(template, concreteInfo)
: undefined

const scriptParser = getScriptParser(options.parser, rootAST, "script")
const scriptParser = getScriptParser(options.parser, () =>
getParserLangFromSFC(rootAST),
)
let scriptSetup: VElement | undefined
if (skipParsingScript || !scripts.length) {
result = parseScript("", {
@@ -126,7 +141,10 @@ export function parseForESLint(
const styles = rootAST.children.filter(isStyleElement)
parseStyleElements(styles, locationCalculator, {
...options,
parser: getScriptParser(options.parser, rootAST, "template"),
parser: getScriptParser(options.parser, function* () {
yield "<template>"
yield getParserLangFromSFC(rootAST)
}),
})
}

18 changes: 12 additions & 6 deletions test/fixtures/ast/filters-opt-filter-off-babel/ast.json
Original file line number Diff line number Diff line change
@@ -10,7 +10,8 @@
"end": {
"line": 1,
"column": 0
}
},
"filename": "source.vue"
},
"range": [
0,
@@ -144,7 +145,8 @@
"end": {
"line": 2,
"column": 22
}
},
"filename": "source.vue"
},
"range": [
23,
@@ -162,7 +164,8 @@
"end": {
"line": 2,
"column": 15
}
},
"filename": "source.vue"
},
"range": [
23,
@@ -183,7 +186,8 @@
"end": {
"line": 2,
"column": 22
}
},
"filename": "source.vue"
},
"range": [
30,
@@ -206,7 +210,8 @@
"end": {
"line": 2,
"column": 15
}
},
"filename": "source.vue"
},
"range": [
23,
@@ -229,7 +234,8 @@
"end": {
"line": 2,
"column": 22
}
},
"filename": "source.vue"
},
"range": [
30,
9 changes: 9 additions & 0 deletions test/fixtures/typed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
interface Data {
greeting: string
}

export default {
data(): Data {
return {greeting: "Hello"}
},
}
9 changes: 9 additions & 0 deletions test/fixtures/typed.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
interface Data {
greeting: string
}

export default {
data(): Data {
return {greeting: "Hello"}
},
}
21 changes: 21 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -291,6 +291,27 @@ describe("Basic tests", () => {

assert.deepStrictEqual(messages, [])
})

it("should notify no error with multiple parser with '@typescript-eslint/parser'", async () => {
const cli = new ESLint({
cwd: FIXTURE_DIR,
overrideConfig: {
env: { es6: true, node: true },
parser: PARSER_PATH,
parserOptions: {
parser: {
ts: "@typescript-eslint/parser",
},
},
rules: { semi: ["error", "never"] },
},
useEslintrc: false,
})
const report = await cli.lintFiles(["typed.ts", "typed.tsx"])

assert.deepStrictEqual(report[0].messages, [])
assert.deepStrictEqual(report[1].messages, [])
})
}
})