Skip to content

Commit 39540f7

Browse files
committed
fix(parser): adjust class start position when decorators are involved
closes #10409
1 parent bb8a078 commit 39540f7

File tree

3 files changed

+13
-47
lines changed

3 files changed

+13
-47
lines changed

crates/oxc_linter/src/snapshots/unicorn_no_anonymous_default_export.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,9 @@ source: crates/oxc_linter/src/tester.rs
270270
╰────
271271

272272
eslint-plugin-unicorn(no-anonymous-default-export): This class default export is missing a name
273-
╭─[no_anonymous_default_export.tsx:1:1]
273+
╭─[no_anonymous_default_export.tsx:1:27]
274274
1 │ @decorator export default class {}
275-
· ──────────────────────────────────
275+
· ────────
276276
╰────
277277

278278
eslint-plugin-unicorn(no-anonymous-default-export): This class default export is missing a name
@@ -288,7 +288,7 @@ source: crates/oxc_linter/src/tester.rs
288288
╰────
289289

290290
eslint-plugin-unicorn(no-anonymous-default-export): This class default export is missing a name
291-
╭─[no_anonymous_default_export.tsx:1:1]
291+
╭─[no_anonymous_default_export.tsx:1:48]
292292
1 │ @decorator @decorator(class {}) export default class {}
293-
· ───────────────────────────────────────────────────────
293+
· ────────
294294
╰────

crates/oxc_parser/src/js/class.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ impl<'a> ParserImpl<'a> {
6464
self.bump_any(); // advance `class`
6565

6666
let decorators = self.consume_decorators();
67-
let start_span = decorators.iter().next().map_or(start_span, |d| d.span.start);
67+
68+
// Move span start to decorator position if this is a class expression.
69+
let mut start_span = start_span;
70+
if r#type == ClassType::ClassExpression {
71+
if let Some(d) = decorators.first() {
72+
start_span = d.span.start;
73+
}
74+
}
6875

6976
let id = if self.cur_kind().is_binding_identifier() && !self.at(Kind::Implements) {
7077
Some(self.parse_binding_identifier()?)

tasks/coverage/snapshots/estree_typescript.snap

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ commit: 15392346
22

33
estree_typescript Summary:
44
AST Parsed : 10619/10725 (99.01%)
5-
Positive Passed: 8971/10725 (83.65%)
5+
Positive Passed: 9012/10725 (84.03%)
66
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/ClassDeclarationWithInvalidConstOnPropertyDeclaration.ts
77
A class member cannot have the 'const' keyword.
88
Mismatch: tasks/coverage/typescript/tests/cases/compiler/accessOverriddenBaseClassMember1.ts
@@ -242,7 +242,6 @@ declare' modifier already seen.
242242
Mismatch: tasks/coverage/typescript/tests/cases/compiler/declareDottedExtend.ts
243243
Mismatch: tasks/coverage/typescript/tests/cases/compiler/declareDottedModuleName.ts
244244
Mismatch: tasks/coverage/typescript/tests/cases/compiler/declareModifierOnTypeAlias.ts
245-
Mismatch: tasks/coverage/typescript/tests/cases/compiler/decoratorMetadataRestParameterWithImportedType.ts
246245
tasks/coverage/typescript/tests/cases/compiler/defaultPropsEmptyCurlyBecomesAnyForJs.ts
247246
Unexpected estree file content error: 2 != 3
248247
@@ -294,7 +293,6 @@ Mismatch: tasks/coverage/typescript/tests/cases/compiler/emitBOM.ts
294293
Mismatch: tasks/coverage/typescript/tests/cases/compiler/emitBundleWithPrologueDirectives1.ts
295294
Mismatch: tasks/coverage/typescript/tests/cases/compiler/emitCapturingThisInTupleDestructuring1.ts
296295
Mismatch: tasks/coverage/typescript/tests/cases/compiler/emitCapturingThisInTupleDestructuring2.ts
297-
Mismatch: tasks/coverage/typescript/tests/cases/compiler/emitHelpersWithLocalCollisions.ts
298296
Mismatch: tasks/coverage/typescript/tests/cases/compiler/emitMemberAccessExpression.ts
299297
Mismatch: tasks/coverage/typescript/tests/cases/compiler/emitPinnedCommentsOnTopOfFile.ts
300298
Mismatch: tasks/coverage/typescript/tests/cases/compiler/emitSuperCallBeforeEmitParameterPropertyDeclaration1.ts
@@ -429,11 +427,9 @@ Mismatch: tasks/coverage/typescript/tests/cases/compiler/importExportInternalCom
429427
tasks/coverage/typescript/tests/cases/compiler/importHelpersCommonJSJavaScript.ts
430428
Unexpected estree file content error: 2 != 3
431429
432-
Mismatch: tasks/coverage/typescript/tests/cases/compiler/importHelpersES6.ts
433430
tasks/coverage/typescript/tests/cases/compiler/importHelpersVerbatimModuleSyntax.ts
434431
Unexpected estree file content error: 2 != 3
435432
436-
Mismatch: tasks/coverage/typescript/tests/cases/compiler/importHelpersWithLocalCollisions.ts
437433
Mismatch: tasks/coverage/typescript/tests/cases/compiler/importInTypePosition.ts
438434
tasks/coverage/typescript/tests/cases/compiler/importNonExportedMember10.ts
439435
Unexpected estree file content error: 1 != 2
@@ -1129,12 +1125,6 @@ Mismatch: tasks/coverage/typescript/tests/cases/conformance/decorators/class/con
11291125
Mismatch: tasks/coverage/typescript/tests/cases/conformance/decorators/class/constructor/decoratorOnClassConstructor3.ts
11301126
Mismatch: tasks/coverage/typescript/tests/cases/conformance/decorators/class/constructor/decoratorOnClassConstructor4.ts
11311127
Mismatch: tasks/coverage/typescript/tests/cases/conformance/decorators/class/constructor/parameter/decoratorOnClassConstructorParameter5.ts
1132-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/decorators/class/decoratedClassExportsCommonJS1.ts
1133-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/decorators/class/decoratedClassExportsCommonJS2.ts
1134-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/decorators/class/decoratedClassExportsSystem1.ts
1135-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/decorators/class/decoratedClassExportsSystem2.ts
1136-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/decorators/class/decoratedClassFromExternalModule.ts
1137-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/decorators/class/decoratorOnClass2.ts
11381128
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/decorators/decoratorMetadata-jsdoc.ts
11391129
Unexpected token
11401130
Mismatch: tasks/coverage/typescript/tests/cases/conformance/decorators/decoratorMetadata.ts
@@ -1266,12 +1256,6 @@ Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/classDeclaration
12661256
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing6.ts
12671257
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing7.ts
12681258
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing8.ts
1269-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/decorators/class/decoratorOnClass2.es6.ts
1270-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/decorators/class/decoratorOnClass3.es6.ts
1271-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/decorators/class/decoratorOnClass4.es6.ts
1272-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/decorators/class/decoratorOnClass6.es6.ts
1273-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/decorators/class/decoratorOnClass7.es6.ts
1274-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/decorators/class/decoratorOnClass8.es6.ts
12751259
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/defaultParameters/emitDefaultParametersFunction.ts
12761260
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/defaultParameters/emitDefaultParametersFunctionES6.ts
12771261
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/defaultParameters/emitDefaultParametersFunctionExpression.ts
@@ -1400,10 +1384,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/es6/functionD
14001384
Cannot use `yield` as an identifier in a generator context
14011385
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration5_es6.ts
14021386
Cannot use `yield` as an identifier in a generator context
1403-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/moduleExportsAmd/decoratedDefaultExportsGetExportedAmd.ts
1404-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/moduleExportsCommonjs/decoratedDefaultExportsGetExportedCommonjs.ts
1405-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/moduleExportsSystem/decoratedDefaultExportsGetExportedSystem.ts
1406-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/moduleExportsUmd/decoratedDefaultExportsGetExportedUmd.ts
14071387
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/modules/exportsAndImportsWithContextualKeywordNames01.ts
14081388
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/modules/exportsAndImportsWithContextualKeywordNames02.ts
14091389
Mismatch: tasks/coverage/typescript/tests/cases/conformance/es6/restParameters/emitRestParametersFunction.ts
@@ -1664,10 +1644,7 @@ Unexpected trailing comma after rest element
16641644
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts
16651645
A rest parameter must be last in a parameter list
16661646
Mismatch: tasks/coverage/typescript/tests/cases/conformance/esDecorators/classDeclaration/esDecorators-classDeclaration-commentPreservation.ts
1667-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/esDecorators/classDeclaration/esDecorators-classDeclaration-commonjs-classNamespaceMerge.ts
1668-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/esDecorators/classDeclaration/esDecorators-classDeclaration-commonjs.ts
16691647
Mismatch: tasks/coverage/typescript/tests/cases/conformance/esDecorators/classDeclaration/esDecorators-classDeclaration-parameterProperties.ts
1670-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/esDecorators/classDeclaration/esDecorators-classDeclaration-setFunctionName.ts
16711648
Mismatch: tasks/coverage/typescript/tests/cases/conformance/esDecorators/classExpression/esDecorators-classExpression-missingEmitHelpers-classDecorator.17.ts
16721649
Mismatch: tasks/coverage/typescript/tests/cases/conformance/esDecorators/classExpression/esDecorators-classExpression-missingEmitHelpers-classDecorator.5.ts
16731650
Mismatch: tasks/coverage/typescript/tests/cases/conformance/esDecorators/classExpression/esDecorators-classExpression-missingEmitHelpers-classDecorator.7.ts
@@ -1718,12 +1695,6 @@ Expected a semicolon or an implicit semicolon after a statement, but found none
17181695
tasks/coverage/typescript/tests/cases/conformance/externalModules/commonJsImportBindingElementNarrowType.ts
17191696
Unexpected estree file content error: 1 != 2
17201697

1721-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/externalModules/es6/es6modulekindWithES5Target.ts
1722-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/externalModules/es6/es6modulekindWithES5Target11.ts
1723-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/externalModules/es6/es6modulekindWithES5Target3.ts
1724-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/externalModules/esnext/esnextmodulekindWithES5Target.ts
1725-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/externalModules/esnext/esnextmodulekindWithES5Target11.ts
1726-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/externalModules/esnext/esnextmodulekindWithES5Target3.ts
17271698
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/externalModules/exportNonInitializedVariablesInIfThenStatementNoCrash1.ts
17281699
Missing initializer in const declaration
17291700
Mismatch: tasks/coverage/typescript/tests/cases/conformance/externalModules/globalAugmentationModuleResolution.ts
@@ -2443,18 +2414,6 @@ Mismatch: tasks/coverage/typescript/tests/cases/conformance/scanner/ecmascript5/
24432414
Mismatch: tasks/coverage/typescript/tests/cases/conformance/scanner/ecmascript5/scannerS7.2_A1.5_T2.ts
24442415
Mismatch: tasks/coverage/typescript/tests/cases/conformance/scanner/ecmascript5/scannerS7.6_A4.2_T1.ts
24452416
Mismatch: tasks/coverage/typescript/tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarations.11.ts
2446-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarationsWithESClassDecorators.10.ts
2447-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarationsWithESClassDecorators.2.ts
2448-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarationsWithESClassDecorators.3.ts
2449-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarationsWithESClassDecorators.4.ts
2450-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarationsWithESClassDecorators.8.ts
2451-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarationsWithESClassDecorators.9.ts
2452-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarationsWithLegacyClassDecorators.10.ts
2453-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarationsWithLegacyClassDecorators.2.ts
2454-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarationsWithLegacyClassDecorators.3.ts
2455-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarationsWithLegacyClassDecorators.4.ts
2456-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarationsWithLegacyClassDecorators.8.ts
2457-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarationsWithLegacyClassDecorators.9.ts
24582417
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/for-ofStatements/ES5For-of12.ts
24592418
Unexpected token
24602419
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/for-ofStatements/ES5For-of20.ts

0 commit comments

Comments
 (0)