mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
fix(transformer): fix typescript globals being recognized as globals (#7100)
closes #7090
This commit is contained in:
parent
6133a502c6
commit
b188b4ad75
3 changed files with 33 additions and 11 deletions
|
|
@ -152,6 +152,7 @@ pub trait CompilerInterface {
|
|||
return;
|
||||
}
|
||||
|
||||
let stats = semantic_return.semantic.stats();
|
||||
let (mut symbols, mut scopes) = semantic_return.semantic.into_symbol_table_and_scope_tree();
|
||||
|
||||
/* Transform */
|
||||
|
|
@ -169,20 +170,34 @@ pub trait CompilerInterface {
|
|||
return;
|
||||
}
|
||||
|
||||
symbols = transformer_return.symbols;
|
||||
scopes = transformer_return.scopes;
|
||||
(symbols, scopes) = (transformer_return.symbols, transformer_return.scopes);
|
||||
}
|
||||
|
||||
if let Some(config) = self.inject_options() {
|
||||
let ret =
|
||||
InjectGlobalVariables::new(&allocator, config).build(symbols, scopes, &mut program);
|
||||
let inject_options = self.inject_options();
|
||||
let define_options = self.define_options();
|
||||
|
||||
// Symbols and scopes are out of sync.
|
||||
if inject_options.is_some() || define_options.is_some() {
|
||||
(symbols, scopes) = SemanticBuilder::new()
|
||||
.with_stats(stats)
|
||||
.build(&program)
|
||||
.semantic
|
||||
.into_symbol_table_and_scope_tree();
|
||||
}
|
||||
|
||||
if let Some(options) = inject_options {
|
||||
let ret = InjectGlobalVariables::new(&allocator, options).build(
|
||||
symbols,
|
||||
scopes,
|
||||
&mut program,
|
||||
);
|
||||
symbols = ret.symbols;
|
||||
scopes = ret.scopes;
|
||||
}
|
||||
|
||||
if let Some(config) = self.define_options() {
|
||||
if let Some(options) = define_options {
|
||||
let ret =
|
||||
ReplaceGlobalDefines::new(&allocator, config).build(symbols, scopes, &mut program);
|
||||
ReplaceGlobalDefines::new(&allocator, options).build(symbols, scopes, &mut program);
|
||||
Compressor::new(&allocator, CompressOptions::dead_code_elimination())
|
||||
.build_with_symbols_and_scopes(ret.symbols, ret.scopes, &mut program);
|
||||
// symbols = ret.symbols;
|
||||
|
|
|
|||
2
napi/transform/index.d.ts
vendored
2
napi/transform/index.d.ts
vendored
|
|
@ -203,8 +203,6 @@ export interface TransformOptions {
|
|||
typescript?: TypeScriptOptions
|
||||
/** Configure how TSX and JSX are transformed. */
|
||||
jsx?: JsxOptions
|
||||
/** Enable ES2015 transformations. */
|
||||
es2015?: Es2015Options
|
||||
/** Define Plugin */
|
||||
define?: Record<string, string>
|
||||
/** Inject Plugin */
|
||||
|
|
|
|||
|
|
@ -63,9 +63,8 @@ $RefreshReg$(_c, "App");
|
|||
});
|
||||
|
||||
describe('define plugin', () => {
|
||||
const code = 'if (process.env.NODE_ENV === "production") { foo; }';
|
||||
|
||||
it('matches output', () => {
|
||||
const code = 'if (process.env.NODE_ENV === "production") { foo; }';
|
||||
const ret = oxc.transform('test.tsx', code, {
|
||||
define: {
|
||||
'process.env.NODE_ENV': '"development"',
|
||||
|
|
@ -73,6 +72,16 @@ describe('define plugin', () => {
|
|||
});
|
||||
assert.equal(ret.code, '');
|
||||
});
|
||||
|
||||
it('handles typescript declare global', () => {
|
||||
const code = 'declare let __TEST_DEFINE__: string; console.log({ __TEST_DEFINE__ });';
|
||||
const ret = oxc.transform('test.ts', code, {
|
||||
define: {
|
||||
'__TEST_DEFINE__': '"replaced"',
|
||||
},
|
||||
});
|
||||
assert.equal(ret.code, 'console.log({ __TEST_DEFINE__: "replaced" });\n');
|
||||
});
|
||||
});
|
||||
|
||||
describe('inject plugin', () => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue