diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index f8b6e984a2..41083e722a 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -3959,6 +3959,21 @@ module ts { // NOTE(cyrusn): Some expressions may seem to be side effect free, but may // actually have side effects. For example, a binary + expression may cause // the toString method to be called on value, which may have side effects. + + // These are the set of syntactic productions which we know could definitely + // have side effects: + case SyntaxKind.CallExpression: + case SyntaxKind.NewExpression: + return true; + + case SyntaxKind.PropertyAccessExpression: + case SyntaxKind.ElementAccessExpression: + // Property/Element access might end up causing an accessor to run. As + // such, we have to assume there will be side effects. + return true; + + // These are the set of syntactic productions which we know definitely do not + // have side effects: case SyntaxKind.StringLiteral: case SyntaxKind.NoSubstitutionTemplateLiteral: case SyntaxKind.NumericLiteral: @@ -3975,6 +3990,8 @@ module ts { case SyntaxKind.SetAccessor: return false; + // These constructs may or may not have side effects depending on their + // constituent children. case SyntaxKind.ArrayBindingPattern: case SyntaxKind.ObjectBindingPattern: return forEach((node).elements, hasPossibleSideEffects); @@ -3987,16 +4004,9 @@ module ts { return hasPossibleSideEffects((node).name) || hasPossibleSideEffects((node).initializer); - case SyntaxKind.PropertyAccessExpression: - return hasPossibleSideEffects((node).expression); - case SyntaxKind.ArrayLiteralExpression: return forEach((node).elements, hasPossibleSideEffects); - case SyntaxKind.ElementAccessExpression: - return hasPossibleSideEffects((node).expression) || - hasPossibleSideEffects((node).argumentExpression); - case SyntaxKind.ParenthesizedExpression: return hasPossibleSideEffects((node).expression); diff --git a/tests/baselines/reference/functionWithDefaultParameterWithNoStatements11.js b/tests/baselines/reference/functionWithDefaultParameterWithNoStatements11.js index 54ec778105..3f0b2dab8e 100644 --- a/tests/baselines/reference/functionWithDefaultParameterWithNoStatements11.js +++ b/tests/baselines/reference/functionWithDefaultParameterWithNoStatements11.js @@ -8,6 +8,9 @@ function bar(a = v[0]) { //// [functionWithDefaultParameterWithNoStatements11.js] var v; -function foo(a) { } -function bar(a) { +function foo(a) { + if (a === void 0) { a = v[0]; } +} +function bar(a) { + if (a === void 0) { a = v[0]; } } diff --git a/tests/baselines/reference/functionWithDefaultParameterWithNoStatements9.js b/tests/baselines/reference/functionWithDefaultParameterWithNoStatements9.js index e982227560..81e6612958 100644 --- a/tests/baselines/reference/functionWithDefaultParameterWithNoStatements9.js +++ b/tests/baselines/reference/functionWithDefaultParameterWithNoStatements9.js @@ -5,6 +5,9 @@ function bar(a = console.log) { } //// [functionWithDefaultParameterWithNoStatements9.js] -function foo(a) { } -function bar(a) { +function foo(a) { + if (a === void 0) { a = console.log; } +} +function bar(a) { + if (a === void 0) { a = console.log; } }