Skip to content

Commit

Permalink
refactor(span): change shape of Language (#2680)
Browse files Browse the repository at this point in the history
Closes #2677.

Also changed the JSON output to be `javascript`, `typescript` or
`typescriptDefinition`. The current value `javaScript` / `typeScript` is
a bit weird.
  • Loading branch information
overlookmotel authored Mar 11, 2024
1 parent b378e7e commit 75ae563
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions crates/oxc_span/src/source_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ pub struct SourceType {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, SerAttrs)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[serde(rename_all = "camelCase")]
#[serde(rename_all = "lowercase")]
pub enum Language {
JavaScript,
#[serde(rename_all = "camelCase")]
TypeScript {
is_definition_file: bool,
},
TypeScript,
#[serde(rename = "typescriptDefinition")]
TypeScriptDefinition,
}

/// Script or Module
Expand Down Expand Up @@ -91,15 +90,15 @@ impl SourceType {
}

pub fn is_javascript(self) -> bool {
matches!(self.language, Language::JavaScript)
self.language == Language::JavaScript
}

pub fn is_typescript(self) -> bool {
matches!(self.language, Language::TypeScript { .. })
matches!(self.language, Language::TypeScript | Language::TypeScriptDefinition)
}

pub fn is_typescript_definition(self) -> bool {
matches!(self.language, Language::TypeScript { is_definition_file: true })
self.language == Language::TypeScriptDefinition
}

pub fn is_jsx(self) -> bool {
Expand Down Expand Up @@ -131,15 +130,15 @@ impl SourceType {
#[must_use]
pub fn with_typescript(mut self, yes: bool) -> Self {
if yes {
self.language = Language::TypeScript { is_definition_file: false };
self.language = Language::TypeScript;
}
self
}

#[must_use]
pub fn with_typescript_definition(mut self, yes: bool) -> Self {
if yes {
self.language = Language::TypeScript { is_definition_file: true };
self.language = Language::TypeScriptDefinition;
}
self
}
Expand Down Expand Up @@ -182,14 +181,15 @@ impl SourceType {
)
})?;

let is_definition_file = file_name.ends_with(".d.ts")
|| file_name.ends_with(".d.mts")
|| file_name.ends_with(".d.cts");

let language = match extension {
"js" | "mjs" | "cjs" | "jsx" => Language::JavaScript,
"ts" | "mts" | "cts" | "tsx" => Language::TypeScript { is_definition_file },
_ => unreachable!(),
"ts" if file_name.ends_with(".d.ts") => Language::TypeScriptDefinition,
"mts" if file_name.ends_with(".d.mts") => Language::TypeScriptDefinition,
"cts" if file_name.ends_with(".d.cts") => Language::TypeScriptDefinition,
_ => {
debug_assert!(matches!(extension, "ts" | "mts" | "cts" | "tsx"));
Language::TypeScript
}
};

let variant = match extension {
Expand Down

0 comments on commit 75ae563

Please sign in to comment.