Skip to content

Commit

Permalink
fixup edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed May 24, 2020
1 parent b2c4b42 commit 75ec628
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
30 changes: 18 additions & 12 deletions src/ast/nodes/AssignmentExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,31 @@ export default class AssignmentExpression extends NodeBase {
: this.left.variable?.exportName;
const _ = options.compact ? '' : ' ';
if (exportNames) {
let rhsParens = '';
let lhsSystemExports = '';
for (const exportName of exportNames) {
lhsSystemExports += `${lhsSystemExports.length ? _ : ''}exports('${exportName}',`;
rhsParens += ')';
}
const operatorPos = findFirstOccurrenceOutsideComment(
code.original,
this.operator,
this.left.end
);
const operation =
this.operator.length > 1 ? ` ${exportNames[0]} ${this.operator.slice(0, -1)}` : '';
code.overwrite(
operatorPos,
operatorPos + this.operator.length,
`=${_}${lhsSystemExports}${operation}`
);
code.appendLeft(this.right.end, rhsParens);
if (exportNames.length === 1) {
code.overwrite(
operatorPos,
operatorPos + this.operator.length,
`=${_}exports('${exportNames[0]}',${operation}`
);
code.appendLeft(this.right.end, ')');
} else {
code.overwrite(
operatorPos,
operatorPos + this.operator.length,
`=${_}function${_}(v)${_}{${getSystemExportStatement(
[this.left.variable!],
options
)};${_}return v;}${_}(${operation}`
);
code.appendLeft(this.right.end, ')');
}
} else if ('addExportedVariables' in this.left) {
const systemPatternExports: Variable[] = [];
this.left.addExportedVariables(systemPatternExports);
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/ExportDefaultDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export default class ExportDefaultDeclaration extends NodeBase {
this.variable.exportName &&
this.variable.exportName.length > 1
) {
code.appendLeft(this.end, ` ${getSystemExportStatement([this.variable])};`);
code.appendLeft(this.end, ` ${getSystemExportStatement([this.variable], options)};`);
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/ast/nodes/VariableDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export default class VariableDeclaration extends NodeBase {
isInDeclaration = false;
} else {
if (options.format === 'system' && node.init !== null) {
const _ = options.compact ? '' : ' ';
if (node.id.type !== NodeType.Identifier) {
node.id.addExportedVariables(systemPatternExports);
} else if (node.id.variable!.safeExportName) {
Expand All @@ -187,10 +188,19 @@ export default class VariableDeclaration extends NodeBase {
);
nextSeparatorString += ')';
} else if (node.id.variable!.exportName) {
for (const exportName of node.id.variable!.exportName) {
if (node.id.variable!.exportName.length === 1) {
code.prependLeft(
code.original.indexOf('=', node.id.end) + 1,
` exports('${exportName}',`
` exports('${node.id.variable!.exportName[0]}',`
);
nextSeparatorString += ')';
} else {
code.prependLeft(
code.original.indexOf('=', node.id.end) + 1,
` function${_}(v)${_}{${getSystemExportStatement(
[node.id.variable!],
options
)};${_}return v;}${_}(`
);
nextSeparatorString += ')';
}
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/no-treeshake/_expected/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ System.register('stirred', ['external'], function (exports) {

var foo = 13;

const quux = exports('strange', exports('quux', 1));
const quux = function (v) {exports({quux: quux, strange: quux}); return v;} ( 1);

const other = () => quux;

Expand Down
12 changes: 6 additions & 6 deletions test/form/samples/system-multiple-export-bindings/_expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ System.register([], function (exports) {
// Namespace variable

// Variable Declaration
let a = exports('a2', exports('a', 1)), b = exports('b', 2), c = exports('c2', exports('c', 3));
let a = function (v) {exports({a: a, a2: a}); return v;} ( 1), b = exports('b', 2), c = function (v) {exports({c: c, c2: c}); return v;} ( 3);

// Export default expression
var a$1 = exports('default', a);

// Assignment Expression
a = exports('a', exports('a2', b = exports('b', c = exports('c', exports('c2', 0)))));
a = function (v) {exports({a: a, a2: a}); return v;} ( b = exports('b', c = function (v) {exports({c: c, c2: c}); return v;} ( 0)));

// Destructing Assignment Expression
(function (v) {exports({a: a, a2: a, b: b, c: c, c2: c}); return v;} ({ a, b, c } = { c: 4, b: 5, a: 6 }));

// Destructuring Defaults
var p = exports('pp', exports('p', 5));
var q = exports('qq', exports('q', 10));
(function (v) {exports({p: p, pp: p}); return v;} ({ p = q = exports('q', exports('qq', 20)) } = {}));
var p = function (v) {exports({p: p, pp: p}); return v;} ( 5);
var q = function (v) {exports({q: q, qq: q}); return v;} ( 10);
(function (v) {exports({p: p, pp: p}); return v;} ({ p = q = function (v) {exports({q: q, qq: q}); return v;} ( 20) } = {}));

// Function Assignment
function fn () {

}
fn = exports('fn', exports('fn2', 5));
fn = function (v) {exports({fn: fn, fn2: fn}); return v;} ( 5);

// Update Expression
a++, exports({a: a, a2: a}), (exports('b', b + 1), b++), ++c, exports({c: c, c2: c});
Expand Down

0 comments on commit 75ec628

Please sign in to comment.