Skip to content

Commit bb58633

Browse files
authored
refactor (#246)
1 parent 769da24 commit bb58633

27 files changed

+153
-195
lines changed

scripts/update-fixtures-ast.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ for (const name of TARGETS) {
102102
const servicesPath = path.join(ROOT, `${name}/services.json`)
103103
const source = fs.readFileSync(sourcePath, "utf8")
104104
const parserOptions = optionsPath ? require(optionsPath) : {}
105-
const options = Object.assign(
106-
{ filePath: sourcePath },
107-
PARSER_OPTIONS,
108-
parserOptions,
109-
)
105+
const options = {
106+
filePath: sourcePath,
107+
...PARSER_OPTIONS,
108+
...parserOptions,
109+
}
110110
// console.log("Start:", name)
111111
const actual = parser.parseForESLint(source, options)
112112
const tokenRanges = getAllTokens(actual.ast).map((t) =>

scripts/update-fixtures-document-fragment.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ for (const name of TARGETS) {
5858
path.join(ROOT, `${name}/parser-options.js`),
5959
].find((fp) => fs.existsSync(fp))
6060
const source = fs.readFileSync(sourcePath, "utf8")
61-
const options = Object.assign(
62-
{ filePath: sourcePath },
63-
PARSER_OPTIONS,
64-
optionsPath ? require(optionsPath) : {},
65-
)
61+
const options = {
62+
filePath: sourcePath,
63+
...PARSER_OPTIONS,
64+
...(optionsPath ? require(optionsPath) : {}),
65+
}
66+
6667
const result = parser.parseForESLint(source, options)
6768
const actual = result.services.getDocumentFragment()
6869

src/ast/traverse.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const KEYS = Evk.unionWith({
2727
VSlotScopeExpression: ["params"],
2828
VStartTag: ["attributes"],
2929
VText: [],
30+
VGenericExpression: ["expression"],
3031
})
3132

3233
/**
@@ -82,7 +83,7 @@ function traverse(node: Node, parent: Node | null, visitor: Visitor): void {
8283
visitor.enterNode(node, parent)
8384

8485
const keys =
85-
(visitor.visitorKeys || KEYS)[node.type] || getFallbackKeys(node)
86+
(visitor.visitorKeys ?? KEYS)[node.type] ?? getFallbackKeys(node)
8687
for (i = 0; i < keys.length; ++i) {
8788
const child = (node as any)[keys[i]]
8889

src/common/ast-utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ export function isLang(
7878
* @returns The `lang` attribute value.
7979
*/
8080
export function getLang(element: VElement | undefined): string | null {
81-
const langAttr = element && element.startTag.attributes.find(isLang)
82-
const lang = langAttr && langAttr.value && langAttr.value.value
81+
const langAttr = element?.startTag.attributes.find(isLang)
82+
const lang = langAttr?.value?.value
8383
return lang || null
8484
}
8585
/**

src/common/eslint-scope.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let escopeCache: ESLintScope | null = null
1212
* Load the newest `eslint-scope` from the loaded ESLint or dependency.
1313
*/
1414
export function getEslintScope(): ESLintScope {
15-
return escopeCache || (escopeCache = getNewest())
15+
return escopeCache ?? (escopeCache = getNewest())
1616
}
1717

1818
/**

src/common/espree.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let espreeCache: Espree | null = null
1616
* Gets the espree that the given ecmaVersion can parse.
1717
*/
1818
export function getEspree(): Espree {
19-
return espreeCache || (espreeCache = getNewestEspree())
19+
return espreeCache ?? (espreeCache = getNewestEspree())
2020
}
2121

2222
export function getEcmaVersionIfUseEspree(

src/common/fix-locations.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ export function fixLocations(
2424
): void {
2525
fixNodeLocations(result.ast, result.visitorKeys, locationCalculator)
2626

27-
for (const token of result.ast.tokens || []) {
27+
for (const token of result.ast.tokens ?? []) {
2828
fixLocation(token, locationCalculator)
2929
}
30-
for (const comment of result.ast.comments || []) {
30+
for (const comment of result.ast.comments ?? []) {
3131
fixLocation(comment, locationCalculator)
3232
}
3333
}

src/common/location-calculator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class LocationCalculatorForHtml
6262
super(ltOffsets)
6363
this.gapOffsets = gapOffsets
6464
this.ltOffsets = ltOffsets
65-
this.baseOffset = baseOffset || 0
65+
this.baseOffset = baseOffset ?? 0
6666
this.baseIndexOfGap =
6767
this.baseOffset === 0
6868
? 0

src/external/token-store/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ export default class TokenStore {
334334
)
335335
.getOneToken()
336336

337-
if (token && token.range[0] === offset) {
337+
if (token?.range[0] === offset) {
338338
return token
339339
}
340340
return null

src/html/intermediate-tokenizer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export class IntermediateTokenizer {
222222
private processComment(token: Token): IntermediateToken | null {
223223
this.comments.push(token)
224224

225-
if (this.currentToken != null && this.currentToken.type === "Text") {
225+
if (this.currentToken?.type === "Text") {
226226
return this.commit()
227227
}
228228
return null

src/html/parser.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ export class Parser {
421421

422422
if (name === "template") {
423423
const xmlns = token.attributes.find((a) => a.key.name === "xmlns")
424-
const value = xmlns && xmlns.value && xmlns.value.value
424+
const value = xmlns?.value?.value
425425

426426
if (value === NS.HTML || value === NS.MathML || value === NS.SVG) {
427427
return value
@@ -477,7 +477,7 @@ export class Parser {
477477

478478
node.key.name = adjustAttributeName(node.key.name, namespace)
479479
const key = this.getTagName(node.key)
480-
const value = node.value && node.value.value
480+
const value = node.value?.value
481481

482482
if (key === "xmlns" && value !== namespace) {
483483
this.reportParseError(node, "x-invalid-namespace")
@@ -616,8 +616,7 @@ export class Parser {
616616
for (const attribute of element.startTag.attributes) {
617617
if (attribute.directive) {
618618
if (
619-
attribute.key.argument != null &&
620-
attribute.key.argument.type === "VExpressionContainer"
619+
attribute.key.argument?.type === "VExpressionContainer"
621620
) {
622621
resolveReferences(attribute.key.argument)
623622
}

src/html/tokenizer.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -980,8 +980,7 @@ export class Tokenizer {
980980
function maybeValidCustomBlock(this: Tokenizer, nextCp: number) {
981981
return (
982982
this.currentToken &&
983-
this.lastTagOpenToken &&
984-
this.lastTagOpenToken.value.startsWith(
983+
this.lastTagOpenToken?.value.startsWith(
985984
this.currentToken.value + String.fromCodePoint(nextCp),
986985
)
987986
)

src/index.ts

+14-16
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,13 @@ export function parseForESLint(
4949
code: string,
5050
parserOptions: any,
5151
): AST.ESLintExtendedProgram {
52-
const options: ParserOptions = Object.assign(
53-
{
54-
comment: true,
55-
loc: true,
56-
range: true,
57-
tokens: true,
58-
},
59-
parserOptions || {},
60-
)
52+
const options: ParserOptions = {
53+
comment: true,
54+
loc: true,
55+
range: true,
56+
tokens: true,
57+
...(parserOptions ?? {}),
58+
}
6159

6260
let result: AST.ESLintExtendedProgram
6361
let document: AST.VDocumentFragment | null
@@ -70,12 +68,12 @@ export function parseForESLint(
7068
;({ result, document, locationCalculator } = parseAsSFC(code, options))
7169
}
7270

73-
result.services = Object.assign(
74-
result.services || {},
75-
services.define(code, result.ast, document, locationCalculator, {
71+
result.services = {
72+
...(result.services || {}),
73+
...services.define(code, result.ast, document, locationCalculator, {
7674
parserOptions: options,
7775
}),
78-
)
76+
}
7977

8078
return result
8179
}
@@ -96,7 +94,7 @@ export { AST }
9694
function parseAsSFC(code: string, options: ParserOptions) {
9795
const optionsForTemplate = {
9896
...options,
99-
ecmaVersion: options.ecmaVersion || DEFAULT_ECMA_VERSION,
97+
ecmaVersion: options.ecmaVersion ?? DEFAULT_ECMA_VERSION,
10098
}
10199
const skipParsingScript = options.parser === false
102100
const tokenizer = new HTMLTokenizer(code, optionsForTemplate)
@@ -128,7 +126,7 @@ function parseAsSFC(code: string, options: ParserOptions) {
128126
if (skipParsingScript || !scripts.length) {
129127
result = parseScript("", {
130128
...options,
131-
ecmaVersion: options.ecmaVersion || DEFAULT_ECMA_VERSION,
129+
ecmaVersion: options.ecmaVersion ?? DEFAULT_ECMA_VERSION,
132130
parser: scriptParser,
133131
})
134132
} else if (
@@ -195,7 +193,7 @@ function parseAsSFC(code: string, options: ParserOptions) {
195193
function parseAsScript(code: string, options: ParserOptions) {
196194
return parseScript(code, {
197195
...options,
198-
ecmaVersion: options.ecmaVersion || DEFAULT_ECMA_VERSION,
196+
ecmaVersion: options.ecmaVersion ?? DEFAULT_ECMA_VERSION,
199197
parser: getScriptParser(options.parser, () => {
200198
const ext = (
201199
path.extname(options.filePath || "unknown.js").toLowerCase() ||

src/parser-services.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ export function define(
341341
)
342342
// Register handlers into the intermediate event emitter.
343343
for (const selector of Object.keys(
344-
visitor || {},
344+
visitor ?? {},
345345
)) {
346346
emitter.on(selector, visitor![selector])
347347
}

src/script-setup/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export function parseScriptSetupElements(
216216
): ESLintExtendedProgram {
217217
const parserOptions: ParserOptions = {
218218
...originalParserOptions,
219-
ecmaVersion: originalParserOptions.ecmaVersion || DEFAULT_ECMA_VERSION,
219+
ecmaVersion: originalParserOptions.ecmaVersion ?? DEFAULT_ECMA_VERSION,
220220
}
221221
const scriptSetupModuleCodeBlocks = getScriptSetupModuleCodeBlocks(
222222
scriptSetupElement,
@@ -323,7 +323,7 @@ export function parseScriptSetupElements(
323323
const textNode = node.children[0]
324324
return Math.min(
325325
start,
326-
textNode != null && textNode.type === "VText"
326+
textNode?.type === "VText"
327327
? textNode.range[0]
328328
: node.startTag.range[1],
329329
)
@@ -344,7 +344,7 @@ export function parseScriptSetupElements(
344344
const textNode = node.children[0]
345345
return Math.max(
346346
end,
347-
textNode != null && textNode.type === "VText"
347+
textNode?.type === "VText"
348348
? textNode.range[1]
349349
: (node.endTag?.range[0] ?? node.range[1]),
350350
)
@@ -575,7 +575,7 @@ function getScriptSetupCodeBlocks(
575575
(t) => t.range[0] === body.range[0],
576576
)
577577
const exportToken = tokens[exportTokenIndex]
578-
if (exportToken && exportToken.value === "export") {
578+
if (exportToken?.value === "export") {
579579
// Consume code up to the start position of `export`.
580580
// The code may contain legacy decorators.
581581
append(statementCodeBlocks, usedOffset, exportToken.range[0])
@@ -958,7 +958,7 @@ function remapLocationAndTokens(
958958
{ codeBlocks }: ScriptSetupModuleCodeBlocks,
959959
locationCalculator: LocationCalculator,
960960
) {
961-
const tokens = result.ast.tokens || []
961+
const tokens = result.ast.tokens ?? []
962962

963963
const endMap = new Map<number, number>()
964964
const buffer: number[] = []

src/script-setup/scope-analyzer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function extractVariables(scopeManager: escopeTypes.ScopeManager) {
131131
const moduleScope = globalScope.childScopes.find(
132132
(scope) => scope.type === "module",
133133
)
134-
for (const variable of (moduleScope && moduleScope.variables) || []) {
134+
for (const variable of moduleScope?.variables ?? []) {
135135
scriptVariables.set(variable.name, variable)
136136
}
137137
return scriptVariables

0 commit comments

Comments
 (0)