forked from rollup/rollup-docs-cn
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow functions to redeclare vars and functions in function scopes (#…
…5248)
- Loading branch information
1 parent
53d6360
commit 70b7b90
Showing
12 changed files
with
139 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { AstContext } from '../../Module'; | ||
import { logRedeclarationError } from '../../utils/logs'; | ||
import type Identifier from '../nodes/Identifier'; | ||
import type { ExpressionEntity } from '../nodes/shared/Expression'; | ||
import { VariableKind } from '../nodes/shared/VariableKinds'; | ||
import LocalVariable from '../variables/LocalVariable'; | ||
import ChildScope from './ChildScope'; | ||
import type ParameterScope from './ParameterScope'; | ||
|
||
export default class FunctionBodyScope extends ChildScope { | ||
constructor( | ||
readonly parent: ParameterScope, | ||
readonly context: AstContext | ||
) { | ||
super(parent, context); | ||
} | ||
|
||
// There is stuff that is only allowed in function scopes, i.e. functions can | ||
// be redeclared, functions and var can redeclare each other | ||
addDeclaration( | ||
identifier: Identifier, | ||
context: AstContext, | ||
init: ExpressionEntity, | ||
kind: VariableKind | ||
): LocalVariable { | ||
const name = identifier.name; | ||
const existingVariable = | ||
this.hoistedVariables?.get(name) || (this.variables.get(name) as LocalVariable); | ||
if (existingVariable) { | ||
const existingKind = existingVariable.kind; | ||
if ( | ||
(kind === VariableKind.var || kind === VariableKind.function) && | ||
(existingKind === VariableKind.var || | ||
existingKind === VariableKind.function || | ||
existingKind === VariableKind.parameter) | ||
) { | ||
existingVariable.addDeclaration(identifier, init); | ||
return existingVariable; | ||
} | ||
context.error(logRedeclarationError(name), identifier.start); | ||
} | ||
const newVariable = new LocalVariable(identifier.name, identifier, init, context, kind); | ||
this.variables.set(name, newVariable); | ||
return newVariable; | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
test/function/samples/ast-validations/redeclare-block-function-function/_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,24 @@ | ||
const path = require('node:path'); | ||
const ID_MAIN = path.join(__dirname, 'main.js'); | ||
|
||
module.exports = defineTest({ | ||
description: 'throws when redeclaring a function binding as a function in a block scope', | ||
error: { | ||
code: 'REDECLARATION_ERROR', | ||
frame: ` | ||
1: { | ||
2: function foo() {} | ||
3: function foo() {} | ||
^ | ||
4: }`, | ||
id: ID_MAIN, | ||
loc: { | ||
column: 10, | ||
file: ID_MAIN, | ||
line: 3 | ||
}, | ||
message: 'Identifier "foo" has already been declared', | ||
pos: 31, | ||
watchFiles: [ID_MAIN] | ||
} | ||
}); |
4 changes: 4 additions & 0 deletions
4
test/function/samples/ast-validations/redeclare-block-function-function/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,4 @@ | ||
{ | ||
function foo() {} | ||
function foo() {} | ||
} |
22 changes: 22 additions & 0 deletions
22
test/function/samples/ast-validations/redeclare-top-level-function-function/_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,22 @@ | ||
const path = require('node:path'); | ||
const ID_MAIN = path.join(__dirname, 'main.js'); | ||
|
||
module.exports = defineTest({ | ||
description: 'throws when redeclaring a top-level function binding as a function', | ||
error: { | ||
code: 'REDECLARATION_ERROR', | ||
frame: ` | ||
1: function foo() {} | ||
2: function foo() {} | ||
^`, | ||
id: ID_MAIN, | ||
loc: { | ||
column: 9, | ||
file: ID_MAIN, | ||
line: 2 | ||
}, | ||
message: 'Identifier "foo" has already been declared', | ||
pos: 27, | ||
watchFiles: [ID_MAIN] | ||
} | ||
}); |
2 changes: 2 additions & 0 deletions
2
test/function/samples/ast-validations/redeclare-top-level-function-function/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,2 @@ | ||
function foo() {} | ||
function foo() {} |
22 changes: 22 additions & 0 deletions
22
test/function/samples/ast-validations/redeclare-top-level-var-function/_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,22 @@ | ||
const path = require('node:path'); | ||
const ID_MAIN = path.join(__dirname, 'main.js'); | ||
|
||
module.exports = defineTest({ | ||
description: 'throws when redeclaring a top-level var binding as a function', | ||
error: { | ||
code: 'REDECLARATION_ERROR', | ||
frame: ` | ||
1: var foo; | ||
2: function foo() {} | ||
^`, | ||
id: ID_MAIN, | ||
loc: { | ||
column: 9, | ||
file: ID_MAIN, | ||
line: 2 | ||
}, | ||
message: 'Identifier "foo" has already been declared', | ||
pos: 18, | ||
watchFiles: [ID_MAIN] | ||
} | ||
}); |
2 changes: 2 additions & 0 deletions
2
test/function/samples/ast-validations/redeclare-top-level-var-function/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,2 @@ | ||
var foo; | ||
function foo() {} |
3 changes: 3 additions & 0 deletions
3
test/function/samples/ast-validations/redeclare-var-function/_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 = defineTest({ | ||
description: 'allows to redeclare vars and functions as vars and functions in function scopes' | ||
}); |
10 changes: 10 additions & 0 deletions
10
test/function/samples/ast-validations/redeclare-var-function/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,10 @@ | ||
function foo() { | ||
var fn = 1; | ||
function fn() {} | ||
function fn() {} | ||
var fn = 2; | ||
|
||
assert.equal(fn, 2); | ||
} | ||
|
||
foo(); |