Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4afd207

Browse files
authored
Interepret parserallow* options as top level only (#17203)
* fix: set top level await when AllowAwaitOutsideFunction is true* fix: set top level return when AllowReturnOutsideFunction is true* fix: set top level super when AllowSuperOutsideMethod is true* refactor: simplify new.targe scope check* rename test suites* fix: do not construct invalid js in template* Revert "fix: set top level super when AllowSuperOutsideMethod is true"This reverts commit9a256c7.# Conflicts:#packages/babel-parser/src/parser/util.ts#packages/babel-parser/test/fixtures/core/opts/allowSuperOutsideMethod-true-invalid-in-function-body/input.js#packages/babel-parser/test/fixtures/core/opts/allowSuperOutsideMethod-true-invalid-in-function-body/options.json#packages/babel-parser/test/fixtures/core/opts/allowSuperOutsideMethod-true-invalid-in-function-body/output.json* polish: overwrite await using in async errorwhen await using is in ambient context* add async-do allowReturnOutsideFunction test* temporarily disable prettier async-do-expressions test
1 parent7428811 commit4afd207

File tree

18 files changed

+200
-45
lines changed

18 files changed

+200
-45
lines changed

‎packages/babel-parser/src/parser/expression.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,11 +1893,7 @@ export default abstract class ExpressionParser extends LValParser {
18931893
"target",
18941894
);
18951895

1896-
if(
1897-
!this.scope.inNonArrowFunction&&
1898-
!this.scope.inClass&&
1899-
!(this.optionFlags&OptionFlags.AllowNewTargetOutsideFunction)
1900-
){
1896+
if(!this.scope.allowNewTarget){
19011897
this.raise(Errors.UnexpectedNewTarget,metaProp);
19021898
}
19031899

@@ -2869,10 +2865,7 @@ export default abstract class ExpressionParser extends LValParser {
28692865
// Returns whether `await` is allowed or not in this context, and if it is
28702866
// keeps track of it to determine whether a module uses top-level await.
28712867
recordAwaitIfAllowed():boolean{
2872-
constisAwaitAllowed=
2873-
this.prodParam.hasAwait||
2874-
(this.optionFlags&OptionFlags.AllowAwaitOutsideFunction&&
2875-
!this.scope.inFunction);
2868+
constisAwaitAllowed=this.prodParam.hasAwait;
28762869

28772870
if(isAwaitAllowed&&!this.scope.inFunction){
28782871
this.state.hasTopLevelAwait=true;

‎packages/babel-parser/src/parser/statement.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,10 +1090,7 @@ export default abstract class StatementParser extends ExpressionParser {
10901090
}
10911091

10921092
parseReturnStatement(this:Parser,node:Undone<N.ReturnStatement>){
1093-
if(
1094-
!this.prodParam.hasReturn&&
1095-
!(this.optionFlags&OptionFlags.AllowReturnOutsideFunction)
1096-
){
1093+
if(!this.prodParam.hasReturn){
10971094
this.raise(Errors.IllegalReturn,this.state.startLoc);
10981095
}
10991096

‎packages/babel-parser/src/parser/util.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,23 @@ export default abstract class UtilParser extends Tokenizer {
356356

357357
enterInitialScopes(){
358358
letparamFlags=ParamKind.PARAM;
359-
if(this.inModule){
359+
if(
360+
this.inModule||
361+
this.optionFlags&OptionFlags.AllowAwaitOutsideFunction
362+
){
360363
paramFlags|=ParamKind.PARAM_AWAIT;
361364
}
362365
if(this.optionFlags&OptionFlags.AllowYieldOutsideFunction){
363366
paramFlags|=ParamKind.PARAM_YIELD;
364367
}
365-
this.scope.enter(ScopeFlag.PROGRAM);
368+
if(this.optionFlags&OptionFlags.AllowReturnOutsideFunction){
369+
paramFlags|=ParamKind.PARAM_RETURN;
370+
}
371+
letscopeFlags=ScopeFlag.PROGRAM;
372+
if(this.optionFlags&OptionFlags.AllowNewTargetOutsideFunction){
373+
scopeFlags|=ScopeFlag.NEW_TARGET;
374+
}
375+
this.scope.enter(scopeFlags);
366376
this.prodParam.enter(paramFlags);
367377
}
368378

‎packages/babel-parser/src/plugins/typescript/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3135,7 +3135,11 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
31353135

31363136
// If node.declare is true, the error has already been raised in tsTryParseDeclare.
31373137
if(!node.declare&&(kind==="using"||kind==="await using")){
3138-
this.raise(TSErrors.UsingDeclarationInAmbientContext,node,kind);
3138+
this.raiseOverwrite(
3139+
TSErrors.UsingDeclarationInAmbientContext,
3140+
node,
3141+
kind,
3142+
);
31393143
returndeclaration;
31403144
}
31413145

‎packages/babel-parser/src/util/scope.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,41 @@ export default class ScopeHandler<IScope extends Scope = Scope> {
4141
return(this.currentScope().flags&ScopeFlag.PROGRAM)>0;
4242
}
4343
getinFunction(){
44-
return(this.currentVarScopeFlags()&ScopeFlag.FUNCTION)>0;
44+
return(this.currentVarScopeFlags()&ScopeFlag.FUNCTION_BASE)>0;
4545
}
4646
getallowSuper(){
4747
return(this.currentThisScopeFlags()&ScopeFlag.SUPER)>0;
4848
}
4949
getallowDirectSuper(){
5050
return(this.currentThisScopeFlags()&ScopeFlag.DIRECT_SUPER)>0;
5151
}
52+
getallowNewTarget(){
53+
return(this.currentThisScopeFlags()&ScopeFlag.NEW_TARGET)>0;
54+
}
5255
getinClass(){
53-
return(this.currentThisScopeFlags()&ScopeFlag.CLASS)>0;
56+
return(this.currentThisScopeFlags()&ScopeFlag.CLASS_BASE)>0;
5457
}
5558
getinClassAndNotInNonArrowFunction(){
5659
constflags=this.currentThisScopeFlags();
57-
return(flags&ScopeFlag.CLASS)>0&&(flags&ScopeFlag.FUNCTION)===0;
60+
return(
61+
(flags&ScopeFlag.CLASS_BASE)>0&&
62+
(flags&ScopeFlag.FUNCTION_BASE)===0
63+
);
5864
}
5965
getinStaticBlock(){
6066
for(leti=this.scopeStack.length-1;;i--){
6167
const{ flags}=this.scopeStack[i];
6268
if(flags&ScopeFlag.STATIC_BLOCK){
6369
returntrue;
6470
}
65-
if(flags&(ScopeFlag.VAR|ScopeFlag.CLASS)){
71+
if(flags&(ScopeFlag.VAR|ScopeFlag.CLASS_BASE)){
6672
// function body, module body, class property initializers
6773
returnfalse;
6874
}
6975
}
7076
}
7177
getinNonArrowFunction(){
72-
return(this.currentThisScopeFlags()&ScopeFlag.FUNCTION)>0;
78+
return(this.currentThisScopeFlags()&ScopeFlag.FUNCTION_BASE)>0;
7379
}
7480
getinBareCaseStatement(){
7581
return(this.currentScope().flags&ScopeFlag.SWITCH)>0;
@@ -98,7 +104,7 @@ export default class ScopeHandler<IScope extends Scope = Scope> {
98104
// > treated like var declarations rather than like lexical declarations.
99105
treatFunctionsAsVarInScope(scope:IScope):boolean{
100106
return!!(
101-
scope.flags&(ScopeFlag.FUNCTION|ScopeFlag.STATIC_BLOCK)||
107+
scope.flags&(ScopeFlag.FUNCTION_BASE|ScopeFlag.STATIC_BLOCK)||
102108
(!this.parser.inModule&&scope.flags&ScopeFlag.PROGRAM)
103109
);
104110
}
@@ -220,7 +226,7 @@ export default class ScopeHandler<IScope extends Scope = Scope> {
220226
for(leti=this.scopeStack.length-1;;i--){
221227
const{ flags}=this.scopeStack[i];
222228
if(
223-
flags&(ScopeFlag.VAR|ScopeFlag.CLASS)&&
229+
flags&(ScopeFlag.VAR|ScopeFlag.CLASS_BASE)&&
224230
!(flags&ScopeFlag.ARROW)
225231
){
226232
returnflags;

‎packages/babel-parser/src/util/scopeflags.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
// Each scope gets a bitset that may contain these flags
22
/* prettier-ignore */
33
exportconstenumScopeFlag{
4-
OTHER=0b0000000000,
5-
PROGRAM=0b0000000001,
6-
FUNCTION=0b0000000010,
7-
ARROW=0b0000000100,
8-
SIMPLE_CATCH=0b0000001000,
9-
SUPER=0b0000010000,
10-
DIRECT_SUPER=0b0000100000,
11-
CLASS=0b0001000000,
12-
STATIC_BLOCK=0b0010000000,
13-
SWITCH=0b0100000000,
14-
TS_MODULE=0b1000000000,
15-
VAR=PROGRAM|FUNCTION|STATIC_BLOCK|TS_MODULE,
4+
OTHER=0b00000000000,
5+
PROGRAM=0b00000000001,
6+
FUNCTION_BASE=0b00000000010,
7+
ARROW=0b00000000100,
8+
SIMPLE_CATCH=0b00000001000,
9+
SUPER=0b00000010000,
10+
DIRECT_SUPER=0b00000100000,
11+
CLASS_BASE=0b00001000000,
12+
STATIC_BLOCK=0b00010000000,
13+
SWITCH=0b00100000000,
14+
NEW_TARGET=0b01000000000,
15+
TS_MODULE=0b10000000000,
16+
FUNCTION=FUNCTION_BASE|NEW_TARGET,
17+
CLASS=CLASS_BASE|NEW_TARGET,
18+
VAR=PROGRAM|FUNCTION|STATIC_BLOCK|TS_MODULE,
1619
}
1720

1821
/* prettier-ignore */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
classC{
2+
static{
3+
forawait(constiofimports){}
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"allowAwaitOutsideFunction":true,
3+
"throws":"Unexpected token, expected\"(\" (3:8)"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
classC{
2+
static{
3+
return0;
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"allowReturnOutsideFunction":true
3+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp