Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions esprima/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Messages:
InvalidLHSInForLoop = "Invalid left-hand side in for-loop"
InvalidModuleSpecifier = "Unexpected token"
InvalidRegExp = "Invalid regular expression"
InvalidTaggedTemplateOnOptionalChain = 'Invalid tagged template on optional chain'
LetInLexicalBinding = "let is disallowed as a lexically bound name"
MissingFromClause = "Unexpected token"
MultipleDefaultsInSwitch = "More than one default clause in switch statement"
Expand Down
14 changes: 11 additions & 3 deletions esprima/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ def __init__(self, label):


class CallExpression(Node):
def __init__(self, callee, args):
def __init__(self, callee, args, optional):
self.type = Syntax.CallExpression
self.callee = callee
self.arguments = args
self.optional = optional


class CatchClause(Node):
Expand All @@ -150,6 +151,11 @@ def __init__(self, param, body):
self.param = param
self.body = body

class ChainExpression(Node):
def __init__(self, expression):
self.type = Syntax.ChainExpression
self.expression = expression


class ClassBody(Node):
def __init__(self, body):
Expand All @@ -174,11 +180,12 @@ def __init__(self, id, superClass, body):


class ComputedMemberExpression(Node):
def __init__(self, object, property):
def __init__(self, object, property, optional):
self.type = Syntax.MemberExpression
self.computed = True
self.object = object
self.property = property
self.optional = optional


class ConditionalExpression(Node):
Expand Down Expand Up @@ -472,11 +479,12 @@ def __init__(self, argument):


class StaticMemberExpression(Node):
def __init__(self, object, property):
def __init__(self, object, property, optional):
self.type = Syntax.MemberExpression
self.computed = False
self.object = object
self.property = property
self.optional = optional


class Super(Node):
Expand Down
71 changes: 52 additions & 19 deletions esprima/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,15 +1095,15 @@ def parseLeftHandSideExpressionAllowCall(self):
else:
expr = self.inheritCoverGrammar(self.parseNewExpression if self.matchKeyword('new') else self.parsePrimaryExpression)

hasOptional = False
while True:
if self.match('.'):
self.context.isBindingElement = False
self.context.isAssignmentTarget = True
self.expect('.')
property = self.parseIdentifierName()
expr = self.finalize(self.startNode(startToken), Node.StaticMemberExpression(expr, property))
optional = False
if self.match('?.'):
optional = True
hasOptional = True
self.expect('?.')

elif self.match('('):
if self.match('('):
asyncArrow = maybeAsync and (startToken.lineNumber == self.lookahead.lineNumber)
self.context.isBindingElement = False
self.context.isAssignmentTarget = False
Expand All @@ -1113,27 +1113,43 @@ def parseLeftHandSideExpressionAllowCall(self):
args = self.parseArguments()
if expr.type is Syntax.Import and len(args) != 1:
self.tolerateError(Messages.BadImportCallArity)
expr = self.finalize(self.startNode(startToken), Node.CallExpression(expr, args))
expr = self.finalize(self.startNode(startToken), Node.CallExpression(expr, args, optional))
if asyncArrow and self.match('=>'):
for arg in args:
self.reinterpretExpressionAsPattern(arg)
expr = Node.AsyncArrowParameterPlaceHolder(args)
elif self.match('['):
self.context.isBindingElement = False
self.context.isAssignmentTarget = True
self.context.isAssignmentTarget = not optional
self.expect('[')
property = self.isolateCoverGrammar(self.parseExpression)
self.expect(']')
expr = self.finalize(self.startNode(startToken), Node.ComputedMemberExpression(expr, property))
expr = self.finalize(self.startNode(startToken), Node.ComputedMemberExpression(expr, property, optional))

elif self.lookahead.type is Token.Template and self.lookahead.head:
# Optional template literal is not included in the spec.
# https://github.com/tc39/proposal-optional-chaining/issues/54
if optional:
self.throwUnexpectedToken(self.lookahead)
if hasOptional:
self.throwError(Messages.InvalidTaggedTemplateOnOptionalChain)
quasi = self.parseTemplateLiteral()
expr = self.finalize(self.startNode(startToken), Node.TaggedTemplateExpression(expr, quasi))

elif self.match('.') or optional:
self.context.isBindingElement = False
self.context.isAssignmentTarget = not optional
if not optional:
self.expect('.')
property = self.parseIdentifierName()
expr = self.finalize(self.startNode(startToken), Node.StaticMemberExpression(expr, property, optional))

else:
break

self.context.allowIn = previousAllowIn
if hasOptional:
return Node.ChainExpression(expr)

return expr

Expand All @@ -1155,29 +1171,46 @@ def parseLeftHandSideExpression(self):
else:
expr = self.inheritCoverGrammar(self.parseNewExpression if self.matchKeyword('new') else self.parsePrimaryExpression)

hasOptional = False
while True:
optional = False
if self.match('?.'):
optional = True
hasOptional = True
self.expect('?.')

if self.match('['):
self.context.isBindingElement = False
self.context.isAssignmentTarget = True
self.context.isAssignmentTarget = not optional
self.expect('[')
property = self.isolateCoverGrammar(self.parseExpression)
self.expect(']')
expr = self.finalize(node, Node.ComputedMemberExpression(expr, property))

elif self.match('.'):
self.context.isBindingElement = False
self.context.isAssignmentTarget = True
self.expect('.')
property = self.parseIdentifierName()
expr = self.finalize(node, Node.StaticMemberExpression(expr, property))
expr = self.finalize(node, Node.ComputedMemberExpression(expr, property, optional))

elif self.lookahead.type is Token.Template and self.lookahead.head:
# Optional template literal is not included in the spec.
# https://github.com/tc39/proposal-optional-chaining/issues/54
if optional:
self.throwUnexpectedToken(self.lookahead)
if hasOptional:
self.throwError(Messages.InvalidTaggedTemplateOnOptionalChain)
quasi = self.parseTemplateLiteral()
expr = self.finalize(node, Node.TaggedTemplateExpression(expr, quasi))

elif self.match('.') or optional:
self.context.isBindingElement = False
self.context.isAssignmentTarget = not optional
if not optional:
self.expect('.')
property = self.parseIdentifierName()
expr = self.finalize(node, Node.StaticMemberExpression(expr, property, optional))

else:
break

if hasOptional:
return Node.ChainExpression(expr)

return expr

# https://tc39.github.io/ecma262/#sec-update-expressions
Expand Down
12 changes: 11 additions & 1 deletion esprima/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,14 +563,24 @@ def scanPunctuator(self):
if self.curlyStack:
self.curlyStack.pop()

elif str == '?':
self.index += 1
if self.source[self.index] == '?':
self.index += 1
str = '??'
if self.source[self.index] == '.' and not self.source[self.index + 1].isdigit():
# "?." in "foo?.3:0" should not be treated as optional chaining.
# See https://github.com/tc39/proposal-optional-chaining#notes
self.index += 1
str = '?.'

elif str in (
')',
';',
',',
'[',
']',
':',
'?',
'~',
):
self.index += 1
Expand Down
1 change: 1 addition & 0 deletions esprima/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Syntax:
BreakStatement = "BreakStatement"
CallExpression = "CallExpression"
CatchClause = "CatchClause"
ChainExpression = 'ChainExpression'
ClassBody = "ClassBody"
ClassDeclaration = "ClassDeclaration"
ClassExpression = "ClassExpression"
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/ES6/arrow-function/migrated_0018.tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
}
}
],
"optional": false,
"range": [
0,
13
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/ES6/arrow-function/migrated_0019.tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
}
}
],
"optional": false,
"range": [
0,
17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@
"type": "Literal",
"value": 0,
"raw": "0"
}
},
"optional": false
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@
"type": "Literal",
"value": 0,
"raw": "0"
}
},
"optional": false
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,8 @@
"type": "Literal",
"value": 0,
"raw": "0"
}
},
"optional": false
}
},
"kind": "init",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,8 @@
"type": "Identifier",
"name": "some_call"
},
"arguments": []
"arguments": [],
"optional": false
},
"property": {
"range": [
Expand All @@ -477,7 +478,8 @@
},
"type": "Identifier",
"name": "a"
}
},
"optional": false
},
"kind": "init",
"method": false,
Expand Down Expand Up @@ -569,7 +571,8 @@
},
"type": "Identifier",
"name": "a"
}
},
"optional": false
},
"kind": "init",
"method": false,
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/ES6/lexical-declaration/let_member.tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@
},
"type": "Identifier",
"name": "let"
}
},
"optional": false
},
"right": {
"range": [
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/ES6/meta-property/new-target-invoke.tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@
"name": "target"
}
},
"arguments": []
"arguments": [],
"optional": false
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@
},
"arguments": []
},
"arguments": []
"arguments": [],
"optional": false
}
}
]
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/ES6/spread-element/call-multi-spread.tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@
"name": "z"
}
}
]
],
"optional": false
}
}
],
Expand Down
11 changes: 6 additions & 5 deletions test/fixtures/ES6/spread-element/call-spread-default.tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
},
"arguments": [
{
"type": "Identifier",
"name": "g",
"range": [
2,
3
Expand All @@ -66,9 +68,7 @@
"line": 1,
"column": 3
}
},
"type": "Identifier",
"name": "g"
}
},
{
"range": [
Expand Down Expand Up @@ -141,7 +141,8 @@
}
}
}
]
],
"optional": false
}
}
],
Expand Down Expand Up @@ -342,4 +343,4 @@
"column": 15
}
}
}
}
3 changes: 2 additions & 1 deletion test/fixtures/ES6/spread-element/call-spread-first.tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@
"type": "Identifier",
"name": "z"
}
]
],
"optional": false
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@
"raw": ".5"
}
}
]
],
"optional": false
}
}
],
Expand Down
Loading