Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f3ac447

Browse filesBrowse files
authored
Fix an error when using CRLF for generic directive (#220)
1 parent d4eef64 commit f3ac447
Copy full SHA for f3ac447

File tree

17 files changed

+9441
-18
lines changed
Filter options

17 files changed

+9441
-18
lines changed

‎src/script/generic.ts

Copy file name to clipboardExpand all lines: src/script/generic.ts
+25-5Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,31 @@ 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 startIndex = rawParam.indexOf(node.name.name) + node.name.name.length
128+
while (startIndex < rawParam.length) {
129+
if (rawParam.startsWith("extends", startIndex)) {
130+
return rawParam.slice(startIndex + 7)
131+
}
132+
if (rawParam.startsWith("//", startIndex)) {
133+
const lfIndex = rawParam.indexOf("\n", startIndex)
134+
if (lfIndex >= 0) {
135+
startIndex = lfIndex + 1
136+
continue
137+
}
138+
return "unknown"
139+
}
140+
if (rawParam.startsWith("/*", startIndex)) {
141+
const endIndex = rawParam.indexOf("*/", startIndex)
142+
if (endIndex >= 0) {
143+
startIndex = endIndex + 2
144+
continue
145+
}
146+
return "unknown"
147+
}
148+
startIndex++
149+
}
150+
151+
return "unknown"
132152
}
133153

134154
/** Remove variable def */

‎src/script/index.ts

Copy file name to clipboardExpand all lines: src/script/index.ts
+52-13Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,34 @@ export function parseScriptFragment(
212212
code: string,
213213
locationCalculator: LocationCalculator,
214214
parserOptions: ParserOptions,
215+
): ESLintExtendedProgram {
216+
return parseScriptFragmentWithOption(
217+
code,
218+
locationCalculator,
219+
parserOptions,
220+
)
221+
}
222+
223+
/**
224+
* Parse the given source code.
225+
*
226+
* @param code The source code to parse.
227+
* @param locationCalculator The location calculator for fixLocations.
228+
* @param parserOptions The parser options.
229+
* @param processOptions The process options.
230+
* @returns The result of parsing.
231+
*/
232+
function parseScriptFragmentWithOption(
233+
code: string,
234+
locationCalculator: LocationCalculator,
235+
parserOptions: ParserOptions,
236+
processOptions?: {
237+
preFixLocationProcess?: (result: ESLintExtendedProgram) => void
238+
},
215239
): ESLintExtendedProgram {
216240
try {
217241
const result = parseScript(code, parserOptions)
242+
processOptions?.preFixLocationProcess?.(result)
218243
fixLocations(result, locationCalculator)
219244
return result
220245
} catch (err) {
@@ -1259,19 +1284,38 @@ export function parseGenericExpression(
12591284
throwEmptyError(locationCalculator, "a type parameter")
12601285
}
12611286

1262-
try {
1263-
const result = parseScriptFragment(
1264-
`void function<${code}>(){}`,
1265-
locationCalculator.getSubCalculatorShift(-14),
1266-
{ ...parserOptions, project: undefined },
1267-
)
1287+
function getParams(result: ESLintExtendedProgram) {
12681288
const { ast } = result
12691289
const statement = ast.body[0] as ESLintExpressionStatement
12701290
const rawExpression = statement.expression as ESLintUnaryExpression
12711291
const classDecl = rawExpression.argument as ESLintClassExpression
12721292
const typeParameters = (classDecl as TSESTree.ClassExpression)
12731293
.typeParameters
1274-
const params = typeParameters?.params
1294+
return typeParameters?.params
1295+
}
1296+
1297+
try {
1298+
const rawParams: string[] = []
1299+
const scriptLet = `void function<${code}>(){}`
1300+
const result = parseScriptFragmentWithOption(
1301+
scriptLet,
1302+
locationCalculator.getSubCalculatorShift(-14),
1303+
{ ...parserOptions, project: undefined },
1304+
{
1305+
preFixLocationProcess(preResult) {
1306+
const params = getParams(preResult)
1307+
if (params) {
1308+
for (const param of params) {
1309+
rawParams.push(
1310+
scriptLet.slice(param.range[0], param.range[1]),
1311+
)
1312+
}
1313+
}
1314+
},
1315+
},
1316+
)
1317+
const { ast } = result
1318+
const params = getParams(result)
12751319

12761320
if (!params || params.length === 0) {
12771321
return {
@@ -1300,12 +1344,7 @@ export function parseGenericExpression(
13001344
loc: { start: firstParam.loc.start, end: lastParam.loc.end },
13011345
parent: DUMMY_PARENT,
13021346
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-
),
1347+
rawParams,
13091348
}
13101349

13111350
// Modify parent.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.