Skip to content

Commit

Permalink
Fix an error when using CRLF for generic directive (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Jan 22, 2024
1 parent d4eef64 commit f3ac447
Show file tree
Hide file tree
Showing 17 changed files with 9,441 additions and 18 deletions.
30 changes: 25 additions & 5 deletions src/script/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,31 @@ function getConstraint(node: TSESTree.TSTypeParameter, rawParam: string) {
if (!node.constraint) {
return "unknown"
}
const start = node.range[0]
return rawParam.slice(
node.constraint.range[0] - start,
node.constraint.range[1] - start,
)
let startIndex = rawParam.indexOf(node.name.name) + node.name.name.length
while (startIndex < rawParam.length) {
if (rawParam.startsWith("extends", startIndex)) {
return rawParam.slice(startIndex + 7)
}
if (rawParam.startsWith("//", startIndex)) {
const lfIndex = rawParam.indexOf("\n", startIndex)
if (lfIndex >= 0) {
startIndex = lfIndex + 1
continue
}
return "unknown"
}
if (rawParam.startsWith("/*", startIndex)) {
const endIndex = rawParam.indexOf("*/", startIndex)
if (endIndex >= 0) {
startIndex = endIndex + 2
continue
}
return "unknown"
}
startIndex++
}

return "unknown"
}

/** Remove variable def */
Expand Down
65 changes: 52 additions & 13 deletions src/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,34 @@ export function parseScriptFragment(
code: string,
locationCalculator: LocationCalculator,
parserOptions: ParserOptions,
): ESLintExtendedProgram {
return parseScriptFragmentWithOption(
code,
locationCalculator,
parserOptions,
)
}

/**
* Parse the given source code.
*
* @param code The source code to parse.
* @param locationCalculator The location calculator for fixLocations.
* @param parserOptions The parser options.
* @param processOptions The process options.
* @returns The result of parsing.
*/
function parseScriptFragmentWithOption(
code: string,
locationCalculator: LocationCalculator,
parserOptions: ParserOptions,
processOptions?: {
preFixLocationProcess?: (result: ESLintExtendedProgram) => void
},
): ESLintExtendedProgram {
try {
const result = parseScript(code, parserOptions)
processOptions?.preFixLocationProcess?.(result)
fixLocations(result, locationCalculator)
return result
} catch (err) {
Expand Down Expand Up @@ -1259,19 +1284,38 @@ export function parseGenericExpression(
throwEmptyError(locationCalculator, "a type parameter")
}

try {
const result = parseScriptFragment(
`void function<${code}>(){}`,
locationCalculator.getSubCalculatorShift(-14),
{ ...parserOptions, project: undefined },
)
function getParams(result: ESLintExtendedProgram) {
const { ast } = result
const statement = ast.body[0] as ESLintExpressionStatement
const rawExpression = statement.expression as ESLintUnaryExpression
const classDecl = rawExpression.argument as ESLintClassExpression
const typeParameters = (classDecl as TSESTree.ClassExpression)
.typeParameters
const params = typeParameters?.params
return typeParameters?.params
}

try {
const rawParams: string[] = []
const scriptLet = `void function<${code}>(){}`
const result = parseScriptFragmentWithOption(
scriptLet,
locationCalculator.getSubCalculatorShift(-14),
{ ...parserOptions, project: undefined },
{
preFixLocationProcess(preResult) {
const params = getParams(preResult)
if (params) {
for (const param of params) {
rawParams.push(
scriptLet.slice(param.range[0], param.range[1]),
)
}
}
},
},
)
const { ast } = result
const params = getParams(result)

if (!params || params.length === 0) {
return {
Expand Down Expand Up @@ -1300,12 +1344,7 @@ export function parseGenericExpression(
loc: { start: firstParam.loc.start, end: lastParam.loc.end },
parent: DUMMY_PARENT,
params,
rawParams: params.map((param) =>
code.slice(
param.range[0] - typeParameters.range[0] - 1,
param.range[1] - typeParameters.range[0] - 1,
),
),
rawParams,
}

// Modify parent.
Expand Down

0 comments on commit f3ac447

Please sign in to comment.