Skip to content

Commit

Permalink
Normalize object literal shorthand even when wrapped (#8155)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed May 29, 2022
1 parent a3571b2 commit 3b3b135
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
@@ -0,0 +1,3 @@
const v = require("./b.js");

output = v;
@@ -0,0 +1,7 @@
const value = require("./c.js");

const obj = {
value,
};

exports = module.exports = obj.value;
@@ -0,0 +1 @@
module.exports = 1234;
14 changes: 12 additions & 2 deletions packages/core/integration-tests/test/scope-hoisting.js
Expand Up @@ -3705,8 +3705,6 @@ describe('scope hoisting', function () {
),
);

// console.log(await outputFS.readFile(b.getBundles()[0].filePath, 'utf8'));

let output = await run(b);
assert.equal(output, 1);
});
Expand Down Expand Up @@ -3771,6 +3769,18 @@ describe('scope hoisting', function () {
assert.deepEqual(output, {foo: 2});
});

it('should support referencing a require in object literal shorthands when wrapped', async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/commonjs/wrap-module-obj-literal-require/a.js',
),
);

let output = await run(b);
assert.strictEqual(output, 1234);
});

it('should support typeof require when wrapped', async function () {
// https://github.com/parcel-bundler/parcel/issues/5883
let b = await bundle(
Expand Down
4 changes: 0 additions & 4 deletions packages/transformers/js/core/src/hoist.rs
Expand Up @@ -914,10 +914,6 @@ impl<'a> Fold for Hoist<'a> {
}

fn fold_prop(&mut self, node: Prop) -> Prop {
if self.collect.should_wrap {
return node.fold_children_with(self);
}

match node {
Prop::Shorthand(ident) => Prop::KeyValue(KeyValueProp {
key: PropName::Ident(Ident::new(ident.sym.clone(), DUMMY_SP)),
Expand Down
5 changes: 2 additions & 3 deletions packages/transformers/js/core/src/lib.rs
Expand Up @@ -28,11 +28,10 @@ use std::str::FromStr;

use path_slash::PathExt;
use serde::{Deserialize, Serialize};
use swc_atoms::JsWord;
use swc_common::comments::SingleThreadedComments;
use swc_common::errors::{DiagnosticBuilder, Emitter, Handler};
use swc_common::{chain, sync::Lrc, FileName, Globals, Mark, SourceMap};
use swc_ecmascript::ast::{Ident, Module};
use swc_ecmascript::ast::Module;
use swc_ecmascript::codegen::text_writer::JsWriter;
use swc_ecmascript::parser::lexer::Lexer;
use swc_ecmascript::parser::{EsConfig, PResult, Parser, StringInput, Syntax, TsConfig};
Expand All @@ -44,7 +43,7 @@ use swc_ecmascript::transforms::{
optimization::simplify::dead_branch_remover, optimization::simplify::expr_simplifier,
pass::Optional, proposals::decorators, react, typescript,
};
use swc_ecmascript::visit::{Fold, FoldWith, VisitWith};
use swc_ecmascript::visit::{FoldWith, VisitWith};

use decl_collector::*;
use dependency_collector::*;
Expand Down

0 comments on commit 3b3b135

Please sign in to comment.