forked from vuejs/vue-eslint-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser-options.ts
91 lines (80 loc) · 2.55 KB
/
parser-options.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import * as path from "path"
import type { VDocumentFragment } from "../ast"
import { getLang, isScriptElement, isScriptSetupElement } from "./ast-utils"
export interface ParserOptions {
// vue-eslint-parser options
parser?: boolean | string
vueFeatures?: {
interpolationAsNonHTML?: boolean // default true
filter?: boolean // default true
styleCSSVariableInjection?: boolean // default true
}
// espree options
ecmaVersion?: number | "latest"
sourceType?: "script" | "module"
ecmaFeatures?: { [key: string]: any }
// @typescript-eslint/parser options
jsxPragma?: string
jsxFragmentName?: string | null
lib?: string[]
project?: string | string[]
projectFolderIgnoreList?: string[]
tsconfigRootDir?: string
extraFileExtensions?: string[]
warnOnUnsupportedTypeScriptVersion?: boolean
// set by eslint
filePath?: string
// enables by eslint
comment?: boolean
loc?: boolean
range?: boolean
tokens?: boolean
// From ESLint
eslintScopeManager?: boolean
// others
// [key: string]: any
templateTokenizer?: { [key: string]: string }
}
export function isSFCFile(parserOptions: ParserOptions) {
if (parserOptions.filePath === "<input>") {
return true
}
return path.extname(parserOptions.filePath || "unknown.vue") === ".vue"
}
/**
* Gets the script parser name from the given parser lang.
*/
export function getScriptParser(
parser: boolean | string | Record<string, string | undefined> | undefined,
getParserLang: () => string | null | Iterable<string | null>,
): string | undefined {
if (parser && typeof parser === "object") {
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
}
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
}