Skip to content

Commit

Permalink
Export updated assignments when using shorthand update assignment exp…
Browse files Browse the repository at this point in the history
…ressions in SystemJS (#3242)
  • Loading branch information
lukastaegert committed Nov 18, 2019
1 parent 0697166 commit 6dcc4e0
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 8 deletions.
18 changes: 14 additions & 4 deletions src/ast/nodes/AssignmentExpression.ts
@@ -1,5 +1,5 @@
import MagicString from 'magic-string';
import { RenderOptions } from '../../utils/renderHelpers';
import { findFirstOccurrenceOutsideComment, RenderOptions } from '../../utils/renderHelpers';
import { getSystemExportStatement } from '../../utils/systemJsRendering';
import { HasEffectsContext } from '../ExecutionContext';
import { EMPTY_PATH, ObjectPath, UNKNOWN_PATH } from '../utils/PathTracker';
Expand Down Expand Up @@ -51,9 +51,19 @@ export default class AssignmentExpression extends NodeBase {
this.right.render(code, options);
if (options.format === 'system') {
if (this.left.variable && this.left.variable.exportName) {
code.prependLeft(
code.original.indexOf('=', this.left.end) + 1,
` exports('${this.left.variable.exportName}',`
const operatorPos = findFirstOccurrenceOutsideComment(
code.original,
this.operator,
this.left.end
);
const operation =
this.operator.length > 1
? ` ${this.left.variable.exportName} ${this.operator.slice(0, -1)}`
: '';
code.overwrite(
operatorPos,
operatorPos + this.operator.length,
`= exports('${this.left.variable.exportName}',${operation}`
);
code.appendLeft(this.right.end, `)`);
} else if ('addExportedVariables' in this.left) {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/renderHelpers.ts
Expand Up @@ -30,7 +30,7 @@ export function findFirstOccurrenceOutsideComment(code: string, searchString: st
searchPos = code.indexOf(searchString, start);
while (true) {
start = code.indexOf('/', start);
if (start === -1 || start > searchPos) return searchPos;
if (start === -1 || start >= searchPos) return searchPos;
charCodeAfterSlash = code.charCodeAt(++start);
++start;

Expand Down Expand Up @@ -116,13 +116,13 @@ export function getCommaSeparatedNodesWithBoundaries<N extends Node>(
code: MagicString,
start: number,
end: number
): ({
): {
contentEnd: number;
end: number;
node: N;
separator: number | null;
start: number;
})[] {
}[] {
const splitUpNodes = [];
let node, nextNode, nextNodeStart, contentEnd, char;
let separator = start - 1;
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/export-live-bindings/_expected/system.js
Expand Up @@ -10,7 +10,7 @@ System.register('iife', [], function (exports) {
});

function update () {
foo += exports('foo', 10);
foo = exports('foo', foo + 10);
}

let foo = exports('foo', 10);
Expand Down
4 changes: 4 additions & 0 deletions test/form/samples/updating-assignments/_config.js
@@ -0,0 +1,4 @@
module.exports = {
description: 'Correctly handles assignment operators on exports',
options: { output: { name: 'bundle' } }
};
20 changes: 20 additions & 0 deletions test/form/samples/updating-assignments/_expected/amd.js
@@ -0,0 +1,20 @@
define(['exports'], function (exports) { 'use strict';

exports.x = 1;
exports.x = 2;
exports.x += 1;
exports.x -= 1;
exports.x *= 2;
exports.x /= 2;
exports.x %= 2;
exports.x **= 2;
exports.x <<= 1;
exports.x >>= 1;
exports.x >>>= 1;
exports.x &= 3;
exports.x ^= 2;
exports.x |= 2;

Object.defineProperty(exports, '__esModule', { value: true });

});
18 changes: 18 additions & 0 deletions test/form/samples/updating-assignments/_expected/cjs.js
@@ -0,0 +1,18 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

exports.x = 1;
exports.x = 2;
exports.x += 1;
exports.x -= 1;
exports.x *= 2;
exports.x /= 2;
exports.x %= 2;
exports.x **= 2;
exports.x <<= 1;
exports.x >>= 1;
exports.x >>>= 1;
exports.x &= 3;
exports.x ^= 2;
exports.x |= 2;
16 changes: 16 additions & 0 deletions test/form/samples/updating-assignments/_expected/es.js
@@ -0,0 +1,16 @@
let x = 1;
x = 2;
x += 1;
x -= 1;
x *= 2;
x /= 2;
x %= 2;
x **= 2;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= 3;
x ^= 2;
x |= 2;

export { x };
21 changes: 21 additions & 0 deletions test/form/samples/updating-assignments/_expected/iife.js
@@ -0,0 +1,21 @@
var bundle = (function (exports) {
'use strict';

exports.x = 1;
exports.x = 2;
exports.x += 1;
exports.x -= 1;
exports.x *= 2;
exports.x /= 2;
exports.x %= 2;
exports.x **= 2;
exports.x <<= 1;
exports.x >>= 1;
exports.x >>>= 1;
exports.x &= 3;
exports.x ^= 2;
exports.x |= 2;

return exports;

}({}));
23 changes: 23 additions & 0 deletions test/form/samples/updating-assignments/_expected/system.js
@@ -0,0 +1,23 @@
System.register('bundle', [], function (exports) {
'use strict';
return {
execute: function () {

let x = exports('x', 1);
x = exports('x', 2);
x = exports('x', x + 1);
x = exports('x', x - 1);
x = exports('x', x * 2);
x = exports('x', x / 2);
x = exports('x', x % 2);
x = exports('x', x ** 2);
x = exports('x', x << 1);
x = exports('x', x >> 1);
x = exports('x', x >>> 1);
x = exports('x', x & 3);
x = exports('x', x ^ 2);
x = exports('x', x | 2);

}
};
});
24 changes: 24 additions & 0 deletions test/form/samples/updating-assignments/_expected/umd.js
@@ -0,0 +1,24 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.bundle = {}));
}(this, (function (exports) { 'use strict';

exports.x = 1;
exports.x = 2;
exports.x += 1;
exports.x -= 1;
exports.x *= 2;
exports.x /= 2;
exports.x %= 2;
exports.x **= 2;
exports.x <<= 1;
exports.x >>= 1;
exports.x >>>= 1;
exports.x &= 3;
exports.x ^= 2;
exports.x |= 2;

Object.defineProperty(exports, '__esModule', { value: true });

})));
14 changes: 14 additions & 0 deletions test/form/samples/updating-assignments/main.js
@@ -0,0 +1,14 @@
export let x = 1;
x = 2;
x += 1;
x -= 1;
x *= 2;
x /= 2;
x %= 2;
x **= 2;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= 3;
x ^= 2;
x |= 2;

0 comments on commit 6dcc4e0

Please sign in to comment.