diff --git a/crates/oxc_span/src/source_type/mod.rs b/crates/oxc_span/src/source_type/mod.rs index f58acb3a80b92..ee22fd1fa72a7 100644 --- a/crates/oxc_span/src/source_type/mod.rs +++ b/crates/oxc_span/src/source_type/mod.rs @@ -468,25 +468,35 @@ impl SourceType { ) })?; - let (language, module_kind) = match extension { - "js" | "mjs" | "jsx" => (Language::JavaScript, ModuleKind::Module), - "cjs" => (Language::JavaScript, ModuleKind::Script), - "ts" if file_name.ends_with(".d.ts") => { - (Language::TypeScriptDefinition, ModuleKind::Module) - } - "mts" if file_name.ends_with(".d.mts") => { - (Language::TypeScriptDefinition, ModuleKind::Module) + let module_kind = match extension { + "js" | "tsx" | "ts" | "jsx" | "mts" | "mjs" => ModuleKind::Module, + "cjs" | "cts" => ModuleKind::Script, + _ => unreachable!(), + }; + + let language = match extension { + // https://www.typescriptlang.org/tsconfig/#allowArbitraryExtensions + // `{file basename}.d.{extension}.ts` + // https://github.com/microsoft/TypeScript/issues/50133 + "ts" => { + if file_name[..file_name.len() - 3].split('.').rev().take(2).any(|c| c == "d") { + Language::TypeScriptDefinition + } else { + Language::TypeScript + } } - "cts" if file_name.ends_with(".d.cts") => { - (Language::TypeScriptDefinition, ModuleKind::Script) + "js" | "cjs" | "mjs" | "jsx" => Language::JavaScript, + "tsx" => Language::TypeScript, + #[allow(clippy::case_sensitive_file_extension_comparisons)] + "mts" | "cts" => { + if file_name[..file_name.len() - 4].ends_with(".d") { + Language::TypeScriptDefinition + } else { + Language::TypeScript + } } - "ts" | "mts" | "tsx" => (Language::TypeScript, ModuleKind::Module), - "cts" => (Language::TypeScript, ModuleKind::Script), _ => { - #[cfg(debug_assertions)] unreachable!(); - #[cfg(not(debug_assertions))] - return Err(UnknownExtension(format!("Unknown extension: {extension}").into())); } }; @@ -547,14 +557,12 @@ mod tests { #[test] #[allow(clippy::similar_names)] fn test_d_ts_from_path() { - let dts = SourceType::from_path("foo.d.ts") - .expect("foo.d.ts should be a valid TypeScript definition file path."); - let dmts = SourceType::from_path("foo.d.mts") - .expect("foo.d.mts should be a valid TypeScript definition file path."); - let dcts = SourceType::from_path("foo.d.cts") - .expect("foo.d.cts should be a valid TypeScript definition file path."); - - for ty in &[dts, dmts, dcts] { + let dts = SourceType::from_path("foo.d.ts").unwrap(); + let dmts = SourceType::from_path("foo.d.mts").unwrap(); + let dcts = SourceType::from_path("foo.d.cts").unwrap(); + let arbitrary = SourceType::from_path("foo.d.ext.ts").unwrap(); + + for ty in &[dts, dmts, dcts, arbitrary] { assert!(ty.is_typescript()); assert!(ty.is_typescript_definition()); assert!(!ty.is_javascript()); diff --git a/tasks/coverage/snapshots/parser_typescript.snap b/tasks/coverage/snapshots/parser_typescript.snap index b4a45f93a6b50..9cbe6b1320bce 100644 --- a/tasks/coverage/snapshots/parser_typescript.snap +++ b/tasks/coverage/snapshots/parser_typescript.snap @@ -2,7 +2,7 @@ commit: d85767ab parser_typescript Summary: AST Parsed : 6494/6503 (99.86%) -Positive Passed: 6481/6503 (99.66%) +Positive Passed: 6483/6503 (99.69%) Negative Passed: 1275/5747 (22.19%) Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ClassDeclaration24.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ExportAssignment7.ts @@ -4765,24 +4765,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/moduleResolut · ▲ ╰──── help: Try insert a semicolon here -Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/nonjsExtensions/declarationFileForHtmlFileWithinDeclarationFile.ts - - × Function implementation is missing or not immediately following the declaration. - ╭─[typescript/tests/cases/conformance/nonjsExtensions/declarationFileForHtmlFileWithinDeclarationFile.ts:11:5] - 10 │ export class HTML5Element extends HTMLElement { - 11 │ connectedCallback(): void; - · ───────────────── - 12 │ } - ╰──── -Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/nonjsExtensions/declarationFileForHtmlImport.ts - - × Function implementation is missing or not immediately following the declaration. - ╭─[typescript/tests/cases/conformance/nonjsExtensions/declarationFileForHtmlImport.ts:11:5] - 10 │ export class HTML5Element extends HTMLElement { - 11 │ connectedCallback(): void; - · ───────────────── - 12 │ } - ╰──── Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/plainJSRedeclare3.ts × Identifier `orbitol` has already been declared diff --git a/tasks/coverage/snapshots/semantic_typescript.snap b/tasks/coverage/snapshots/semantic_typescript.snap index d87e4bac54437..10f3a3f22ab56 100644 --- a/tasks/coverage/snapshots/semantic_typescript.snap +++ b/tasks/coverage/snapshots/semantic_typescript.snap @@ -2,7 +2,7 @@ commit: d85767ab semantic_typescript Summary: AST Parsed : 6503/6503 (100.00%) -Positive Passed: 2848/6503 (43.80%) +Positive Passed: 2852/6503 (43.86%) tasks/coverage/typescript/tests/cases/compiler/2dArrays.ts semantic error: Symbol reference IDs mismatch for "Cell": after transform: SymbolId(0): [ReferenceId(1)] @@ -8118,11 +8118,6 @@ semantic error: Scope children mismatch: after transform: ScopeId(0): [ScopeId(1), ScopeId(2)] rebuilt : ScopeId(0): [ScopeId(1)] -tasks/coverage/typescript/tests/cases/compiler/declarationEmitTransitiveImportOfHtmlDeclarationItem.ts -semantic error: Scope children mismatch: -after transform: ScopeId(0): [ScopeId(1)] -rebuilt : ScopeId(0): [] - tasks/coverage/typescript/tests/cases/compiler/declarationEmitTripleSlashReferenceAmbientModule.ts semantic error: Unresolved references mismatch: after transform: ["Url"] @@ -20383,15 +20378,12 @@ rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2)] tasks/coverage/typescript/tests/cases/compiler/moduleAugmentationsImports1.ts semantic error: Bindings mismatch: -after transform: ScopeId(0): ["./a", "A", "B", "Cls"] -rebuilt : ScopeId(0): ["A"] -Scope children mismatch: -after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3), ScopeId(6)] -rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2)] +after transform: ScopeId(0): ["A", "a", "b", "c"] +rebuilt : ScopeId(0): ["a", "b", "c"] tasks/coverage/typescript/tests/cases/compiler/moduleAugmentationsImports2.ts semantic error: Bindings mismatch: -after transform: ScopeId(0): ["./a", "A", "B"] +after transform: ScopeId(0): ["./a", "A", "Cls"] rebuilt : ScopeId(0): ["A"] Scope children mismatch: after transform: ScopeId(0): [ScopeId(1), ScopeId(2)] @@ -46349,30 +46341,18 @@ semantic error: Unresolved references mismatch: after transform: ["const"] rebuilt : [] -tasks/coverage/typescript/tests/cases/conformance/nonjsExtensions/declarationFileForHtmlFileWithinDeclarationFile.ts -semantic error: Function implementation is missing or not immediately following the declaration. - -tasks/coverage/typescript/tests/cases/conformance/nonjsExtensions/declarationFileForHtmlImport.ts -semantic error: Function implementation is missing or not immediately following the declaration. - -tasks/coverage/typescript/tests/cases/conformance/nonjsExtensions/declarationFileForJsonImport.ts -semantic error: Bindings mismatch: -after transform: ScopeId(0): ["val"] -rebuilt : ScopeId(0): [] -Reference symbol mismatch for "val": -after transform: SymbolId(0) "val" -rebuilt : -Reference flags mismatch for "val": -after transform: ReferenceId(0): ReferenceFlags(Read) -rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) +tasks/coverage/typescript/tests/cases/conformance/nonjsExtensions/declarationFilesForNodeNativeModules.ts +semantic error: Missing SymbolId: "mod" +Missing ReferenceId: "require" +Binding symbols mismatch: +after transform: ScopeId(0): [SymbolId(0)] +rebuilt : ScopeId(0): [SymbolId(0)] +Reference symbol mismatch for "mod": +after transform: SymbolId(0) "mod" +rebuilt : SymbolId(0) "mod" Unresolved references mismatch: after transform: [] -rebuilt : ["val"] - -tasks/coverage/typescript/tests/cases/conformance/nonjsExtensions/declarationFilesForNodeNativeModules.ts -semantic error: Scope children mismatch: -after transform: ScopeId(0): [ScopeId(1)] -rebuilt : ScopeId(0): [] +rebuilt : ["require"] tasks/coverage/typescript/tests/cases/conformance/override/override10.ts semantic error: Scope children mismatch: