8000 Merge branch 'vuejs:master' into master · waynzh/vue-eslint-parser@9f11754 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f11754

Browse files
authored
Merge branch 'vuejs:master' into master
2 parents 8ec8337 + ae61e8d commit 9f11754

32 files changed

+17004
-194
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-eslint-parser",
3-
"version": "9.4.0",
3+
"version": "9.4.2",
44
"description": "The ESLint custom parser for `.vue` files.",
55
"engines": {
66
"node": "^14.17.0 || >=16.0.0"

scripts/update-fixtures-ast.js

Lines changed: 6 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212
const fs = require("fs")
1313
const path = require("path")
1414
const parser = require("../src")
15-
const escope = require("eslint-scope")
1615
const semver = require("semver")
16+
const {
17+
scopeToJSON,
18+
analyze,
19+
replacer,
20+
getAllTokens,
21+
} = require("../test/test-utils")
1722

1823
//------------------------------------------------------------------------------
1924
// Helpers
@@ -30,40 +35,6 @@ const PARSER_OPTIONS = {
3035
eslintScopeManager: true,
3136
}
3237

33-
/**
34-
* Remove `parent` proeprties from the given AST.
35-
* @param {string} key The key.
36-
* @param {any} value The value of the key.
37-
* @returns {any} The value of the key to output.
38-
*/
39-
function replacer(key, value) {
40-
if (key === "parent") {
41-
return undefined
42-
}
43-
if (key === "errors" && Array.isArray(value)) {
44-
return value.map((e) => ({
45-
message: e.message,
46-
index: e.index,
47-
lineNumber: e.lineNumber,
48-
column: e.column,
49-
}))
50-
}
51-
return value
52-
}
53-
54-
/**
55-
* Get all tokens of the given AST.
56-
* @param {ASTNode} ast The root node of AST.
57-
* @returns {Token[]} Tokens.
58-
*/
59-
function getAllTokens(ast) {
60-
const tokenArrays = [ast.tokens, ast.comments]
61-
if (ast.templateBody != null) {
62-
tokenArrays.push(ast.templateBody.tokens, ast.templateBody.comments)
63-
}
64-
return Array.prototype.concat.apply([], tokenArrays)
65-
}
66-
6738
/**
6839
* Create simple tree.
6940
* @param {string} source The source code.
@@ -98,109 +69,6 @@ function getTree(source, ast) {
9869
return root.children
9970
}
10071

101-
function scopeToJSON(scopeManager) {
102-
return JSON.stringify(normalizeScope(scopeManager.globalScope), replacer, 4)
103-
104-
function normalizeScope(scope) {
105-
return {
106-
type: scope.type,
107-
variables: scope.variables.map(normalizeVar),
108-
references: scope.references.map(normalizeReference),
109-
childScopes: scope.childScopes.map(normalizeScope),
110-
through: scope.through.map(normalizeReference),
111-
}
112-
}
113-
114-
function normalizeVar(v) {
115-
return {
116-
name: v.name,
117-
identifiers: v.identifiers.map(normalizeId),
118-
defs: v.defs.map(normalizeDef),
119-
references: v.references.map(normalizeReference),
120-
}
121-
}
122-
123-
function normalizeReference(reference) {
124-
return {
125-
identifier: normalizeId(reference.identifier),
126-
from: reference.from.type,
127-
resolved: normalizeId(
128-
reference.resolved &&
129-
reference.resolved.defs &&
130-
reference.resolved.defs[0] &&
131-
reference.resolved.defs[0].name,
132-
),
133-
init: reference.init || null,
134-
vueUsedInTemplate: reference.vueUsedInTemplate
135-
? reference.vueUsedInTemplate
136-
: undefined,
137-
}
138-
}
139-
140-
function normalizeDef(def) {
141-
return {
142-
type: def.type,
143-
node: normalizeDefNode(def.node),
144-
name: def.name.name,
145-
}
146-
}
147-
148-
function normalizeId(identifier) {
149-
return (
150-
identifier && {
151-
type: identifier.type,
152-
name: identifier.name,
153-
loc: identifier.loc,
154-
}
155-
)
156-
}
157-
158-
function normalizeDefNode(node) {
159-
return {
160-
type: node.type,
161-
loc: node.loc,
162-
}
163-
}
164-
}
165-
166-
/**
167-
* Analyze scope
168-
*/
169-
function analyze(ast, parserOptions) {
170-
const ecmaVersion = parserOptions.ecmaVersion || 2017
171-
const ecmaFeatures = parserOptions.ecmaFeatures || {}
172-
const sourceType = parserOptions.sourceType || "script"
173-
const result = escope.analyze(ast, {
174-
ignoreEval: true,
175-
nodejsScope: false,
176-
impliedStrict: ecmaFeatures.impliedStrict,
177-
ecmaVersion,
178-
sourceType,
179-
fallback: getFallbackKeys,
180-
})
181-
182-
return result
183-
184-
function getFallbackKeys(node) {
185-
return Object.keys(node).filter(fallbackKeysFilter, node)
186-
}
187-
188-
function fallbackKeysFilter(key) {
189-
const value = null
190-
return (
191-
key !== "comments" &&
192-
key !== "leadingComments" &&
193-
key !== "loc" &&
194-
key !== "parent" &&
195-
key !== "range" &&
196-
key !== "tokens" &&
197-
key !== "trailingComments" &&
198-
typeof value === "object" &&
199-
(typeof value.type === "string" || Array.isArray(value))
200-
)
201-
}
202-
}
203-
20472
//------------------------------------------------------------------------------
20573
// Main
20674
//------------------------------------------------------------------------------

src/ast/nodes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ export interface ESLintArrowFunctionExpression extends HasLocation, HasParent {
486486
generator: boolean
487487
id: ESLintIdentifier | null
488488
params: ESLintPattern[]
489-
body: ESLintBlockStatement
489+
body: ESLintBlockStatement | ESLintExpression
490490
}
491491

492492
export interface ESLintSequenceExpression extends HasLocation, HasParent {

src/script/generic.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,40 @@ function getConstraint(node: TSESTree.TSTypeParameter, rawParam: string) {
124124
if (!node.constraint) {
125125
return "unknown"
126126
}
127-
const start = node.range[0]
128-
return rawParam.slice(
129-
node.constraint.range[0] - start,
130-
node.constraint.range[1] - start,
131-
)
127+
let index = rawParam.indexOf(node.name.name) + node.name.name.length
128+
let startIndex: number | null = null
129+
while (index < rawParam.length) {
130+
if (startIndex == null) {
131+
if (rawParam.startsWith("extends", index)) {
132+
startIndex = index = index + 7
133+
continue
134+
}
135+
} else if (rawParam[index] === "=") {
136+
return rawParam.slice(startIndex, index)
137+
}
138+
if (rawParam.startsWith("//", index)) {
139+
const lfIndex = rawParam.indexOf("\n", index)
140+
if (lfIndex >= 0) {
141+
index = lfIndex + 1
142+
continue
143+
}
144+
return "unknown"
145+
}
146+
if (rawParam.startsWith("/*", index)) {
147+
const endIndex = rawParam.indexOf("*/", index)
148+
if (endIndex >= 0) {
149+
index = endIndex + 2
150+
continue
151+
}
152+
return "unknown"
153+
}
154+
index++
155+
}
156+
if (startIndex == null) {
157+
return "unknown"
158+
}
159+
160+
return rawParam.slice(startIndex)
132161
}
133162

134163
/** Remove variable def */

src/script/index.ts

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import type {
3232
VSlotScopeExpression,
3333
OffsetRange,
3434
VGenericExpression,
35-
ESLintClassExpression,
3635
} from "../ast"
3736
import { ParseError } from "../ast"
3837
import { debug } from "../common/debug"
@@ -212,9 +211,34 @@ export function parseScriptFragment(
212211
code: string,
213212
locationCalculator: LocationCalculator,
214213
parserOptions: ParserOptions,
214+
): ESLintExtendedProgram {
215+
return parseScriptFragmentWithOption(
216+
code,
217+
locationCalculator,
218+
parserOptions,
219+
)
220+
}
221+
222+
/**
223+
* Parse the given source code.
224+
*
225+
* @param code The source code to parse.
226+
* @param locationCalculator The location calculator for fixLocations.
227+
* @param parserOptions The parser options.
228+
* @param processOptions The process options.
229+
* @returns The result of parsing.
230+
*/
231+
function parseScriptFragmentWithOption(
232+
code: string,
233+
locationCalculator: LocationCalculator,
234+
parserOptions: ParserOptions,
235+
processOptions?: {
236+
preFixLocationProcess?: (result: ESLintExtendedProgram) => void
237+
},
215238
): ESLintExtendedProgram {
216239
try {
217240
const result = parseScript(code, parserOptions)
241+
processOptions?.preFixLocationProcess?.(result)
218242
fixLocations(result, locationCalculator)
219243
return result
220244
} catch (err) {
@@ -1259,19 +1283,38 @@ export function parseGenericExpression(
12591283
throwEmptyError(locationCalculator, "a type parameter")
12601284
}
12611285

1286+
function getParams(result: ESLintExtendedProgram) {
1287+
const { ast } = result
1288+
const statement = ast.body[0] as ESLintExpressionStatement
1289+
const rawExpression = statement.expression as ESLintUnaryExpression
1290+
const classDecl = rawExpression.argument as ESLintFunctionExpression
1291+
const typeParameters = (classDecl as TSESTree.FunctionExpression)
1292+
.typeParameters
1293+
return typeParameters?.params
1294+
}
1295+
12621296
try {
1263-
const result = parseScriptFragment(
1264-
`void function<${code}>(){}`,
1297+
const rawParams: string[] = []
1298+
const scriptLet = `void function<${code}>(){}`
1299+
const result = parseScriptFragmentWithOption(
1300+
scriptLet,
12651301
locationCalculator.getSubCalculatorShift(-14),
12661302
{ ...parserOptions, project: undefined },
1303+
{
1304+
preFixLocationProcess(preResult) {
1305+
const params = getParams(preResult)
1306+
if (params) {
1307+
for (const param of params) {
1308+
rawParams.push(
1309+
scriptLet.slice(param.range[0], param.range[1]),
1310+
)
1311+
}
1312+
}
1313+
},
1314+
},
12671315
)
12681316
const { ast } = result
1269-
const statement = ast.body[0] as ESLintExpressionStatement
1270-
const rawExpression = statement.expression as ESLintUnaryExpression
1271-
const classDecl = rawExpression.argument as ESLintClassExpression
1272-
const typeParameters = (classDecl as TSESTree.ClassExpression)
1273-
.typeParameters
1274-
const params = typeParameters?.params
1317+
const params = getParams(result)
12751318

12761319
if (!params || params.length === 0) {
12771320
return {
@@ -1300,12 +1343,7 @@ export function parseGenericExpression(
13001343
loc: { start: firstParam.loc.start, end: lastParam.loc.end },
13011344
parent: DUMMY_PARENT,
13021345
params,
1303-
rawParams: params.map((param) =>
1304-
code.slice(
1305-
param.range[0] - typeParameters.range[0] - 1,
1306-
param.range[1] - typeParameters.range[0] - 1,
1307-
),
1308-
),
1346+
rawParams,
13091347
}
13101348

13111349
// Modify parent.

0 commit comments

Comments
 (0)
0