mirror of
https://github.com/danbulant/oxc
synced 2026-05-21 05:08:45 +00:00
feat(transformer): add defaulted Module::Preserve option (#7225)
This commit is contained in:
parent
68dc899887
commit
b4258ee58e
5 changed files with 3006 additions and 338 deletions
|
|
@ -15,6 +15,7 @@ use crate::options::babel::BabelModule;
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub enum Module {
|
pub enum Module {
|
||||||
#[default]
|
#[default]
|
||||||
|
Preserve,
|
||||||
ESM,
|
ESM,
|
||||||
CommonJS,
|
CommonJS,
|
||||||
}
|
}
|
||||||
|
|
@ -36,7 +37,7 @@ impl TryFrom<BabelModule> for Module {
|
||||||
fn try_from(value: BabelModule) -> Result<Self, Self::Error> {
|
fn try_from(value: BabelModule) -> Result<Self, Self::Error> {
|
||||||
match value {
|
match value {
|
||||||
BabelModule::Commonjs => Ok(Self::CommonJS),
|
BabelModule::Commonjs => Ok(Self::CommonJS),
|
||||||
BabelModule::Auto | BabelModule::Boolean(false) => Ok(Self::ESM),
|
BabelModule::Auto | BabelModule::Boolean(false) => Ok(Self::Preserve),
|
||||||
_ => Err(Error::msg(format!("{value:?} module is not implemented."))),
|
_ => Err(Error::msg(format!("{value:?} module is not implemented."))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,25 @@ describe('transform', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('modules', () => {
|
||||||
|
it('should transform export = and import ', () => {
|
||||||
|
const code = `
|
||||||
|
export = function foo (): void {}
|
||||||
|
import bar = require('bar')
|
||||||
|
`;
|
||||||
|
const ret = oxc.transform('test.ts', code, {
|
||||||
|
typescript: {
|
||||||
|
declaration: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
assert.deepEqual(ret, {
|
||||||
|
code: 'module.exports = function foo() {};\nconst bar = require("bar");\n',
|
||||||
|
declaration: 'declare const _default: () => void;\nexport = _default;\n',
|
||||||
|
errors: [],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('react refresh plugin', () => {
|
describe('react refresh plugin', () => {
|
||||||
const code = `import { useState } from "react";
|
const code = `import { useState } from "react";
|
||||||
export const App = () => {
|
export const App = () => {
|
||||||
|
|
|
||||||
|
|
@ -423,7 +423,14 @@ after transform: ["Y", "foo"]
|
||||||
rebuilt : []
|
rebuilt : []
|
||||||
|
|
||||||
tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/typescript/import-require/input.js
|
tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/typescript/import-require/input.js
|
||||||
semantic error: Import assignment cannot be used when targeting ECMAScript modules.
|
semantic error: Missing SymbolId: "x"
|
||||||
|
Missing ReferenceId: "require"
|
||||||
|
Binding symbols mismatch:
|
||||||
|
after transform: ScopeId(0): [SymbolId(0)]
|
||||||
|
rebuilt : ScopeId(0): [SymbolId(0)]
|
||||||
|
Unresolved references mismatch:
|
||||||
|
after transform: []
|
||||||
|
rebuilt : ["require"]
|
||||||
|
|
||||||
tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/typescript/literals/input.js
|
tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/typescript/literals/input.js
|
||||||
semantic error: Bindings mismatch:
|
semantic error: Bindings mismatch:
|
||||||
|
|
@ -1100,10 +1107,14 @@ after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3), ScopeId(4), Sc
|
||||||
rebuilt : ScopeId(0): []
|
rebuilt : ScopeId(0): []
|
||||||
|
|
||||||
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/export/equals/input.ts
|
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/export/equals/input.ts
|
||||||
semantic error: Export assignment cannot be used when targeting ECMAScript modules.
|
semantic error: Reference flags mismatch for "f":
|
||||||
|
after transform: ReferenceId(0): ReferenceFlags(Read | Type)
|
||||||
|
rebuilt : ReferenceId(1): ReferenceFlags(Read)
|
||||||
|
|
||||||
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/export/equals-in-unambiguous/input.ts
|
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/export/equals-in-unambiguous/input.ts
|
||||||
semantic error: Export assignment cannot be used when targeting ECMAScript modules.
|
semantic error: Reference flags mismatch for "f":
|
||||||
|
after transform: ReferenceId(0): ReferenceFlags(Read | Type)
|
||||||
|
rebuilt : ReferenceId(1): ReferenceFlags(Read)
|
||||||
|
|
||||||
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/export/export-type/input.ts
|
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/export/export-type/input.ts
|
||||||
semantic error: Bindings mismatch:
|
semantic error: Bindings mismatch:
|
||||||
|
|
@ -1223,10 +1234,24 @@ after transform: ScopeId(0): [SymbolId(0)]
|
||||||
rebuilt : ScopeId(0): [SymbolId(0)]
|
rebuilt : ScopeId(0): [SymbolId(0)]
|
||||||
|
|
||||||
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/equals-require/input.ts
|
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/equals-require/input.ts
|
||||||
semantic error: Import assignment cannot be used when targeting ECMAScript modules.
|
semantic error: Missing SymbolId: "a"
|
||||||
|
Missing ReferenceId: "require"
|
||||||
|
Binding symbols mismatch:
|
||||||
|
after transform: ScopeId(0): [SymbolId(0)]
|
||||||
|
rebuilt : ScopeId(0): [SymbolId(0)]
|
||||||
|
Unresolved references mismatch:
|
||||||
|
after transform: []
|
||||||
|
rebuilt : ["require"]
|
||||||
|
|
||||||
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/equals-require-in-unambiguous/input.ts
|
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/equals-require-in-unambiguous/input.ts
|
||||||
semantic error: Import assignment cannot be used when targeting ECMAScript modules.
|
semantic error: Missing SymbolId: "a"
|
||||||
|
Missing ReferenceId: "require"
|
||||||
|
Binding symbols mismatch:
|
||||||
|
after transform: ScopeId(0): [SymbolId(0)]
|
||||||
|
rebuilt : ScopeId(0): [SymbolId(0)]
|
||||||
|
Unresolved references mismatch:
|
||||||
|
after transform: []
|
||||||
|
rebuilt : ["require"]
|
||||||
|
|
||||||
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import/input.ts
|
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import/input.ts
|
||||||
semantic error: Missing SymbolId: "A"
|
semantic error: Missing SymbolId: "A"
|
||||||
|
|
@ -1235,10 +1260,24 @@ after transform: ScopeId(0): [SymbolId(0)]
|
||||||
rebuilt : ScopeId(0): [SymbolId(0)]
|
rebuilt : ScopeId(0): [SymbolId(0)]
|
||||||
|
|
||||||
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import-require/input.ts
|
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import-require/input.ts
|
||||||
semantic error: Import assignment cannot be used when targeting ECMAScript modules.
|
semantic error: Missing SymbolId: "a"
|
||||||
|
Missing ReferenceId: "require"
|
||||||
|
Binding symbols mismatch:
|
||||||
|
after transform: ScopeId(0): [SymbolId(0)]
|
||||||
|
rebuilt : ScopeId(0): [SymbolId(0)]
|
||||||
|
Unresolved references mismatch:
|
||||||
|
after transform: []
|
||||||
|
rebuilt : ["require"]
|
||||||
|
|
||||||
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import-type-as-identifier/input.ts
|
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import-type-as-identifier/input.ts
|
||||||
semantic error: Import assignment cannot be used when targeting ECMAScript modules.
|
semantic error: Missing SymbolId: "type"
|
||||||
|
Missing ReferenceId: "require"
|
||||||
|
Binding symbols mismatch:
|
||||||
|
after transform: ScopeId(0): [SymbolId(0)]
|
||||||
|
rebuilt : ScopeId(0): [SymbolId(0)]
|
||||||
|
Unresolved references mismatch:
|
||||||
|
after transform: []
|
||||||
|
rebuilt : ["require"]
|
||||||
|
|
||||||
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import-type-require/input.ts
|
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import-type-require/input.ts
|
||||||
semantic error: Bindings mismatch:
|
semantic error: Bindings mismatch:
|
||||||
|
|
@ -1246,7 +1285,17 @@ after transform: ScopeId(0): ["a"]
|
||||||
rebuilt : ScopeId(0): []
|
rebuilt : ScopeId(0): []
|
||||||
|
|
||||||
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-named-import-require/input.ts
|
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-named-import-require/input.ts
|
||||||
semantic error: Import assignment cannot be used when targeting ECMAScript modules.
|
semantic error: Missing SymbolId: "a"
|
||||||
|
Missing ReferenceId: "require"
|
||||||
|
Binding symbols mismatch:
|
||||||
|
after transform: ScopeId(0): [SymbolId(0)]
|
||||||
|
rebuilt : ScopeId(0): [SymbolId(0)]
|
||||||
|
Reference symbol mismatch for "a":
|
||||||
|
after transform: SymbolId(0) "a"
|
||||||
|
rebuilt : SymbolId(0) "a"
|
||||||
|
Unresolved references mismatch:
|
||||||
|
after transform: []
|
||||||
|
rebuilt : ["require"]
|
||||||
|
|
||||||
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/import-default-and-named-id-type/input.ts
|
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/import-default-and-named-id-type/input.ts
|
||||||
semantic error: Bindings mismatch:
|
semantic error: Bindings mismatch:
|
||||||
|
|
@ -1269,7 +1318,14 @@ after transform: ScopeId(0): ["a"]
|
||||||
rebuilt : ScopeId(0): []
|
rebuilt : ScopeId(0): []
|
||||||
|
|
||||||
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/import-type-as-identifier/input.ts
|
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/import-type-as-identifier/input.ts
|
||||||
semantic error: Import assignment cannot be used when targeting ECMAScript modules.
|
semantic error: Missing SymbolId: "type"
|
||||||
|
Missing ReferenceId: "require"
|
||||||
|
Binding symbols mismatch:
|
||||||
|
after transform: ScopeId(0): [SymbolId(0)]
|
||||||
|
rebuilt : ScopeId(0): [SymbolId(0)]
|
||||||
|
Unresolved references mismatch:
|
||||||
|
after transform: []
|
||||||
|
rebuilt : ["require"]
|
||||||
|
|
||||||
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/internal-comments/input.ts
|
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/internal-comments/input.ts
|
||||||
semantic error: Bindings mismatch:
|
semantic error: Bindings mismatch:
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,6 +1,6 @@
|
||||||
commit: d20b314c
|
commit: d20b314c
|
||||||
|
|
||||||
Passed: 317/633
|
Passed: 319/633
|
||||||
|
|
||||||
# All Passed:
|
# All Passed:
|
||||||
* babel-plugin-transform-class-static-block
|
* babel-plugin-transform-class-static-block
|
||||||
|
|
@ -572,7 +572,7 @@ rebuilt : ScopeId(1): []
|
||||||
x Output mismatch
|
x Output mismatch
|
||||||
|
|
||||||
|
|
||||||
# babel-plugin-transform-typescript (41/155)
|
# babel-plugin-transform-typescript (43/155)
|
||||||
* cast/as-expression/input.ts
|
* cast/as-expression/input.ts
|
||||||
Unresolved references mismatch:
|
Unresolved references mismatch:
|
||||||
after transform: ["T", "x"]
|
after transform: ["T", "x"]
|
||||||
|
|
@ -943,17 +943,6 @@ Scope children mismatch:
|
||||||
after transform: ScopeId(0): [ScopeId(1)]
|
after transform: ScopeId(0): [ScopeId(1)]
|
||||||
rebuilt : ScopeId(0): []
|
rebuilt : ScopeId(0): []
|
||||||
|
|
||||||
* exports/export=/input.ts
|
|
||||||
|
|
||||||
! TS(1203): Export assignment cannot be used when targeting ECMAScript
|
|
||||||
| modules.
|
|
||||||
,-[tasks/coverage/babel/packages/babel-plugin-transform-typescript/test/fixtures/exports/export=/input.ts:1:1]
|
|
||||||
1 | export = 0;
|
|
||||||
: ^^^^^^^^^^^
|
|
||||||
`----
|
|
||||||
help: Consider using 'export default' or another module format instead.
|
|
||||||
|
|
||||||
|
|
||||||
* exports/imported-types/input.ts
|
* exports/imported-types/input.ts
|
||||||
Bindings mismatch:
|
Bindings mismatch:
|
||||||
after transform: ScopeId(0): ["A", "B", "C"]
|
after transform: ScopeId(0): ["A", "B", "C"]
|
||||||
|
|
@ -1140,20 +1129,6 @@ Bindings mismatch:
|
||||||
after transform: ScopeId(0): ["A", "B"]
|
after transform: ScopeId(0): ["A", "B"]
|
||||||
rebuilt : ScopeId(0): []
|
rebuilt : ScopeId(0): []
|
||||||
|
|
||||||
* imports/import=-module/input.ts
|
|
||||||
|
|
||||||
! TS(1202): Import assignment cannot be used when targeting ECMAScript
|
|
||||||
| modules.
|
|
||||||
,-[tasks/coverage/babel/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=-module/input.ts:1:1]
|
|
||||||
1 | import lib = require("lib");
|
|
||||||
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
||||||
2 | lib();
|
|
||||||
`----
|
|
||||||
help: Consider using 'import * as ns from "mod"',
|
|
||||||
'import {a} from "mod"', 'import d from "mod"', or another
|
|
||||||
module format instead.
|
|
||||||
|
|
||||||
|
|
||||||
* imports/import=-module-to-cjs/input.ts
|
* imports/import=-module-to-cjs/input.ts
|
||||||
Missing SymbolId: "lib"
|
Missing SymbolId: "lib"
|
||||||
Missing ReferenceId: "require"
|
Missing ReferenceId: "require"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue