Skip to content

Commit

Permalink
Forward useDefineForClassFields to swc (#8107)
Browse files Browse the repository at this point in the history
* Forward useDefineForClassFields to swc

* Bump swc
  • Loading branch information
mischnic committed May 21, 2022
1 parent 97a7864 commit 9a44f02
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 47 deletions.
89 changes: 45 additions & 44 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,24 @@
class LoggerClass {
@logger()
foo = 5;
}

function logger() {
return function (proto, originalKey) {
const privateKey = `__${originalKey}`;
Object.defineProperty(proto, originalKey, {
get() {
output(`${originalKey} ${this[privateKey]}`);
return this[privateKey];
},
set(value) {
this[privateKey] = 10 + value;
},
});
};
}

const instance = new LoggerClass();
instance.foo;
instance.foo = 6;
instance.foo;
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false
}
}
18 changes: 18 additions & 0 deletions packages/core/integration-tests/test/transpilation.js
Expand Up @@ -297,6 +297,24 @@ describe('transpilation', function () {
]);
});

it('should support enabling decorators and setting useDefineForClassFields in tsconfig.json', async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/decorators-useDefineForClassFields/index.ts',
),
);

let output = [];
await run(b, {
output(...o) {
output.push(...o);
},
});

assert.deepEqual(output, ['foo 15', 'foo 16']);
});

it('should support transpiling optional chaining', async function () {
let b = await bundle(
path.join(__dirname, '/integration/babel-optional-chaining/index.js'),
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/js/core/Cargo.toml
Expand Up @@ -9,7 +9,7 @@ crate-type = ["rlib"]

[dependencies]
swc_ecmascript = { version = "0.157.0", features = ["parser", "transforms", "module", "optimization", "react", "typescript", "utils", "visit", "codegen", "utils", "preset_env"] }
swc_common = { version = "0.18.2", features = ["tty-emitter", "sourcemap"] }
swc_common = { version = "0.18.5", features = ["tty-emitter", "sourcemap"] }
swc_atoms = "0.2.11"
indoc = "1.0.3"
serde = "1.0.123"
Expand Down
3 changes: 2 additions & 1 deletion packages/transformers/js/core/src/lib.rs
Expand Up @@ -81,6 +81,7 @@ pub struct Config {
automatic_jsx_runtime: bool,
jsx_import_source: Option<String>,
decorators: bool,
use_define_for_class_fields: bool,
is_development: bool,
react_refresh: bool,
targets: Option<HashMap<String, String>>,
Expand Down Expand Up @@ -234,9 +235,9 @@ pub fn transform(config: Config) -> Result<TransformResult, std::io::Error> {
Optional::new(
decorators::decorators(decorators::Config {
legacy: true,
use_define_for_class_fields: config.use_define_for_class_fields,
// Always disabled for now, SWC's implementation doesn't match TSC.
emit_metadata: false,
use_define_for_class_fields: true
}),
config.decorators
),
Expand Down
8 changes: 7 additions & 1 deletion packages/transformers/js/src/JSTransformer.js
Expand Up @@ -138,6 +138,8 @@ type TSConfig = {
jsxImportSource?: string,
// https://www.typescriptlang.org/tsconfig#experimentalDecorators
experimentalDecorators?: boolean,
// https://www.typescriptlang.org/tsconfig#useDefineForClassFields
useDefineForClassFields?: boolean,
...
},
...
Expand All @@ -152,7 +154,8 @@ export default (new Transformer({
jsxImportSource,
automaticJSXRuntime,
reactRefresh,
decorators;
decorators,
useDefineForClassFields;
if (config.isSource) {
let reactLib;
if (pkg?.alias && pkg.alias['react']) {
Expand Down Expand Up @@ -231,6 +234,7 @@ export default (new Transformer({

isJSX = Boolean(compilerOptions?.jsx || pragma);
decorators = compilerOptions?.experimentalDecorators;
useDefineForClassFields = compilerOptions?.useDefineForClassFields;
}

// Check if we should ignore fs calls
Expand Down Expand Up @@ -280,6 +284,7 @@ export default (new Transformer({
inlineFS,
reactRefresh,
decorators,
useDefineForClassFields,
};
},
async transform({asset, config, options, logger}) {
Expand Down Expand Up @@ -407,6 +412,7 @@ export default (new Transformer({
!asset.env.isWorklet() &&
Boolean(config?.reactRefresh),
decorators: Boolean(config?.decorators),
use_define_for_class_fields: Boolean(config?.useDefineForClassFields),
targets,
source_maps: !!asset.env.sourceMap,
scope_hoist:
Expand Down

0 comments on commit 9a44f02

Please sign in to comment.