pub struct SourceType { /* private fields */ }Expand description
Source Type for JavaScript vs TypeScript / Script vs Module / JSX
Implementations§
Source§impl SourceType
impl SourceType
Sourcepub const fn cjs() -> SourceType
pub const fn cjs() -> SourceType
Creates a SourceType representing a regular JavaScript file.
This file could be a vanilla script (no module system of any kind) or a CommonJS file.
The resulting source type is not a module, nor does it support JSX.
Use SourceType::jsx for JSX sources.
§Example
let js = SourceType::cjs();
assert!(js.is_javascript());
assert!(js.is_script()); // not a module
assert!(!js.is_jsx());Sourcepub const fn mjs() -> SourceType
pub const fn mjs() -> SourceType
Creates a SourceType representing a JavaScript file using ES6
modules. This is akin to a file with an .mjs extension.
§Example
let mjs = SourceType::mjs();Sourcepub const fn unambiguous() -> SourceType
pub const fn unambiguous() -> SourceType
A SourceType that will be treated as a module if it contains ESM syntax.
After a file is parsed with an unambiguous source type, it will have a final
ModuleKind of either Module or Script.
Sourcepub const fn jsx() -> SourceType
pub const fn jsx() -> SourceType
Creates a SourceType representing a JavaScript` file with JSX.
§Example
let jsx = SourceType::jsx();
assert!(jsx.is_javascript());
assert!(jsx.is_jsx());Sourcepub const fn ts() -> SourceType
pub const fn ts() -> SourceType
Creates a SourceType representing a TypeScript file.
Unlike SourceType::cjs, this method creates modules. Use
SourceType::tsx for TypeScript files with JSX support.
§Example
let ts = SourceType::ts();
assert!(ts.is_typescript());
assert!(!ts.is_typescript_definition());
assert!(ts.is_module());
assert!(!ts.is_jsx());Sourcepub const fn tsx() -> SourceType
pub const fn tsx() -> SourceType
Creates a SourceType representing a TypeScript file with JSX.
§Example
let tsx = SourceType::tsx();
assert!(tsx.is_typescript());
assert!(!tsx.is_typescript_definition());
assert!(tsx.is_module());
assert!(tsx.is_jsx());Sourcepub const fn d_ts() -> SourceType
pub const fn d_ts() -> SourceType
Creates a SourceType representing a [TypeScript definition] file.
§Example
let dts = SourceType::d_ts();
assert!(dts.is_typescript());
assert!(dts.is_typescript_definition());
assert!(dts.is_module());
assert!(!dts.is_jsx());Sourcepub fn is_script(self) -> bool
pub fn is_script(self) -> bool
Returns true if this SourceType is script.
Sourcepub fn is_module(self) -> bool
pub fn is_module(self) -> bool
Returns true if this SourceType is module.
Sourcepub fn is_unambiguous(self) -> bool
pub fn is_unambiguous(self) -> bool
Returns true if this SourceType is unambiguous.
Sourcepub fn module_kind(self) -> ModuleKind
pub fn module_kind(self) -> ModuleKind
What module system is this source type using?
Sourcepub fn is_javascript(self) -> bool
pub fn is_javascript(self) -> bool
Returns true if this is a JavaScript file with or without syntax
extensions (like JSX).
Sourcepub fn is_typescript(self) -> bool
pub fn is_typescript(self) -> bool
Returns true if this is a TypeScript file or TypeScript definition file.
I.e., true for .ts, .cts, .mts, .tsx, and .d.ts files.
Sourcepub fn is_typescript_definition(self) -> bool
pub fn is_typescript_definition(self) -> bool
Returns true if this is a TypeScript definition file (e.g. .d.ts).
Sourcepub fn is_jsx(self) -> bool
pub fn is_jsx(self) -> bool
Returns true if this source type is using JSX.
Note that TSX is considered JSX in this context.
Sourcepub fn is_strict(self) -> bool
pub fn is_strict(self) -> bool
Does this source type implicitly use strict mode semantics?
Does not consider "use strict"; directives.
Sourcepub const fn with_script(self, yes: bool) -> SourceType
pub const fn with_script(self, yes: bool) -> SourceType
Mark this SourceType as a script if yes is true. No change
will occur if yes is false.
Sourcepub const fn with_module(self, yes: bool) -> SourceType
pub const fn with_module(self, yes: bool) -> SourceType
Mark this SourceType as a module if yes is true. No change
will occur if yes is false.
Sourcepub const fn with_unambiguous(self, yes: bool) -> SourceType
pub const fn with_unambiguous(self, yes: bool) -> SourceType
Mark this SourceType as unambiguous if yes is true. No change
will occur if yes is false.
Sourcepub const fn with_javascript(self, yes: bool) -> SourceType
pub const fn with_javascript(self, yes: bool) -> SourceType
Mark this SourceType as using JavaScript if yes is true. No change
will occur if yes is false.
Sourcepub const fn with_typescript(self, yes: bool) -> SourceType
pub const fn with_typescript(self, yes: bool) -> SourceType
Mark this SourceType as using TypeScript if yes is true. No change
will occur if yes is false.
Sourcepub const fn with_typescript_definition(self, yes: bool) -> SourceType
pub const fn with_typescript_definition(self, yes: bool) -> SourceType
Mark this SourceType as a [TypeScript definition] file if yes is true.
Sourcepub const fn with_jsx(self, yes: bool) -> SourceType
pub const fn with_jsx(self, yes: bool) -> SourceType
Mark this SourceType as using JSX if yes is true. No change
will occur if yes is false.
When using TypeScript, this source type now represents a TSX file.
Sourcepub const fn with_standard(self, yes: bool) -> SourceType
pub const fn with_standard(self, yes: bool) -> SourceType
Disable language extensions (e.g. JSX) if yes is true. No change
will occur if yes is false.
Sourcepub fn from_path<P>(path: P) -> Result<SourceType, UnknownExtension>
pub fn from_path<P>(path: P) -> Result<SourceType, UnknownExtension>
Converts a file Path to SourceType.
§Examples
// supports .ts, .mts, .cts, .tsx, .d.ts, etc.
let ts = SourceType::from_path("foo.ts").unwrap();
assert!(ts.is_typescript());
assert!(!ts.is_typescript_definition());
// supports .js, .mjs, .cjs, .jsx
let jsx = SourceType::from_path("foo.jsx").unwrap();
assert!(jsx.is_javascript());
assert!(jsx.is_jsx());§Behavior
§JSX
All JavaScript-like files are treated as JSX, since some tools (like
babel) also do not make a distinction between .js and .jsx. However,
for TypeScript files, only .tsx files are treated as JSX.
Note that this behavior deviates from SourceType::cjs, which produces
scripts.
§Modules vs. Scripts.
Oxc has partial support for Node’s
CommonJS detection
strategy. Any file with a .c[tj]s extension is treated as a script.
All other files are treated as modules.
§Errors
Returns UnknownExtension if:
- there is no file name
- the file extension is not one of “js”, “mjs”, “cjs”, “jsx”, “ts”,
“mts”, “cts”, “tsx”. See
VALID_EXTENSIONSfor the list of valid extensions.
Sourcepub fn from_extension(extension: &str) -> Result<SourceType, UnknownExtension>
pub fn from_extension(extension: &str) -> Result<SourceType, UnknownExtension>
Converts a file extension to SourceType.
§Errors
Returns UnknownExtension if:
- the file extension is not one of “js”, “mjs”, “cjs”, “jsx”, “ts”,
“mts”, “cts”, “tsx”. See
VALID_EXTENSIONSfor the list of valid extensions.
Trait Implementations§
Source§impl Clone for SourceType
impl Clone for SourceType
Source§fn clone(&self) -> SourceType
fn clone(&self) -> SourceType
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<'a> CloneIn<'a> for SourceType
impl<'a> CloneIn<'a> for SourceType
Source§type Cloned = SourceType
type Cloned = SourceType
Source§fn clone_in(&self, _: &'a Allocator) -> SourceType
fn clone_in(&self, _: &'a Allocator) -> SourceType
self into the given allocator. allocator may be the same one
that self is already in.Source§fn clone_in_with_semantic_ids(
&self,
allocator: &'new_alloc Allocator,
) -> Self::Cloned
fn clone_in_with_semantic_ids( &self, allocator: &'new_alloc Allocator, ) -> Self::Cloned
clone_in, but for some special type, it will also clone the semantic ids.
Please use this method only if you make sure semantic info is synced with the ast node.Source§impl ContentEq for SourceType
impl ContentEq for SourceType
Source§fn content_eq(&self, other: &SourceType) -> bool
fn content_eq(&self, other: &SourceType) -> bool
self and other to be equal.Source§fn content_ne(&self, other: &Self) -> bool
fn content_ne(&self, other: &Self) -> bool
self and other not to be equal.
The default implementation is almost always
sufficient, and should not be overridden without very good reason.Source§impl Debug for SourceType
impl Debug for SourceType
Source§impl Default for SourceType
impl Default for SourceType
Source§fn default() -> SourceType
fn default() -> SourceType
Source§impl<'a> Dummy<'a> for SourceType
impl<'a> Dummy<'a> for SourceType
Source§fn dummy(allocator: &'a Allocator) -> SourceType
fn dummy(allocator: &'a Allocator) -> SourceType
Create a dummy SourceType.
Does not allocate any data into arena.
Source§impl From<FileExtension> for SourceType
impl From<FileExtension> for SourceType
Source§fn from(file_ext: FileExtension) -> SourceType
fn from(file_ext: FileExtension) -> SourceType
Source§impl Hash for SourceType
impl Hash for SourceType
Source§impl PartialEq for SourceType
impl PartialEq for SourceType
impl Copy for SourceType
impl Eq for SourceType
impl StructuralPartialEq for SourceType
Auto Trait Implementations§
impl Freeze for SourceType
impl RefUnwindSafe for SourceType
impl Send for SourceType
impl Sync for SourceType
impl Unpin for SourceType
impl UnwindSafe for SourceType
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more