-
-
Notifications
You must be signed in to change notification settings - Fork 258
Optional Chaining: Stage 1 plugin #545
Changes from 6 commits
b902fe6
1eaf016
7b51979
0927e24
b2fdd94
bc9edd1
d3bc8fc
03d89b6
51bd87b
26096d6
9bcd85a
b038600
c1702e1
2dd624b
e1ec23c
4628bb9
4c8f4a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -391,6 +391,18 @@ export default class Tokenizer { | |
return this.finishOp(code === 61 ? tt.eq : tt.prefix, 1); | ||
} | ||
|
||
readToken_question() { // '?' | ||
const next = this.input.charCodeAt(this.state.pos + 1); | ||
if (next === 46) { // 46 = question '.' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment is confusing; just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or just |
||
this.state.pos += 2; | ||
return this.finishToken(tt.questionDot); | ||
} | ||
else { | ||
++this.state.pos; | ||
return this.finishToken(tt.question); | ||
} | ||
} | ||
|
||
getTokenFromCode(code) { | ||
switch (code) { | ||
// The interpretation of a dot depends on whether it is followed | ||
|
@@ -425,7 +437,7 @@ export default class Tokenizer { | |
return this.finishToken(tt.colon); | ||
} | ||
|
||
case 63: ++this.state.pos; return this.finishToken(tt.question); | ||
case 63: return this.readToken_question(); | ||
case 64: ++this.state.pos; return this.finishToken(tt.at); | ||
|
||
case 96: // '`' | ||
|
@@ -844,7 +856,7 @@ export default class Tokenizer { | |
const type = this.state.type; | ||
let update; | ||
|
||
if (type.keyword && prevType === tt.dot) { | ||
if (type.keyword && (prevType === tt.dot || prevType === tt.questionDot)) { | ||
this.state.exprAllowed = false; | ||
} else if (update = type.updateContext) { | ||
update.call(this, prevType); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
new C?.() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test doesn't have an expected? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't parse yet, need to be implemented (i'm still WIP) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
func?.() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test doesn't have an expected? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't parse yet, need to be implemented (i'm still WIP) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
obj?.[expr] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
{ | ||
"type": "File", | ||
"start": 0, | ||
"end": 11, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 11 | ||
} | ||
}, | ||
"program": { | ||
"type": "Program", | ||
"start": 0, | ||
"end": 11, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 11 | ||
} | ||
}, | ||
"sourceType": "script", | ||
"body": [ | ||
{ | ||
"type": "ExpressionStatement", | ||
"start": 0, | ||
"end": 11, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 11 | ||
} | ||
}, | ||
"expression": { | ||
"type": "MemberExpression", | ||
"start": 0, | ||
"end": 11, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 11 | ||
} | ||
}, | ||
"object": { | ||
"type": "Identifier", | ||
"start": 0, | ||
"end": 3, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 3 | ||
}, | ||
"identifierName": "obj" | ||
}, | ||
"name": "obj" | ||
}, | ||
"optional": true, | ||
"property": { | ||
"type": "Identifier", | ||
"start": 6, | ||
"end": 10, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 6 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 10 | ||
}, | ||
"identifierName": "expr" | ||
}, | ||
"name": "expr" | ||
}, | ||
"computed": true | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo?.bar |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
{ | ||
"type": "File", | ||
"start": 0, | ||
"end": 8, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 8 | ||
} | ||
}, | ||
"program": { | ||
"type": "Program", | ||
"start": 0, | ||
"end": 8, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 8 | ||
} | ||
}, | ||
"sourceType": "script", | ||
"body": [ | ||
{ | ||
"type": "ExpressionStatement", | ||
"start": 0, | ||
"end": 8, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 8 | ||
} | ||
}, | ||
"expression": { | ||
"type": "MemberExpression", | ||
"start": 0, | ||
"end": 8, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 8 | ||
} | ||
}, | ||
"object": { | ||
"type": "Identifier", | ||
"start": 0, | ||
"end": 3, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 3 | ||
}, | ||
"identifierName": "foo" | ||
}, | ||
"name": "foo" | ||
}, | ||
"property": { | ||
"type": "Identifier", | ||
"start": 5, | ||
"end": 8, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 5 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 8 | ||
}, | ||
"identifierName": "bar" | ||
}, | ||
"name": "bar" | ||
}, | ||
"optional": true, | ||
"computed": false | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indicates