Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generated code order when using TypeScript, ES6 and decorators #7293

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "@parcel/config-default",
"transformers": {
"*.{ts,tsx}": ["@parcel/transformer-typescript-tsc"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {ClassOne} from './one';

module.exports = ClassOne.name
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {ClassTwo} from './two';

function decorator() {
}

export class ClassOne {
@decorator(ClassTwo)
two?: ClassTwo;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"target": "es6"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {ClassOne} from './one';

function decorator() {
}

export class ClassTwo {
@decorator(ClassOne)
one?: ClassOne;
}
15 changes: 15 additions & 0 deletions packages/core/integration-tests/test/typescript-tsc.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,19 @@ describe('typescript tsc', function () {
let output = await run(b);
assert.equal(new output.Foo().run(), 'bar');
});

it('should support decorators when the target is es6', async () => {
const b = await bundle(
path.join(__dirname, '/integration/typescript-es6-decorators/index.ts'),
{
config: path.join(
__dirname,
'/integration/typescript-es6-decorators/.parcelrc',
),
},
);

const output = await run(b);
assert.strictEqual(output, 'ClassOne');
});
thewilkybarkid marked this conversation as resolved.
Show resolved Hide resolved
});
7 changes: 5 additions & 2 deletions packages/transformers/js/core/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub fn esm2cjs(node: Module, versions: Option<Versions>) -> (Module, bool) {
interops: HashSet::new(),
requires: vec![],
exports: vec![],
export_alls: vec![],
needs_helpers: false,
in_export_decl: false,
in_function_scope: false,
Expand All @@ -44,6 +45,7 @@ struct ESMFold {
requires: Vec<ModuleItem>,
// List of exports to add.
exports: Vec<ModuleItem>,
export_alls: Vec<ModuleItem>,
needs_helpers: bool,
in_export_decl: bool,
in_function_scope: bool,
Expand Down Expand Up @@ -398,7 +400,7 @@ impl Fold for ESMFold {
],
export.span,
);
self.requires.push(export);
self.export_alls.push(export);
}
ModuleDecl::ExportDefaultExpr(export) => {
needs_interop_flag = true;
Expand Down Expand Up @@ -527,7 +529,8 @@ impl Fold for ESMFold {

let mut node = node;
items.splice(0..0, self.requires.clone());
items.splice(0..0, self.exports.clone());
items.extend_from_slice(&self.exports.clone());
items.extend_from_slice(&self.export_alls.clone());

if self.needs_helpers {
items.insert(
Expand Down