Skip to content

Commit

Permalink
further refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jun 6, 2020
1 parent b35d6c5 commit 5d37459
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 52 deletions.
28 changes: 12 additions & 16 deletions src/ast/nodes/AssignmentExpression.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import MagicString from 'magic-string';
import {
findFirstOccurrenceOutsideComment,
RenderOptions,
WHITESPACE
} from '../../utils/renderHelpers';
import { findFirstOccurrenceOutsideComment, RenderOptions } from '../../utils/renderHelpers';
import { getSystemExportExpressionLeft } from '../../utils/systemJsRendering';
import { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import { EMPTY_PATH, ObjectPath, UNKNOWN_PATH } from '../utils/PathTracker';
Expand Down Expand Up @@ -66,14 +62,17 @@ export default class AssignmentExpression extends NodeBase {
this.left.end
);
const operation =
this.operator.length > 1 ? `${_}${exportNames[0]}${_}${this.operator.slice(0, -1)}` : '';
this.operator.length > 1 ? `${exportNames[0]}${_}${this.operator.slice(0, -1)}` : '';
const nextIsSpace =
operation.length === 0 && code.original[operatorPos + this.operator.length] === ' ';
const nextISNewline =
code.original[operatorPos + this.operator.length + (nextIsSpace ? 1 : 0)] === '\n';
code.overwrite(
operatorPos,
operatorPos + this.operator.length,
operatorPos + this.operator.length + (nextIsSpace ? 1 : 0),
`=${_}${getSystemExportExpressionLeft(
[this.left.variable!],
false,
!code.original[operatorPos + this.operator.length].match(WHITESPACE),
!nextISNewline,
options
)}${operation}`
);
Expand All @@ -82,14 +81,11 @@ export default class AssignmentExpression extends NodeBase {
const systemPatternExports: Variable[] = [];
this.left.addExportedVariables(systemPatternExports, options.exportNamesByVariable);
if (systemPatternExports.length > 0) {
const nextIsSpace = code.original[this.start] === ' ';
const nextIsNewline = code.original[this.start + (nextIsSpace ? 1 : 0)] === '\n';
code.prependRight(
this.start,
getSystemExportExpressionLeft(
systemPatternExports,
false,
!code.original[this.start + 1].match(WHITESPACE),
options
)
this.start + (nextIsSpace ? 1 : 0),
getSystemExportExpressionLeft(systemPatternExports, !nextIsNewline, options)
);
code.appendLeft(this.end, ')');
}
Expand Down
5 changes: 3 additions & 2 deletions src/ast/nodes/ExportDefaultDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import MagicString from 'magic-string';
import {
findFirstOccurrenceOutsideComment,
NodeRenderOptions,
RenderOptions,
WHITESPACE
RenderOptions
} from '../../utils/renderHelpers';
import { getSystemExportStatement } from '../../utils/systemJsRendering';
import { treeshakeNode } from '../../utils/treeshakeNode';
Expand All @@ -16,6 +15,8 @@ import Identifier from './Identifier';
import * as NodeType from './NodeType';
import { ExpressionNode, IncludeChildren, NodeBase } from './shared/Node';

const WHITESPACE = /\s/;

// The header ends at the first non-white-space after "default"
function getDeclarationStart(code: string, start: number) {
start = findFirstOccurrenceOutsideComment(code, 'default', start) + 7;
Expand Down
11 changes: 3 additions & 8 deletions src/ast/nodes/VariableDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
findFirstOccurrenceOutsideComment,
getCommaSeparatedNodesWithBoundaries,
NodeRenderOptions,
RenderOptions,
WHITESPACE
RenderOptions
} from '../../utils/renderHelpers';
import {
getSystemExportExpressionLeft,
Expand Down Expand Up @@ -212,15 +211,11 @@ export default class VariableDeclaration extends NodeBase {
);
const nextIsSpace = code.original[operatorPos + 1] === ' ';
const prependPos = operatorPos + 1 + (nextIsSpace ? 1 : 0);
const nextIsNl = code.original[prependPos] === '\n';
code.prependLeft(
prependPos,
(nextIsSpace ? '' : _) +
getSystemExportExpressionLeft(
[node.id.variable!],
true,
!code.original[prependPos].match(WHITESPACE),
options
)
getSystemExportExpressionLeft([node.id.variable!], !nextIsNl, options)
);
nextSeparatorString += ')';
}
Expand Down
2 changes: 0 additions & 2 deletions src/utils/renderHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ export interface NodeRenderOptions {

export const NO_SEMICOLON: NodeRenderOptions = { isNoStatement: true };

export const WHITESPACE = /\s/;

// This assumes there are only white-space and comments between start and the string we are looking for
export function findFirstOccurrenceOutsideComment(code: string, searchString: string, start = 0) {
let searchPos, charCodeAfterSlash;
Expand Down
14 changes: 2 additions & 12 deletions src/utils/systemJsRendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export function getSystemExportStatement(

export function getSystemExportExpressionLeft(
exportedVariables: Variable[],
exportsExpressionValue: boolean,
spaceForParamComma: boolean,
options: RenderOptions
): string {
Expand All @@ -39,23 +38,14 @@ export function getSystemExportExpressionLeft(
return `exports('${options.exportNamesByVariable.get(exportedVariables[0])}',${
spaceForParamComma ? _ : ''
}`;
} else if (exportsExpressionValue) {
} else {
return `function${_}(v)${_}{${_}return exports({${_}${exportedVariables
.map(variable => {
return options.exportNamesByVariable
.get(variable)!
.map(exportName => `${exportName}:${_}v`)
.join(`,${_}`);
})
.join(`,${_}`)}${_}})${s}${_}}(`;
} else {
return `function${_}(v)${_}{${_}return exports({ ${exportedVariables
.map(variable => {
return options.exportNamesByVariable
.get(variable)!
.map(exportName => `${exportName}:${_}${variable.getName()}`)
.join(`,${_}`);
})
.join(`,${_}`)} }),${_}v${s}${_}}(`;
.join(`,${_}`)}${_}}),${_}v${s}${_}}(`;
}
}
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) { return exports({ default: v, __moduleExports: v }); }(42);
var commonjs = function (v) { return exports({ default: v, __moduleExports: v }), 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) { return exports({ default: v, __moduleExports: v }); }({
var other = function (v) { return exports({ default: v, __moduleExports: v }), 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) { return exports({ foo: v, bar: v }); }(1);
const foo = function (v) { return exports({ foo: v, bar: v }), v; }(1);

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

const other = () => quux;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ System.register([], function (exports) {
let a, b, c;

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

}
};
Expand Down
14 changes: 7 additions & 7 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 = function (v) { return exports({ a: v, a2: v }); }(1), b = exports('b', 2), c = function (v) { return exports({ c: v, c2: v }); }(3);
let a = function (v) { return exports({ a: v, a2: v }), v; }(1), b = exports('b', 2), c = function (v) { return exports({ c: v, c2: v }), v; }(3);

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

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

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

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

// Function Assignment
function fn () {

}
fn = function (v) { return exports({ fn: fn, fn2: fn }), v; }( 5);
fn = function (v) { return exports({ fn: v, fn2: v }), 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 5d37459

Please sign in to comment.