Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitf3ac447

Browse files
authored
Fix an error when using CRLF for generic directive (#220)
1 parentd4eef64 commitf3ac447

File tree

17 files changed

+9441
-18
lines changed

17 files changed

+9441
-18
lines changed

‎src/script/generic.ts

Lines 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-
conststart=node.range[0]
128-
returnrawParam.slice(
129-
node.constraint.range[0]-start,
130-
node.constraint.range[1]-start,
131-
)
127+
letstartIndex=rawParam.indexOf(node.name.name)+node.name.name.length
128+
while(startIndex<rawParam.length){
129+
if(rawParam.startsWith("extends",startIndex)){
130+
returnrawParam.slice(startIndex+7)
131+
}
132+
if(rawParam.startsWith("//",startIndex)){
133+
constlfIndex=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+
constendIndex=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

Lines 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+
returnparseScriptFragmentWithOption(
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+
functionparseScriptFragmentWithOption(
233+
code:string,
234+
locationCalculator:LocationCalculator,
235+
parserOptions:ParserOptions,
236+
processOptions?:{
237+
preFixLocationProcess?:(result:ESLintExtendedProgram)=>void
238+
},
215239
):ESLintExtendedProgram{
216240
try{
217241
constresult=parseScript(code,parserOptions)
242+
processOptions?.preFixLocationProcess?.(result)
218243
fixLocations(result,locationCalculator)
219244
returnresult
220245
}catch(err){
@@ -1259,19 +1284,38 @@ export function parseGenericExpression(
12591284
throwEmptyError(locationCalculator,"a type parameter")
12601285
}
12611286

1262-
try{
1263-
constresult=parseScriptFragment(
1264-
`void function<${code}>(){}`,
1265-
locationCalculator.getSubCalculatorShift(-14),
1266-
{ ...parserOptions,project:undefined},
1267-
)
1287+
functiongetParams(result:ESLintExtendedProgram){
12681288
const{ ast}=result
12691289
conststatement=ast.body[0]asESLintExpressionStatement
12701290
constrawExpression=statement.expressionasESLintUnaryExpression
12711291
constclassDecl=rawExpression.argumentasESLintClassExpression
12721292
consttypeParameters=(classDeclasTSESTree.ClassExpression)
12731293
.typeParameters
1274-
constparams=typeParameters?.params
1294+
returntypeParameters?.params
1295+
}
1296+
1297+
try{
1298+
constrawParams:string[]=[]
1299+
constscriptLet=`void function<${code}>(){}`
1300+
constresult=parseScriptFragmentWithOption(
1301+
scriptLet,
1302+
locationCalculator.getSubCalculatorShift(-14),
1303+
{ ...parserOptions,project:undefined},
1304+
{
1305+
preFixLocationProcess(preResult){
1306+
constparams=getParams(preResult)
1307+
if(params){
1308+
for(constparamofparams){
1309+
rawParams.push(
1310+
scriptLet.slice(param.range[0],param.range[1]),
1311+
)
1312+
}
1313+
}
1314+
},
1315+
},
1316+
)
1317+
const{ ast}=result
1318+
constparams=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)

[8]ページ先頭

©2009-2025 Movatter.jp