-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathparser-options.ts
88 lines (79 loc) · 2.47 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
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 false
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
// others
// [key: string]: any
}
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 SFC document fragment.
*/
export function getScriptParser(
parser: boolean | string | Record<string, string | undefined> | undefined,
doc: VDocumentFragment | null,
block: "script" | "template",
): 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]
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)
}
}
return null
}
}