Skip to content

Commit

Permalink
Super class expression parenthesis (#404)
Browse files Browse the repository at this point in the history
* Added parenthesis for some `superClass` expressions

* Simplify logic to enable super class expression parenthesis
  • Loading branch information
sanex3339 authored and michaelficarra committed Jan 10, 2020
1 parent 819a63b commit c917cf2
Show file tree
Hide file tree
Showing 2 changed files with 328 additions and 2 deletions.
4 changes: 2 additions & 2 deletions escodegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@
result = join(result, this.generateExpression(stmt.id, Precedence.Sequence, E_TTT));
}
if (stmt.superClass) {
fragment = join('extends', this.generateExpression(stmt.superClass, Precedence.Assignment, E_TTT));
fragment = join('extends', this.generateExpression(stmt.superClass, Precedence.Unary, E_TTT));
result = join(result, fragment);
}
result.push(space);
Expand Down Expand Up @@ -2091,7 +2091,7 @@
result = join(result, this.generateExpression(expr.id, Precedence.Sequence, E_TTT));
}
if (expr.superClass) {
fragment = join('extends', this.generateExpression(expr.superClass, Precedence.Assignment, E_TTT));
fragment = join('extends', this.generateExpression(expr.superClass, Precedence.Unary, E_TTT));
result = join(result, fragment);
}
result.push(space);
Expand Down
326 changes: 326 additions & 0 deletions test/harmony.js
Original file line number Diff line number Diff line change
Expand Up @@ -3016,6 +3016,128 @@ data = {
}
},

'class hello extends([]||[]){}': {
options: {
format: {
compact: true,
semicolons: false
}
},
generateFrom: {
type: 'Program',
body: [{
type: 'ClassDeclaration',
id: {
type: 'Identifier',
name: 'hello',
range: [6, 11],
loc: {
start: { line: 1, column: 6 },
end: { line: 1, column: 11 }
}
},
superClass: {
type: 'LogicalExpression',
start: 47,
end: 55,
left: {
type: 'ArrayExpression',
start: 47,
end: 49,
elements: []
},
operator: '||',
right: {
type: 'ArrayExpression',
start: 53,
end: 55,
elements: []
},
},
body: {
type: 'ClassBody',
body: [],
range: [21, 23],
loc: {
start: { line: 1, column: 21 },
end: { line: 1, column: 23 }
}
},
range: [0, 23],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 23 }
}
}],
range: [0, 23],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 23 }
}
}
},

'class hello extends([]=[]){}': {
options: {
format: {
compact: true,
semicolons: false
}
},
generateFrom: {
type: 'Program',
body: [{
type: 'ClassDeclaration',
id: {
type: 'Identifier',
name: 'hello',
range: [6, 11],
loc: {
start: { line: 1, column: 6 },
end: { line: 1, column: 11 }
}
},
superClass: {
type: 'AssignmentExpression',
start: 47,
end: 55,
left: {
type: 'ArrayExpression',
start: 47,
end: 49,
elements: []
},
operator: '=',
right: {
type: 'ArrayExpression',
start: 53,
end: 55,
elements: []
},
},
body: {
type: 'ClassBody',
body: [],
range: [21, 23],
loc: {
start: { line: 1, column: 21 },
end: { line: 1, column: 23 }
}
},
range: [0, 23],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 23 }
}
}],
range: [0, 23],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 23 }
}
}
},

'class hello extends[]{static[ok](){}}': {
options: {
format: {
Expand Down Expand Up @@ -4125,6 +4247,210 @@ data = {
}
},

'(class extends([]||[]){static[ok](){}})': {
options: {
format: {
compact: true,
semicolons: false
}
},
generateFrom: {
type: 'Program',
body: [{
type: 'ExpressionStatement',
expression: {
type: 'ClassExpression',
superClass: {
type: 'LogicalExpression',
start: 47,
end: 55,
left: {
type: 'ArrayExpression',
start: 47,
end: 49,
elements: []
},
operator: '||',
right: {
type: 'ArrayExpression',
start: 53,
end: 55,
elements: []
},
},
body: {
type: 'ClassBody',
body: [{
type: 'MethodDefinition',
key: {
type: 'Identifier',
name: 'ok',
range: [24, 26],
loc: {
start: { line: 1, column: 24 },
end: { line: 1, column: 26 }
}
},
value: {
type: 'FunctionExpression',
id: null,
params: [],
defaults: [],
body: {
type: 'BlockStatement',
body: [],
range: [29, 31],
loc: {
start: { line: 1, column: 29 },
end: { line: 1, column: 31 }
}
},
rest: null,
generator: false,
expression: false,
range: [29, 31],
loc: {
start: { line: 1, column: 29 },
end: { line: 1, column: 31 }
}
},
kind: '',
'static': true,
computed: true,
range: [17, 31],
loc: {
start: { line: 1, column: 17 },
end: { line: 1, column: 31 }
}
}],
range: [16, 32],
loc: {
start: { line: 1, column: 16 },
end: { line: 1, column: 32 }
}
},
range: [1, 32],
loc: {
start: { line: 1, column: 1 },
end: { line: 1, column: 32 }
}
},
range: [0, 33],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 33 }
}
}],
range: [0, 33],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 33 }
}
}
},

'(class extends([]=[]){static[ok](){}})': {
options: {
format: {
compact: true,
semicolons: false
}
},
generateFrom: {
type: 'Program',
body: [{
type: 'ExpressionStatement',
expression: {
type: 'ClassExpression',
superClass: {
type: 'AssignmentExpression',
start: 47,
end: 55,
left: {
type: 'ArrayExpression',
start: 47,
end: 49,
elements: []
},
operator: '=',
right: {
type: 'ArrayExpression',
start: 53,
end: 55,
elements: []
},
},
body: {
type: 'ClassBody',
body: [{
type: 'MethodDefinition',
key: {
type: 'Identifier',
name: 'ok',
range: [24, 26],
loc: {
start: { line: 1, column: 24 },
end: { line: 1, column: 26 }
}
},
value: {
type: 'FunctionExpression',
id: null,
params: [],
defaults: [],
body: {
type: 'BlockStatement',
body: [],
range: [29, 31],
loc: {
start: { line: 1, column: 29 },
end: { line: 1, column: 31 }
}
},
rest: null,
generator: false,
expression: false,
range: [29, 31],
loc: {
start: { line: 1, column: 29 },
end: { line: 1, column: 31 }
}
},
kind: '',
'static': true,
computed: true,
range: [17, 31],
loc: {
start: { line: 1, column: 17 },
end: { line: 1, column: 31 }
}
}],
range: [16, 32],
loc: {
start: { line: 1, column: 16 },
end: { line: 1, column: 32 }
}
},
range: [1, 32],
loc: {
start: { line: 1, column: 1 },
end: { line: 1, column: 32 }
}
},
range: [0, 33],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 33 }
}
}],
range: [0, 33],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 33 }
}
}
},

'(class extends"hello"{static[ok](){}})': {
options: {
format: {
Expand Down

0 comments on commit c917cf2

Please sign in to comment.