Skip to content

Commit

Permalink
Polish output cases
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jun 6, 2020
1 parent 31f26c8 commit 5f89362
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 67 deletions.
41 changes: 19 additions & 22 deletions src/ast/nodes/AssignmentExpression.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import MagicString from 'magic-string';
import { findFirstOccurrenceOutsideComment, RenderOptions } from '../../utils/renderHelpers';
import { getSystemExportStatement } from '../../utils/systemJsRendering';
import { getSystemExportStatement, getSystemExportExpressionLeft } from '../../utils/systemJsRendering';
import { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import { EMPTY_PATH, ObjectPath, UNKNOWN_PATH } from '../utils/PathTracker';
import Variable from '../variables/Variable';
import * as NodeType from './NodeType';
import { ExpressionNode, IncludeChildren, NodeBase } from './shared/Node';
import { PatternNode } from './shared/Pattern';

const eqRegex = /=\s*/g;
export default class AssignmentExpression extends NodeBase {
left!: PatternNode | ExpressionNode;
operator!:
Expand Down Expand Up @@ -62,35 +63,31 @@ export default class AssignmentExpression extends NodeBase {
this.left.end
);
const operation =
this.operator.length > 1 ? ` ${exportNames[0]} ${this.operator.slice(0, -1)}` : '';
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, ')');
}
this.operator.length > 1 ? `${_}${exportNames[0]}${_}${this.operator.slice(0, -1)}` : '';
const nextIsWs = code.original[operatorPos + this.operator.length] === ' ' || code.original[operatorPos + this.operator.length] === '\n';
code.overwrite(
operatorPos,
operatorPos + this.operator.length,
`=${_}${getSystemExportExpressionLeft(
[this.left.variable!],
false,
!nextIsWs,
options
)}${operation}`
);
code.appendLeft(this.right.end, ')');
} else if ('addExportedVariables' in this.left) {
const systemPatternExports: Variable[] = [];
this.left.addExportedVariables(systemPatternExports, options.exportNamesByVariable);
if (systemPatternExports.length > 0) {
code.prependRight(
this.start,
`function${_}(v)${_}{${getSystemExportStatement(
getSystemExportExpressionLeft(
systemPatternExports,
false,
code.original[this.start + 1] !== ' ',
options
)};${_}return v;}${_}(`
)
);
code.appendLeft(this.end, ')');
}
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/ClassDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default class ClassDeclaration extends ClassNode {
this.id &&
options.exportNamesByVariable.has(this.id.variable)
) {
code.appendLeft(this.end, ` ${getSystemExportStatement([this.id.variable], options)};`);
code.appendLeft(this.end, `${options.compact ? '' : ' '}${getSystemExportStatement([this.id.variable], options)};`);
}
super.render(code, options);
}
Expand Down
37 changes: 18 additions & 19 deletions src/ast/nodes/VariableDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { BLANK } from '../../utils/blank';
import {
getCommaSeparatedNodesWithBoundaries,
NodeRenderOptions,
RenderOptions
RenderOptions,
findFirstOccurrenceOutsideComment
} from '../../utils/renderHelpers';
import { getSystemExportStatement } from '../../utils/systemJsRendering';
import { getSystemExportStatement, getSystemExportExpressionLeft } from '../../utils/systemJsRendering';
import { InclusionContext } from '../ExecutionContext';
import { EMPTY_PATH } from '../utils/PathTracker';
import Variable from '../variables/Variable';
Expand Down Expand Up @@ -194,28 +195,26 @@ 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, options.exportNamesByVariable);
} else {
const exportNames = options.exportNamesByVariable.get(node.id.variable!);
if (exportNames) {
if (exportNames.length === 1) {
code.prependLeft(
code.original.indexOf('=', node.id.end) + 1,
` exports('${exportNames[0]}',`
);
nextSeparatorString += ')';
} else {
code.prependLeft(
code.original.indexOf('=', node.id.end) + 1,
` function${_}(v)${_}{${getSystemExportStatement(
[node.id.variable!],
options
)};${_}return v;}${_}(`
);
nextSeparatorString += ')';
}
const _ = options.compact ? '' : ' ';
const operatorPos = findFirstOccurrenceOutsideComment(code.original, '=', node.id.end);
const nextIsSpace = code.original[operatorPos + 1] === ' ';
const prependPos = operatorPos + 1 + (nextIsSpace ? 1 : 0);
const nextIsBr = code.original[prependPos] === '\n';
code.prependLeft(
prependPos,
(nextIsSpace ? '' : _) + getSystemExportExpressionLeft(
[node.id.variable!],
true,
!nextIsBr,
options
)
);
nextSeparatorString += ')';
}
}
}
Expand Down
40 changes: 37 additions & 3 deletions src/utils/systemJsRendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ import { RenderOptions } from './renderHelpers';
export function getSystemExportStatement(
exportedVariables: Variable[],
options: RenderOptions
): string {
const _ = options.compact ? '' : ' ';
if (
exportedVariables.length === 1 &&
options.exportNamesByVariable.get(exportedVariables[0])!.length === 1
) {
const variable = exportedVariables[0];
return `exports('${options.exportNamesByVariable.get(variable)}',${_}${variable.getName()})`;
} else {
return `exports({${_}${exportedVariables
.map(variable => {
return options.exportNamesByVariable
.get(variable)!
.map(exportName => `${exportName}:${_}${variable.getName()}`)
.join(`,${_}`);
})
.join(`,${_}`)}${_}})`;
}
}

export function getSystemExportExpressionLeft(
exportedVariables: Variable[],
exportsExpressionValue: boolean,
spaceForParamComma: boolean,
options: RenderOptions
): string {
const _ = options.compact ? '' : ' ';
if (
Expand All @@ -12,15 +37,24 @@ export function getSystemExportStatement(
) {
return `exports('${options.exportNamesByVariable.get(
exportedVariables[0]
)}',${_}${exportedVariables[0].getName()})`;
)}',${spaceForParamComma ? _ : ''}`;
} else if (exportsExpressionValue) {
return `function${_}(v)${_}{${_}return exports({${_}${exportedVariables
.map(variable => {
return options.exportNamesByVariable
.get(variable)!
.map(exportName => `${exportName}:${_}v`)
.join(`,${_}`);
})
.join(`,${_}`)}${_}})${options.compact ? '' : ';'}${_}}(`;
} else {
return `exports({${exportedVariables
return `function${_}(v)${_}{${_}return exports({ ${exportedVariables
.map(variable => {
return options.exportNamesByVariable
.get(variable)!
.map(exportName => `${exportName}:${_}${variable.getName()}`)
.join(`,${_}`);
})
.join(`,${_}`)}})`;
.join(`,${_}`)} }),${_}v${options.compact ? '' : ';'}${_}}(`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ System.register(['./generated-dep2.js'],function(exports){'use strict';var fn$1;
fn();
fn$1();
}
} exports('default',Main1);}}});
}exports('default',Main1);}}});
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ System.register(['./generated-dep2.js','external'],function(exports){'use strict
fn$1();
fn$3();
}
} exports('default',Main2);}}});
}exports('default',Main2);}}});
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ System.register(['external', './other.js'], function (exports) {

console.log(external, value);

var commonjs = function (v) {exports({default: commonjs, __moduleExports: commonjs}); return v;} ( 42);
var commonjs = function (v) { return exports({ default: v, __moduleExports: v }); }(42);

}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ System.register([], function (exports) {

var value = exports('value', 43);

var other = function (v) {exports({default: other, __moduleExports: other}); return v;} ( {
var other = function (v) { return exports({ default: v, __moduleExports: v }); }({
value: value
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ System.register([], function (exports) {
return {
execute: function () {

const foo = function (v) {exports({foo: foo, bar: foo}); return v;} ( 1);
const foo = function (v) { return exports({ foo: v, bar: v }); }(1);

}
};
Expand Down

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

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 = function (v) {exports({strange: quux, quux: quux}); return v;} ( 1);
const quux = function (v) { return exports({ strange: v, quux: v }); }(1);

const other = () => quux;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ System.register([], function (exports) {

let a, b, c;

console.log(function (v) {exports('a', a); return v;} ({a} = someObject));
(function (v) {exports({b: b, c: c}); return v;} ({b, c} = someObject));
console.log(exports('a', {a} = someObject));
(function (v) { return exports({ b: b, c: c }), v; }({b, c} = someObject));

}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ System.register([], function (exports) {
return {
execute: function () {

const {a = 1, ...b} = global1, c = exports('c', global2), {d} = global3; exports({a: a, b: b, d: d});
const [e, ...f] = global4; exports({e: e, f: f});
const {g, x: h = 2, y: {z: i}, a: [j ,k,, l]} = global5; exports({g: g, h: h, i: i, j: j, k: k, l: l});
const {a = 1, ...b} = global1, c = exports('c', global2), {d} = global3; exports({ a: a, b: b, d: d });
const [e, ...f] = global4; exports({ e: e, f: f });
const {g, x: h = 2, y: {z: i}, a: [j ,k,, l]} = global5; exports({ g: g, h: h, i: i, j: j, k: k, l: l });

var m = exports('m', 1);
var {m} = global6; exports('m', m);
Expand Down
20 changes: 10 additions & 10 deletions test/form/samples/system-multiple-export-bindings/_expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,38 @@ System.register([], function (exports) {
x: x,
y: y
});
exports({namespace: namespace, namespace2: namespace});
exports({ namespace: namespace, namespace2: namespace });

// Namespace variable

// Variable Declaration
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);
let a = function (v) { return exports({ a: v, a2: v }); }(1), b = exports('b', 2), c = function (v) { return exports({ c: v, c2: v }); }(3);

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

// Assignment Expression
a = function (v) {exports({a: a, a2: a}); return v;} ( b = exports('b', c = function (v) {exports({c: c, c2: c}); return v;} ( 0)));
a = function (v) { return exports({ a: a, a2: a }), v; }( b = exports('b', c = function (v) { return exports({ c: c, c2: c }), 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 }));
(function (v) { return exports({ a: a, a2: a, b: b, c: c, c2: c }), v; }({ a, b, c } = { c: 4, b: 5, a: 6 }));

// Destructuring Defaults
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) } = {}));
var p = function (v) { return exports({ p: v, pp: v }); }(5);
var q = function (v) { return exports({ q: v, qq: v }); }(10);
(function (v) { return exports({ p: p, pp: p }), v; }({ p = q = function (v) { return exports({ q: q, qq: q }), v; }( 20) } = {}));

// Function Assignment
function fn () {

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

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

// Class Declaration
class A {} exports({A: A, B: A});
class A {} exports({ A: A, B: A });

}
};
Expand Down

0 comments on commit 5f89362

Please sign in to comment.