Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correctly associate hoisted variables with parameters (#2456)
* Associated hoisted variables with parameters, use LocalVariables for parameters * Move test assertions into tested code * Test parameter defaults are consistently deconflicted
- Loading branch information
1 parent
1828166
commit 125e6ed
Showing
18 changed files
with
162 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,45 @@ | ||
import CallOptions from '../CallOptions'; | ||
import { ExecutionPathOptions } from '../ExecutionPathOptions'; | ||
import { ExpressionEntity } from '../nodes/shared/Expression'; | ||
import { EntityPathTracker } from '../utils/EntityPathTracker'; | ||
import ReplaceableInitializationVariable from './ReplaceableInitializationVariable'; | ||
import { LiteralValueOrUnknown, ObjectPath, UNKNOWN_EXPRESSION, UNKNOWN_VALUE } from '../values'; | ||
import LocalVariable from './LocalVariable'; | ||
|
||
export default class ThisVariable extends ReplaceableInitializationVariable { | ||
export default class ThisVariable extends LocalVariable { | ||
constructor(deoptimizationTracker: EntityPathTracker) { | ||
super('this', null, deoptimizationTracker); | ||
super('this', null, null, deoptimizationTracker); | ||
} | ||
|
||
getLiteralValueAtPath(): LiteralValueOrUnknown { | ||
return UNKNOWN_VALUE; | ||
} | ||
|
||
hasEffectsWhenAccessedAtPath(path: ObjectPath, options: ExecutionPathOptions) { | ||
return ( | ||
this._getInit(options).hasEffectsWhenAccessedAtPath(path, options) || | ||
super.hasEffectsWhenAccessedAtPath(path, options) | ||
); | ||
} | ||
|
||
hasEffectsWhenAssignedAtPath(path: ObjectPath, options: ExecutionPathOptions) { | ||
return ( | ||
this._getInit(options).hasEffectsWhenAssignedAtPath(path, options) || | ||
super.hasEffectsWhenAssignedAtPath(path, options) | ||
); | ||
} | ||
|
||
hasEffectsWhenCalledAtPath( | ||
path: ObjectPath, | ||
callOptions: CallOptions, | ||
options: ExecutionPathOptions | ||
) { | ||
return ( | ||
this._getInit(options).hasEffectsWhenCalledAtPath(path, callOptions, options) || | ||
super.hasEffectsWhenCalledAtPath(path, callOptions, options) | ||
); | ||
} | ||
|
||
_getInit(options: ExecutionPathOptions): ExpressionEntity { | ||
return options.getReplacedVariableInit(this) || UNKNOWN_EXPRESSION; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
test/function/samples/deconflict-parameter-defaults/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
description: 'consistently deconflict variable names for parameter defaults' | ||
}; |
27 changes: 27 additions & 0 deletions
27
test/function/samples/deconflict-parameter-defaults/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import './other.js'; | ||
|
||
var a = 'main'; | ||
|
||
function paramDefault(a, b = a) { | ||
assert.equal(b, 'param', 'main-param-b'); | ||
} | ||
paramDefault('param'); | ||
|
||
function outsideDefault(b = a) { | ||
assert.equal(b, 'main', 'main-outside-b'); | ||
} | ||
outsideDefault(); | ||
|
||
function paramDefaultRedeclare(a, b = a) { | ||
var a; | ||
assert.equal(a, 'param', 'main-param-redeclare-a'); | ||
assert.equal(b, 'param', 'main-param-redeclare-b'); | ||
} | ||
paramDefaultRedeclare('param'); | ||
|
||
function outsideDefaultRedeclare(b = a) { | ||
var a; | ||
assert.equal(a, undefined, 'main-outside-redeclare-a'); | ||
assert.equal(b, 'main', 'main-outside-redeclare-a'); | ||
} | ||
outsideDefaultRedeclare(); |
25 changes: 25 additions & 0 deletions
25
test/function/samples/deconflict-parameter-defaults/other.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var a = 'other'; | ||
|
||
function paramDefault(a, b = a) { | ||
assert.equal(b, 'param', 'other-param-b'); | ||
} | ||
paramDefault('param'); | ||
|
||
function outsideDefault(b = a) { | ||
assert.equal(b, 'other', 'other-outside-b'); | ||
} | ||
outsideDefault(); | ||
|
||
function paramDefaultRedeclare(a, b = a) { | ||
var a; | ||
assert.equal(a, 'param', 'other-param-redeclare-a'); | ||
assert.equal(b, 'param', 'other-param-redeclare-b'); | ||
} | ||
paramDefaultRedeclare('param'); | ||
|
||
function outsideDefaultRedeclare(b = a) { | ||
var a; | ||
assert.equal(a, undefined, 'other-outside-redeclare-a'); | ||
assert.equal(b, 'other', 'other-outside-redeclare-a'); | ||
} | ||
outsideDefaultRedeclare(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
description: 'associates redeclared parameters (#2451)' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
function fnDecl(a) { | ||
var a; | ||
if (!a) { | ||
throw Error("a was incorrectly assumed to be undefined in a function declaration"); | ||
} | ||
} | ||
fnDecl(true); | ||
|
||
export const fnExp = function(a) { | ||
var a; | ||
if (!a) { | ||
throw Error("a was incorrectly assumed to be undefined in a function expression"); | ||
} | ||
}; | ||
fnExp(true); | ||
|
||
export const arrowFn = a => { | ||
var a; | ||
if (!a) { | ||
throw Error("a was incorrectly assumed to be undefined in an arrow function"); | ||
} | ||
}; | ||
arrowFn(true); |