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

Fix an error when using CRLF for generic directive #220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions 30 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 65 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
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.