Skip to content

Commit

Permalink
feat(span): implement source type {file basename}.d.{extension}.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 25, 2024
1 parent a037a81 commit ddf5e69
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 76 deletions.
53 changes: 30 additions & 23 deletions crates/oxc_span/src/source_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,25 +468,34 @@ 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,
"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()));
}
};

Expand Down Expand Up @@ -547,14 +556,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 arbitary = SourceType::from_path("foo.d.ext.ts").unwrap();

Check warning on line 562 in crates/oxc_span/src/source_type/mod.rs

View workflow job for this annotation

GitHub Actions / Spell Check

"arbitary" should be "arbitrary".

for ty in &[dts, dmts, dcts, arbitary] {

Check warning on line 564 in crates/oxc_span/src/source_type/mod.rs

View workflow job for this annotation

GitHub Actions / Spell Check

"arbitary" should be "arbitrary".
assert!(ty.is_typescript());
assert!(ty.is_typescript_definition());
assert!(!ty.is_javascript());
Expand Down
20 changes: 1 addition & 19 deletions tasks/coverage/snapshots/parser_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
48 changes: 14 additions & 34 deletions tasks/coverage/snapshots/semantic_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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 : <None>
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:
Expand Down

0 comments on commit ddf5e69

Please sign in to comment.