Skip to content

Commit c917cf2

Browse files
sanex3339michaelficarra
authored andcommitted
Super class expression parenthesis (estools#404)
* Added parenthesis for some `superClass` expressions * Simplify logic to enable super class expression parenthesis
1 parent 819a63b commit c917cf2

File tree

2 files changed

+328
-2
lines changed

2 files changed

+328
-2
lines changed

escodegen.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@
11451145
result = join(result, this.generateExpression(stmt.id, Precedence.Sequence, E_TTT));
11461146
}
11471147
if (stmt.superClass) {
1148-
fragment = join('extends', this.generateExpression(stmt.superClass, Precedence.Assignment, E_TTT));
1148+
fragment = join('extends', this.generateExpression(stmt.superClass, Precedence.Unary, E_TTT));
11491149
result = join(result, fragment);
11501150
}
11511151
result.push(space);
@@ -2091,7 +2091,7 @@
20912091
result = join(result, this.generateExpression(expr.id, Precedence.Sequence, E_TTT));
20922092
}
20932093
if (expr.superClass) {
2094-
fragment = join('extends', this.generateExpression(expr.superClass, Precedence.Assignment, E_TTT));
2094+
fragment = join('extends', this.generateExpression(expr.superClass, Precedence.Unary, E_TTT));
20952095
result = join(result, fragment);
20962096
}
20972097
result.push(space);

test/harmony.js

Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,6 +3016,128 @@ data = {
30163016
}
30173017
},
30183018

3019+
'class hello extends([]||[]){}': {
3020+
options: {
3021+
format: {
3022+
compact: true,
3023+
semicolons: false
3024+
}
3025+
},
3026+
generateFrom: {
3027+
type: 'Program',
3028+
body: [{
3029+
type: 'ClassDeclaration',
3030+
id: {
3031+
type: 'Identifier',
3032+
name: 'hello',
3033+
range: [6, 11],
3034+
loc: {
3035+
start: { line: 1, column: 6 },
3036+
end: { line: 1, column: 11 }
3037+
}
3038+
},
3039+
superClass: {
3040+
type: 'LogicalExpression',
3041+
start: 47,
3042+
end: 55,
3043+
left: {
3044+
type: 'ArrayExpression',
3045+
start: 47,
3046+
end: 49,
3047+
elements: []
3048+
},
3049+
operator: '||',
3050+
right: {
3051+
type: 'ArrayExpression',
3052+
start: 53,
3053+
end: 55,
3054+
elements: []
3055+
},
3056+
},
3057+
body: {
3058+
type: 'ClassBody',
3059+
body: [],
3060+
range: [21, 23],
3061+
loc: {
3062+
start: { line: 1, column: 21 },
3063+
end: { line: 1, column: 23 }
3064+
}
3065+
},
3066+
range: [0, 23],
3067+
loc: {
3068+
start: { line: 1, column: 0 },
3069+
end: { line: 1, column: 23 }
3070+
}
3071+
}],
3072+
range: [0, 23],
3073+
loc: {
3074+
start: { line: 1, column: 0 },
3075+
end: { line: 1, column: 23 }
3076+
}
3077+
}
3078+
},
3079+
3080+
'class hello extends([]=[]){}': {
3081+
options: {
3082+
format: {
3083+
compact: true,
3084+
semicolons: false
3085+
}
3086+
},
3087+
generateFrom: {
3088+
type: 'Program',
3089+
body: [{
3090+
type: 'ClassDeclaration',
3091+
id: {
3092+
type: 'Identifier',
3093+
name: 'hello',
3094+
range: [6, 11],
3095+
loc: {
3096+
start: { line: 1, column: 6 },
3097+
end: { line: 1, column: 11 }
3098+
}
3099+
},
3100+
superClass: {
3101+
type: 'AssignmentExpression',
3102+
start: 47,
3103+
end: 55,
3104+
left: {
3105+
type: 'ArrayExpression',
3106+
start: 47,
3107+
end: 49,
3108+
elements: []
3109+
},
3110+
operator: '=',
3111+
right: {
3112+
type: 'ArrayExpression',
3113+
start: 53,
3114+
end: 55,
3115+
elements: []
3116+
},
3117+
},
3118+
body: {
3119+
type: 'ClassBody',
3120+
body: [],
3121+
range: [21, 23],
3122+
loc: {
3123+
start: { line: 1, column: 21 },
3124+
end: { line: 1, column: 23 }
3125+
}
3126+
},
3127+
range: [0, 23],
3128+
loc: {
3129+
start: { line: 1, column: 0 },
3130+
end: { line: 1, column: 23 }
3131+
}
3132+
}],
3133+
range: [0, 23],
3134+
loc: {
3135+
start: { line: 1, column: 0 },
3136+
end: { line: 1, column: 23 }
3137+
}
3138+
}
3139+
},
3140+
30193141
'class hello extends[]{static[ok](){}}': {
30203142
options: {
30213143
format: {
@@ -4125,6 +4247,210 @@ data = {
41254247
}
41264248
},
41274249

4250+
'(class extends([]||[]){static[ok](){}})': {
4251+
options: {
4252+
format: {
4253+
compact: true,
4254+
semicolons: false
4255+
}
4256+
},
4257+
generateFrom: {
4258+
type: 'Program',
4259+
body: [{
4260+
type: 'ExpressionStatement',
4261+
expression: {
4262+
type: 'ClassExpression',
4263+
superClass: {
4264+
type: 'LogicalExpression',
4265+
start: 47,
4266+
end: 55,
4267+
left: {
4268+
type: 'ArrayExpression',
4269+
start: 47,
4270+
end: 49,
4271+
elements: []
4272+
},
4273+
operator: '||',
4274+
right: {
4275+
type: 'ArrayExpression',
4276+
start: 53,
4277+
end: 55,
4278+
elements: []
4279+
},
4280+
},
4281+
body: {
4282+
type: 'ClassBody',
4283+
body: [{
4284+
type: 'MethodDefinition',
4285+
key: {
4286+
type: 'Identifier',
4287+
name: 'ok',
4288+
range: [24, 26],
4289+
loc: {
4290+
start: { line: 1, column: 24 },
4291+
end: { line: 1, column: 26 }
4292+
}
4293+
},
4294+
value: {
4295+
type: 'FunctionExpression',
4296+
id: null,
4297+
params: [],
4298+
defaults: [],
4299+
body: {
4300+
type: 'BlockStatement',
4301+
body: [],
4302+
range: [29, 31],
4303+
loc: {
4304+
start: { line: 1, column: 29 },
4305+
end: { line: 1, column: 31 }
4306+
}
4307+
},
4308+
rest: null,
4309+
generator: false,
4310+
expression: false,
4311+
range: [29, 31],
4312+
loc: {
4313+
start: { line: 1, column: 29 },
4314+
end: { line: 1, column: 31 }
4315+
}
4316+
},
4317+
kind: '',
4318+
'static': true,
4319+
computed: true,
4320+
range: [17, 31],
4321+
loc: {
4322+
start: { line: 1, column: 17 },
4323+
end: { line: 1, column: 31 }
4324+
}
4325+
}],
4326+
range: [16, 32],
4327+
loc: {
4328+
start: { line: 1, column: 16 },
4329+
end: { line: 1, column: 32 }
4330+
}
4331+
},
4332+
range: [1, 32],
4333+
loc: {
4334+
start: { line: 1, column: 1 },
4335+
end: { line: 1, column: 32 }
4336+
}
4337+
},
4338+
range: [0, 33],
4339+
loc: {
4340+
start: { line: 1, column: 0 },
4341+
end: { line: 1, column: 33 }
4342+
}
4343+
}],
4344+
range: [0, 33],
4345+
loc: {
4346+
start: { line: 1, column: 0 },
4347+
end: { line: 1, column: 33 }
4348+
}
4349+
}
4350+
},
4351+
4352+
'(class extends([]=[]){static[ok](){}})': {
4353+
options: {
4354+
format: {
4355+
compact: true,
4356+
semicolons: false
4357+
}
4358+
},
4359+
generateFrom: {
4360+
type: 'Program',
4361+
body: [{
4362+
type: 'ExpressionStatement',
4363+
expression: {
4364+
type: 'ClassExpression',
4365+
superClass: {
4366+
type: 'AssignmentExpression',
4367+
start: 47,
4368+
end: 55,
4369+
left: {
4370+
type: 'ArrayExpression',
4371+
start: 47,
4372+
end: 49,
4373+
elements: []
4374+
},
4375+
operator: '=',
4376+
right: {
4377+
type: 'ArrayExpression',
4378+
start: 53,
4379+
end: 55,
4380+
elements: []
4381+
},
4382+
},
4383+
body: {
4384+
type: 'ClassBody',
4385+
body: [{
4386+
type: 'MethodDefinition',
4387+
key: {
4388+
type: 'Identifier',
4389+
name: 'ok',
4390+
range: [24, 26],
4391+
loc: {
4392+
start: { line: 1, column: 24 },
4393+
end: { line: 1, column: 26 }
4394+
}
4395+
},
4396+
value: {
4397+
type: 'FunctionExpression',
4398+
id: null,
4399+
params: [],
4400+
defaults: [],
4401+
body: {
4402+
type: 'BlockStatement',
4403+
body: [],
4404+
range: [29, 31],
4405+
loc: {
4406+
start: { line: 1, column: 29 },
4407+
end: { line: 1, column: 31 }
4408+
}
4409+
},
4410+
rest: null,
4411+
generator: false,
4412+
expression: false,
4413+
range: [29, 31],
4414+
loc: {
4415+
start: { line: 1, column: 29 },
4416+
end: { line: 1, column: 31 }
4417+
}
4418+
},
4419+
kind: '',
4420+
'static': true,
4421+
computed: true,
4422+
range: [17, 31],
4423+
loc: {
4424+
start: { line: 1, column: 17 },
4425+
end: { line: 1, column: 31 }
4426+
}
4427+
}],
4428+
range: [16, 32],
4429+
loc: {
4430+
start: { line: 1, column: 16 },
4431+
end: { line: 1, column: 32 }
4432+
}
4433+
},
4434+
range: [1, 32],
4435+
loc: {
4436+
start: { line: 1, column: 1 },
4437+
end: { line: 1, column: 32 }
4438+
}
4439+
},
4440+
range: [0, 33],
4441+
loc: {
4442+
start: { line: 1, column: 0 },
4443+
end: { line: 1, column: 33 }
4444+
}
4445+
}],
4446+
range: [0, 33],
4447+
loc: {
4448+
start: { line: 1, column: 0 },
4449+
end: { line: 1, column: 33 }
4450+
}
4451+
}
4452+
},
4453+
41284454
'(class extends"hello"{static[ok](){}})': {
41294455
options: {
41304456
format: {

0 commit comments

Comments
 (0)