mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
feat(span)!: change SourceType::js to SourceType::cjs and SourceType::mjs (#5606)
This commit is contained in:
parent
1c051ae9ae
commit
4a8aec1605
15 changed files with 42 additions and 31 deletions
|
|
@ -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()
|
||||
|
|
@ -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;
|
||||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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() })
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
@ -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,
|
||||
));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
@ -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());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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()),
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ pub enum LanguageVariant {
|
|||
impl Default for SourceType {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
Self::js()
|
||||
Self::mjs()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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());
|
||||
|
|
@ -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,
|
||||
|
|
@ -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
|
||||
|
|
@ -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.
|
||||
|
|
@ -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());
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Reference in a new issue