Skip to content

Avoid effect of element access expression #39174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions src/compiler/transformers/esnext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace ts {
if (isLogicalOrCoalescingAssignmentExpression(binaryExpression)) {
return transformLogicalAssignment(binaryExpression);
}
// falls through
// falls through
default:
return visitEachChild(node, visitor, context);
}
Expand All @@ -37,32 +37,41 @@ namespace ts {
let left = skipParentheses(visitNode(binaryExpression.left, visitor, isLeftHandSideExpression));
let assignmentTarget = left;
const right = skipParentheses(visitNode(binaryExpression.right, visitor, isExpression));

if (isAccessExpression(left)) {
const tempVariable = factory.createTempVariable(hoistVariableDeclaration);
const propertyAccessTargetSimpleCopiable = isSimpleCopiableExpression(left.expression);
const propertyAccessTarget = propertyAccessTargetSimpleCopiable ? left.expression :
factory.createTempVariable(hoistVariableDeclaration);
const propertyAccessTargetAssignment = propertyAccessTargetSimpleCopiable ? left.expression : factory.createAssignment(
propertyAccessTarget,
left.expression
);

if (isPropertyAccessExpression(left)) {
assignmentTarget = factory.createPropertyAccessExpression(
tempVariable,
propertyAccessTarget,
left.name
);
left = factory.createPropertyAccessExpression(
factory.createAssignment(
tempVariable,
left.expression
),
propertyAccessTargetAssignment,
left.name
);
}
else {
const elementAccessArgumentSimpleCopiable = isSimpleCopiableExpression(left.argumentExpression);
const elementAccessArgument = elementAccessArgumentSimpleCopiable ? left.argumentExpression :
factory.createTempVariable(hoistVariableDeclaration);

assignmentTarget = factory.createElementAccessExpression(
tempVariable,
left.argumentExpression
propertyAccessTarget,
elementAccessArgument
);
left = factory.createElementAccessExpression(
factory.createAssignment(
tempVariable,
left.expression
),
left.argumentExpression
propertyAccessTargetAssignment,
elementAccessArgumentSimpleCopiable ? left.argumentExpression : factory.createAssignment(
elementAccessArgument,
left.argumentExpression
)
);
}
}
Expand Down
28 changes: 28 additions & 0 deletions tests/baselines/reference/logicalAssignment10(target=es2015).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [logicalAssignment10.ts]
var count = 0;
var obj = {};
function incr() {
return ++count;
}

const oobj = {
obj
}

obj[incr()] ??= incr();
oobj["obj"][incr()] ??= incr();


//// [logicalAssignment10.js]
var _a, _b;
var _c, _d, _e;
var count = 0;
var obj = {};
function incr() {
return ++count;
}
const oobj = {
obj
};
(_a = obj[_c = incr()]) !== null && _a !== void 0 ? _a : (obj[_c] = incr());
(_b = (_d = oobj["obj"])[_e = incr()]) !== null && _b !== void 0 ? _b : (_d[_e] = incr());
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment10.ts ===
var count = 0;
>count : Symbol(count, Decl(logicalAssignment10.ts, 0, 3))

var obj = {};
>obj : Symbol(obj, Decl(logicalAssignment10.ts, 1, 3))

function incr() {
>incr : Symbol(incr, Decl(logicalAssignment10.ts, 1, 13))

return ++count;
>count : Symbol(count, Decl(logicalAssignment10.ts, 0, 3))
}

const oobj = {
>oobj : Symbol(oobj, Decl(logicalAssignment10.ts, 6, 5))

obj
>obj : Symbol(obj, Decl(logicalAssignment10.ts, 6, 14))
}

obj[incr()] ??= incr();
>obj : Symbol(obj, Decl(logicalAssignment10.ts, 1, 3))
>incr : Symbol(incr, Decl(logicalAssignment10.ts, 1, 13))
>incr : Symbol(incr, Decl(logicalAssignment10.ts, 1, 13))

oobj["obj"][incr()] ??= incr();
>oobj : Symbol(oobj, Decl(logicalAssignment10.ts, 6, 5))
>"obj" : Symbol(obj, Decl(logicalAssignment10.ts, 6, 14))
>incr : Symbol(incr, Decl(logicalAssignment10.ts, 1, 13))
>incr : Symbol(incr, Decl(logicalAssignment10.ts, 1, 13))

45 changes: 45 additions & 0 deletions tests/baselines/reference/logicalAssignment10(target=es2015).types
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment10.ts ===
var count = 0;
>count : number
>0 : 0

var obj = {};
>obj : {}
>{} : {}

function incr() {
>incr : () => number

return ++count;
>++count : number
>count : number
}

const oobj = {
>oobj : { obj: {}; }
>{ obj} : { obj: {}; }

obj
>obj : {}
}

obj[incr()] ??= incr();
>obj[incr()] ??= incr() : any
>obj[incr()] : error
>obj : {}
>incr() : number
>incr : () => number
>incr() : number
>incr : () => number

oobj["obj"][incr()] ??= incr();
>oobj["obj"][incr()] ??= incr() : any
>oobj["obj"][incr()] : error
>oobj["obj"] : {}
>oobj : { obj: {}; }
>"obj" : "obj"
>incr() : number
>incr : () => number
>incr() : number
>incr : () => number

27 changes: 27 additions & 0 deletions tests/baselines/reference/logicalAssignment10(target=es2020).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [logicalAssignment10.ts]
var count = 0;
var obj = {};
function incr() {
return ++count;
}

const oobj = {
obj
}

obj[incr()] ??= incr();
oobj["obj"][incr()] ??= incr();


//// [logicalAssignment10.js]
var _a, _b, _c;
var count = 0;
var obj = {};
function incr() {
return ++count;
}
const oobj = {
obj
};
obj[_a = incr()] ?? (obj[_a] = incr());
(_b = oobj["obj"])[_c = incr()] ?? (_b[_c] = incr());
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment10.ts ===
var count = 0;
>count : Symbol(count, Decl(logicalAssignment10.ts, 0, 3))

var obj = {};
>obj : Symbol(obj, Decl(logicalAssignment10.ts, 1, 3))

function incr() {
>incr : Symbol(incr, Decl(logicalAssignment10.ts, 1, 13))

return ++count;
>count : Symbol(count, Decl(logicalAssignment10.ts, 0, 3))
}

const oobj = {
>oobj : Symbol(oobj, Decl(logicalAssignment10.ts, 6, 5))

obj
>obj : Symbol(obj, Decl(logicalAssignment10.ts, 6, 14))
}

obj[incr()] ??= incr();
>obj : Symbol(obj, Decl(logicalAssignment10.ts, 1, 3))
>incr : Symbol(incr, Decl(logicalAssignment10.ts, 1, 13))
>incr : Symbol(incr, Decl(logicalAssignment10.ts, 1, 13))

oobj["obj"][incr()] ??= incr();
>oobj : Symbol(oobj, Decl(logicalAssignment10.ts, 6, 5))
>"obj" : Symbol(obj, Decl(logicalAssignment10.ts, 6, 14))
>incr : Symbol(incr, Decl(logicalAssignment10.ts, 1, 13))
>incr : Symbol(incr, Decl(logicalAssignment10.ts, 1, 13))

45 changes: 45 additions & 0 deletions tests/baselines/reference/logicalAssignment10(target=es2020).types
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment10.ts ===
var count = 0;
>count : number
>0 : 0

var obj = {};
>obj : {}
>{} : {}

function incr() {
>incr : () => number

return ++count;
>++count : number
>count : number
}

const oobj = {
>oobj : { obj: {}; }
>{ obj} : { obj: {}; }

obj
>obj : {}
}

obj[incr()] ??= incr();
>obj[incr()] ??= incr() : any
>obj[incr()] : error
>obj : {}
>incr() : number
>incr : () => number
>incr() : number
>incr : () => number

oobj["obj"][incr()] ??= incr();
>oobj["obj"][incr()] ??= incr() : any
>oobj["obj"][incr()] : error
>oobj["obj"] : {}
>oobj : { obj: {}; }
>"obj" : "obj"
>incr() : number
>incr : () => number
>incr() : number
>incr : () => number

26 changes: 26 additions & 0 deletions tests/baselines/reference/logicalAssignment10(target=esnext).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [logicalAssignment10.ts]
var count = 0;
var obj = {};
function incr() {
return ++count;
}

const oobj = {
obj
}

obj[incr()] ??= incr();
oobj["obj"][incr()] ??= incr();


//// [logicalAssignment10.js]
var count = 0;
var obj = {};
function incr() {
return ++count;
}
const oobj = {
obj
};
obj[incr()] ??= incr();
oobj["obj"][incr()] ??= incr();
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment10.ts ===
var count = 0;
>count : Symbol(count, Decl(logicalAssignment10.ts, 0, 3))

var obj = {};
>obj : Symbol(obj, Decl(logicalAssignment10.ts, 1, 3))

function incr() {
>incr : Symbol(incr, Decl(logicalAssignment10.ts, 1, 13))

return ++count;
>count : Symbol(count, Decl(logicalAssignment10.ts, 0, 3))
}

const oobj = {
>oobj : Symbol(oobj, Decl(logicalAssignment10.ts, 6, 5))

obj
>obj : Symbol(obj, Decl(logicalAssignment10.ts, 6, 14))
}

obj[incr()] ??= incr();
>obj : Symbol(obj, Decl(logicalAssignment10.ts, 1, 3))
>incr : Symbol(incr, Decl(logicalAssignment10.ts, 1, 13))
>incr : Symbol(incr, Decl(logicalAssignment10.ts, 1, 13))

oobj["obj"][incr()] ??= incr();
>oobj : Symbol(oobj, Decl(logicalAssignment10.ts, 6, 5))
>"obj" : Symbol(obj, Decl(logicalAssignment10.ts, 6, 14))
>incr : Symbol(incr, Decl(logicalAssignment10.ts, 1, 13))
>incr : Symbol(incr, Decl(logicalAssignment10.ts, 1, 13))

45 changes: 45 additions & 0 deletions tests/baselines/reference/logicalAssignment10(target=esnext).types
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment10.ts ===
var count = 0;
>count : number
>0 : 0

var obj = {};
>obj : {}
>{} : {}

function incr() {
>incr : () => number

return ++count;
>++count : number
>count : number
}

const oobj = {
>oobj : { obj: {}; }
>{ obj} : { obj: {}; }

obj
>obj : {}
}

obj[incr()] ??= incr();
>obj[incr()] ??= incr() : any
>obj[incr()] : error
>obj : {}
>incr() : number
>incr : () => number
>incr() : number
>incr : () => number

oobj["obj"][incr()] ??= incr();
>oobj["obj"][incr()] ??= incr() : any
>oobj["obj"][incr()] : error
>oobj["obj"] : {}
>oobj : { obj: {}; }
>"obj" : "obj"
>incr() : number
>incr : () => number
>incr() : number
>incr : () => number

Loading