mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(span): implement source type {file basename}.d.{extension}.ts (#8109)
This commit is contained in:
parent
708e9cfac0
commit
11c4bd8687
3 changed files with 45 additions and 75 deletions
|
|
@ -468,25 +468,35 @@ impl SourceType {
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let (language, module_kind) = match extension {
|
let module_kind = match extension {
|
||||||
"js" | "mjs" | "jsx" => (Language::JavaScript, ModuleKind::Module),
|
"js" | "tsx" | "ts" | "jsx" | "mts" | "mjs" => ModuleKind::Module,
|
||||||
"cjs" => (Language::JavaScript, ModuleKind::Script),
|
"cjs" | "cts" => ModuleKind::Script,
|
||||||
"ts" if file_name.ends_with(".d.ts") => {
|
_ => unreachable!(),
|
||||||
(Language::TypeScriptDefinition, ModuleKind::Module)
|
};
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
"mts" if file_name.ends_with(".d.mts") => {
|
|
||||||
(Language::TypeScriptDefinition, ModuleKind::Module)
|
|
||||||
}
|
}
|
||||||
"cts" if file_name.ends_with(".d.cts") => {
|
"js" | "cjs" | "mjs" | "jsx" => Language::JavaScript,
|
||||||
(Language::TypeScriptDefinition, ModuleKind::Script)
|
"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!();
|
unreachable!();
|
||||||
#[cfg(not(debug_assertions))]
|
|
||||||
return Err(UnknownExtension(format!("Unknown extension: {extension}").into()));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -547,14 +557,12 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
#[allow(clippy::similar_names)]
|
#[allow(clippy::similar_names)]
|
||||||
fn test_d_ts_from_path() {
|
fn test_d_ts_from_path() {
|
||||||
let dts = SourceType::from_path("foo.d.ts")
|
let dts = SourceType::from_path("foo.d.ts").unwrap();
|
||||||
.expect("foo.d.ts should be a valid TypeScript definition file path.");
|
let dmts = SourceType::from_path("foo.d.mts").unwrap();
|
||||||
let dmts = SourceType::from_path("foo.d.mts")
|
let dcts = SourceType::from_path("foo.d.cts").unwrap();
|
||||||
.expect("foo.d.mts should be a valid TypeScript definition file path.");
|
let arbitrary = SourceType::from_path("foo.d.ext.ts").unwrap();
|
||||||
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] {
|
for ty in &[dts, dmts, dcts, arbitrary] {
|
||||||
assert!(ty.is_typescript());
|
assert!(ty.is_typescript());
|
||||||
assert!(ty.is_typescript_definition());
|
assert!(ty.is_typescript_definition());
|
||||||
assert!(!ty.is_javascript());
|
assert!(!ty.is_javascript());
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ commit: d85767ab
|
||||||
|
|
||||||
parser_typescript Summary:
|
parser_typescript Summary:
|
||||||
AST Parsed : 6494/6503 (99.86%)
|
AST Parsed : 6494/6503 (99.86%)
|
||||||
Positive Passed: 6481/6503 (99.66%)
|
Positive Passed: 6483/6503 (99.69%)
|
||||||
Negative Passed: 1275/5747 (22.19%)
|
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/ClassDeclaration24.ts
|
||||||
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ExportAssignment7.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
|
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
|
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/plainJSRedeclare3.ts
|
||||||
|
|
||||||
× Identifier `orbitol` has already been declared
|
× Identifier `orbitol` has already been declared
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ commit: d85767ab
|
||||||
|
|
||||||
semantic_typescript Summary:
|
semantic_typescript Summary:
|
||||||
AST Parsed : 6503/6503 (100.00%)
|
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
|
tasks/coverage/typescript/tests/cases/compiler/2dArrays.ts
|
||||||
semantic error: Symbol reference IDs mismatch for "Cell":
|
semantic error: Symbol reference IDs mismatch for "Cell":
|
||||||
after transform: SymbolId(0): [ReferenceId(1)]
|
after transform: SymbolId(0): [ReferenceId(1)]
|
||||||
|
|
@ -8118,11 +8118,6 @@ semantic error: Scope children mismatch:
|
||||||
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
|
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
|
||||||
rebuilt : ScopeId(0): [ScopeId(1)]
|
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
|
tasks/coverage/typescript/tests/cases/compiler/declarationEmitTripleSlashReferenceAmbientModule.ts
|
||||||
semantic error: Unresolved references mismatch:
|
semantic error: Unresolved references mismatch:
|
||||||
after transform: ["Url"]
|
after transform: ["Url"]
|
||||||
|
|
@ -20383,15 +20378,12 @@ rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2)]
|
||||||
|
|
||||||
tasks/coverage/typescript/tests/cases/compiler/moduleAugmentationsImports1.ts
|
tasks/coverage/typescript/tests/cases/compiler/moduleAugmentationsImports1.ts
|
||||||
semantic error: Bindings mismatch:
|
semantic error: Bindings mismatch:
|
||||||
after transform: ScopeId(0): ["./a", "A", "B", "Cls"]
|
after transform: ScopeId(0): ["A", "a", "b", "c"]
|
||||||
rebuilt : ScopeId(0): ["A"]
|
rebuilt : ScopeId(0): ["a", "b", "c"]
|
||||||
Scope children mismatch:
|
|
||||||
after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3), ScopeId(6)]
|
|
||||||
rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2)]
|
|
||||||
|
|
||||||
tasks/coverage/typescript/tests/cases/compiler/moduleAugmentationsImports2.ts
|
tasks/coverage/typescript/tests/cases/compiler/moduleAugmentationsImports2.ts
|
||||||
semantic error: Bindings mismatch:
|
semantic error: Bindings mismatch:
|
||||||
after transform: ScopeId(0): ["./a", "A", "B"]
|
after transform: ScopeId(0): ["./a", "A", "Cls"]
|
||||||
rebuilt : ScopeId(0): ["A"]
|
rebuilt : ScopeId(0): ["A"]
|
||||||
Scope children mismatch:
|
Scope children mismatch:
|
||||||
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
|
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
|
||||||
|
|
@ -46349,30 +46341,18 @@ semantic error: Unresolved references mismatch:
|
||||||
after transform: ["const"]
|
after transform: ["const"]
|
||||||
rebuilt : []
|
rebuilt : []
|
||||||
|
|
||||||
tasks/coverage/typescript/tests/cases/conformance/nonjsExtensions/declarationFileForHtmlFileWithinDeclarationFile.ts
|
tasks/coverage/typescript/tests/cases/conformance/nonjsExtensions/declarationFilesForNodeNativeModules.ts
|
||||||
semantic error: Function implementation is missing or not immediately following the declaration.
|
semantic error: Missing SymbolId: "mod"
|
||||||
|
Missing ReferenceId: "require"
|
||||||
tasks/coverage/typescript/tests/cases/conformance/nonjsExtensions/declarationFileForHtmlImport.ts
|
Binding symbols mismatch:
|
||||||
semantic error: Function implementation is missing or not immediately following the declaration.
|
after transform: ScopeId(0): [SymbolId(0)]
|
||||||
|
rebuilt : ScopeId(0): [SymbolId(0)]
|
||||||
tasks/coverage/typescript/tests/cases/conformance/nonjsExtensions/declarationFileForJsonImport.ts
|
Reference symbol mismatch for "mod":
|
||||||
semantic error: Bindings mismatch:
|
after transform: SymbolId(0) "mod"
|
||||||
after transform: ScopeId(0): ["val"]
|
rebuilt : SymbolId(0) "mod"
|
||||||
rebuilt : ScopeId(0): []
|
|
||||||
Reference symbol mismatch for "val":
|
|
||||||
after transform: SymbolId(0) "val"
|
|
||||||
rebuilt : <None>
|
|
||||||
Reference flags mismatch for "val":
|
|
||||||
after transform: ReferenceId(0): ReferenceFlags(Read)
|
|
||||||
rebuilt : ReferenceId(0): ReferenceFlags(Read | Type)
|
|
||||||
Unresolved references mismatch:
|
Unresolved references mismatch:
|
||||||
after transform: []
|
after transform: []
|
||||||
rebuilt : ["val"]
|
rebuilt : ["require"]
|
||||||
|
|
||||||
tasks/coverage/typescript/tests/cases/conformance/nonjsExtensions/declarationFilesForNodeNativeModules.ts
|
|
||||||
semantic error: Scope children mismatch:
|
|
||||||
after transform: ScopeId(0): [ScopeId(1)]
|
|
||||||
rebuilt : ScopeId(0): []
|
|
||||||
|
|
||||||
tasks/coverage/typescript/tests/cases/conformance/override/override10.ts
|
tasks/coverage/typescript/tests/cases/conformance/override/override10.ts
|
||||||
semantic error: Scope children mismatch:
|
semantic error: Scope children mismatch:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue