-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathparser-options.ts
121 lines (109 loc) · 3.41 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import * as path from "path"
import type { VDocumentFragment } from "../ast/index"
import type { CustomTemplateTokenizerConstructor } from "../html/custom-tokenizer"
import { getLang, isScriptElement, isScriptSetupElement } from "./ast-utils"
import type { ParserObject } from "./parser-object"
import { isParserObject } from "./parser-object"
export interface ParserOptions {
// vue-eslint-parser options
parser?:
| boolean
| string
| ParserObject
| Record<string, string | ParserObject | undefined>
vueFeatures?: {
interpolationAsNonHTML?: boolean // default true
filter?: boolean // default true
styleCSSVariableInjection?: boolean // default true
customMacros?: string[]
}
// 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[]
projectService?: boolean | ProjectServiceOptions
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?: Record<
string,
string | CustomTemplateTokenizerConstructor | undefined
>
}
interface ProjectServiceOptions {
allowDefaultProject?: string[]
defaultProject?: string
loadTypeScriptPlugins?: boolean
maximumDefaultProjectFileMatchCount_THIS_WILL_SLOW_DOWN_LINTING?: number
}
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
| ParserObject
| Record<string, string | ParserObject | undefined>
| undefined,
getParserLang: () => string | null | Iterable<string | null>,
): string | ParserObject | undefined {
if (isParserObject(parser)) {
return parser
}
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" ||
isParserObject(parserForLang)
) {
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
}