Skip to content

Commit

Permalink
feat(span)!: change SourceType::js to SourceType::cjs and `Source…
Browse files Browse the repository at this point in the history
…Type::mjs` (#5606)
  • Loading branch information
Boshen committed Sep 8, 2024
1 parent 1c051ae commit 4a8aec1
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 31 deletions.
6 changes: 3 additions & 3 deletions crates/oxc_codegen/tests/integration/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use oxc_parser::Parser;
use oxc_span::SourceType;

pub fn test(source_text: &str, expected: &str) {
let source_type = SourceType::default().with_module(true).with_jsx(true);
let source_type = SourceType::jsx();
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
let result = CodeGenerator::new()
Expand All @@ -22,7 +22,7 @@ pub fn test(source_text: &str, expected: &str) {
}

pub fn test_without_source(source_text: &str, expected: &str) {
let source_type = SourceType::default().with_module(true).with_jsx(true);
let source_type = SourceType::jsx();
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
let result = CodeGenerator::new().build(&ret.program).source_text;
Expand All @@ -33,7 +33,7 @@ pub fn test_without_source(source_text: &str, expected: &str) {
}

pub fn test_minify(source_text: &str, expected: &str) {
let source_type = SourceType::default().with_module(true).with_jsx(true);
let source_type = SourceType::jsx();
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
let result = CodeGenerator::new()
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/tests/integration/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use oxc_span::SourceType;

fn codegen(source_text: &str) -> String {
let allocator = Allocator::default();
let source_type = SourceType::default().with_typescript(true).with_module(true);
let source_type = SourceType::ts();
let ret = Parser::new(&allocator, source_text, source_type).parse();
CodeGenerator::new()
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_isolated_declarations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<'a> IsolatedDeclarations<'a> {
///
/// Returns `Vec<Error>` if any errors were collected during the transformation.
pub fn build(mut self, program: &Program<'a>) -> IsolatedDeclarationsReturn<'a> {
let source_type = SourceType::default().with_module(true).with_typescript_definition(true);
let source_type = SourceType::d_ts();
let directives = self.ast.vec();
let stmts = self.transform_program(program);
let program = self.ast.program(SPAN, source_type, None, directives, stmts);
Expand Down
8 changes: 2 additions & 6 deletions crates/oxc_linter/src/partial_loader/astro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ impl<'a> AstroPartialLoader<'a> {

let js_code =
Span::new(start + ASTRO_SPLIT.len() as u32, end).source_text(self.source_text);
Some(JavaScriptSource::new(
js_code,
SourceType::default().with_typescript(true).with_module(true),
start as usize,
))
Some(JavaScriptSource::new(js_code, SourceType::ts(), start as usize))
}

/// In .astro files, you can add client-side JavaScript by adding one (or more) `<script>` tags.
Expand Down Expand Up @@ -89,7 +85,7 @@ impl<'a> AstroPartialLoader<'a> {
};
results.push(JavaScriptSource::new(
&self.source_text[js_start..js_end],
SourceType::default().with_typescript(true).with_module(true),
SourceType::ts(),
js_start,
));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/partial_loader/svelte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<'a> SveltePartialLoader<'a> {
let js_end = pointer + offset;

let source_text = &self.source_text[js_start..js_end];
let source_type = SourceType::default().with_module(true).with_typescript(is_ts);
let source_type = SourceType::mjs().with_typescript(is_ts);
Some(JavaScriptSource::new(source_text, source_type, js_start))
}
}
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_linter/src/partial_loader/vue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ impl<'a> VuePartialLoader<'a> {
*pointer += offset + SCRIPT_END.len();

let source_text = &self.source_text[js_start..js_end];
let source_type =
SourceType::default().with_module(true).with_typescript(is_ts).with_jsx(is_jsx);
let source_type = SourceType::mjs().with_typescript(is_ts).with_jsx(is_jsx);
Some(JavaScriptSource::new(source_text, source_type, js_start))
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/tests/mangler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use oxc_span::SourceType;

fn mangle(source_text: &str) -> String {
let allocator = Allocator::default();
let source_type = SourceType::default().with_module(true);
let source_type = SourceType::mjs();
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = ret.program;
let mangler = Mangler::new().build(&program);
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_module_lexer/tests/esm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub struct ModuleLexer {

fn parse(source: &str) -> ModuleLexer {
let allocator = Allocator::default();
let source_type = SourceType::default().with_module(true);
let source_type = SourceType::mjs();
let ret = Parser::new(&allocator, source, source_type).parse();
assert!(ret.errors.is_empty(), "{source} should not produce errors.\n{:?}", ret.errors);
let module_lexer = oxc_module_lexer::ModuleLexer::new().build(&ret.program);
Expand Down Expand Up @@ -1515,7 +1515,7 @@ fn export_default() {

fn expect_parse_error(source: &str) {
let allocator = Allocator::default();
let source_type = SourceType::default().with_module(true);
let source_type = SourceType::mjs();
let ret = Parser::new(&allocator, source, source_type).parse();
assert!(!ret.errors.is_empty());
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_module_lexer/tests/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use oxc_span::SourceType;

fn parse(source: &str) -> ModuleLexer {
let allocator = Allocator::default();
let source_type = SourceType::default().with_module(true).with_typescript_definition(true);
let source_type = SourceType::mjs().with_typescript_definition(true);
let ret = Parser::new(&allocator, source, source_type).parse();
assert!(ret.errors.is_empty(), "{source} should not produce errors.\n{:?}", ret.errors);
let module_lexer = oxc_module_lexer::ModuleLexer::new().build(&ret.program);
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ mod test {
#[test]
fn unambiguous() {
let allocator = Allocator::default();
let source_type = SourceType::default().with_unambiguous(true);
let source_type = SourceType::unambiguous();
assert!(source_type.is_unambiguous());
let sources = ["import x from 'foo';", "export {x} from 'foo';", "import.meta"];
for source in sources {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ mod tests {
fn test_reference_resolutions_simple_read_write() {
let alloc = Allocator::default();
let target_symbol_name = Atom::from("a");
let typescript = SourceType::default().with_typescript(true).with_module(true);
let typescript = SourceType::ts();
let sources = [
// simple cases
(SourceType::default(), "let a = 1; a = 2", ReferenceFlags::write()),
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_semantic/src/module_record/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod module_record_tests {
use crate::SemanticBuilder;

fn build(source_text: &str) -> Arc<ModuleRecord> {
let source_type = SourceType::default().with_module(true);
let source_type = SourceType::mjs();
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = allocator.alloc(ret.program);
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_semantic/tests/integration/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<'a> SemanticTester<'a> {
///
/// Use [`SemanticTester::js`] for JavaScript test cases.
pub fn ts(source_text: &'static str) -> Self {
Self::new(source_text, SourceType::default().with_module(true).with_typescript(true))
Self::new(source_text, SourceType::ts())
}

/// Create a new tester for a TypeScript test case with JSX.
Expand All @@ -52,7 +52,7 @@ impl<'a> SemanticTester<'a> {
///
/// Use [`SemanticTester::ts`] for TypeScript test cases.
pub fn js(source_text: &'static str) -> Self {
Self::new(source_text, SourceType::default().with_module(true))
Self::new(source_text, SourceType::mjs())
}

/// Create a new tester for some source text.
Expand Down
30 changes: 23 additions & 7 deletions crates/oxc_span/src/source_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub enum LanguageVariant {
impl Default for SourceType {
#[inline]
fn default() -> Self {
Self::js()
Self::mjs()
}
}

Expand Down Expand Up @@ -114,7 +114,7 @@ impl SourceType {
/// ```
/// # use oxc_span::SourceType;
///
/// let js = SourceType::js();
/// let js = SourceType::cjs();
/// assert!(js.is_javascript());
/// assert!(js.is_script()); // not a module
/// assert!(!js.is_jsx());
Expand All @@ -123,7 +123,23 @@ impl SourceType {
/// [`JavaScript`]: Language::JavaScript
/// [`module`]: ModuleKind::Module
/// [`JSX`]: LanguageVariant::Jsx
pub const fn js() -> Self {
pub const fn cjs() -> Self {
Self {
language: Language::JavaScript,
module_kind: ModuleKind::Script,
variant: LanguageVariant::Standard,
}
}

pub const fn mjs() -> Self {
Self {
language: Language::JavaScript,
module_kind: ModuleKind::Module,
variant: LanguageVariant::Standard,
}
}

pub const fn unambiguous() -> Self {
Self {
language: Language::JavaScript,
module_kind: ModuleKind::Unambiguous,
Expand All @@ -144,12 +160,12 @@ impl SourceType {
///
/// [`JavaScript`]: Language::JavaScript
pub const fn jsx() -> Self {
Self::js().with_jsx(true)
Self::mjs().with_jsx(true)
}

/// Creates a [`SourceType`] representing a [`TypeScript`] file.
///
/// Unlike [`SourceType::js`], this method creates [`modules`]. Use
/// Unlike [`SourceType::cjs`], this method creates [`modules`]. Use
/// [`SourceType::tsx`] for TypeScript files with [`JSX`] support.
///
/// ## Example
Expand Down Expand Up @@ -325,7 +341,7 @@ impl SourceType {
/// 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::js`], which produces
/// Note that this behavior deviates from [`SourceType::cjs`], which produces
/// [`scripts`].
///
/// ### Modules vs. Scripts.
Expand Down Expand Up @@ -492,7 +508,7 @@ mod tests {
assert!(!ty.is_typescript(), "{ty:?}");
}

assert_eq!(SourceType::js().with_jsx(true).with_unambiguous(true), js);
assert_eq!(SourceType::jsx().with_unambiguous(true), js);
assert_eq!(SourceType::jsx().with_module(true), jsx);

assert!(js.is_unambiguous());
Expand Down
2 changes: 1 addition & 1 deletion tasks/website/src/linter/rules/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn source_type_from_code_element(code: ElementRef) -> Option<SourceType> {
match *lang {
"javascript" | "js" => Some(SourceType::default()),
"typescript" | "ts" => Some(SourceType::default().with_typescript(true)),
"tsx" => Some(SourceType::default().with_typescript(true).with_jsx(true)),
"tsx" => Some(SourceType::tsx()),
// FIXME: lots of jsx examples are usefully succinct but not valid JSX.
// "jsx" => Some(SourceType::default().with_jsx(true).with_always_strict(true)),
_ => None,
Expand Down

0 comments on commit 4a8aec1

Please sign in to comment.