forked from lightscript/babylon-lightscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit d5d4e74 Author: William C. Johnson <[email protected]> Date: Sun Jul 16 20:41:48 2017 -0400 Allow placeholder to be changed via config commit 32e43a0 Author: William C. Johnson <[email protected]> Date: Sun Jul 16 20:24:59 2017 -0400 Spread placeholder tests commit 73b9d32 Author: William C. Johnson <[email protected]> Date: Sun Jul 16 15:31:50 2017 -0400 Initial implementation of syntactic placeholders
- Loading branch information
Showing
19 changed files
with
599 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import Parser from "../parser"; | ||
import { types as tt } from "../tokenizer/types"; | ||
const pp = Parser.prototype; | ||
|
||
export default function(parser) { | ||
if (parser.__syntacticPlaceholderPluginInstalled) return; | ||
parser.__syntacticPlaceholderPluginInstalled = true; | ||
|
||
const ph = parser.options.placeholder || "_"; | ||
const quotedPh = (ph + "").replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&"); | ||
const phRegex = new RegExp("^" + quotedPh + "([0-9]*)$"); | ||
|
||
pp.isPlaceholderName = function(name) { | ||
return phRegex.test(name); | ||
}; | ||
|
||
// c/p parseIdentifier | ||
pp._parseIdentifierOrPlaceholder = function(liberal) { | ||
const node = this.startNode(); | ||
if (!liberal) { | ||
this.checkReservedWord(this.state.value, this.state.start, !!this.state.type.keyword, false); | ||
} | ||
|
||
let name; | ||
if (this.match(tt.name)) { | ||
name = this.state.value; | ||
} else if (this.state.type.keyword) { | ||
name = this.state.type.keyword; | ||
} else { | ||
this.unexpected(); | ||
} | ||
|
||
const matches = phRegex.exec(name); | ||
if (matches) { | ||
if (matches[1]) node.index = parseInt(matches[1]); | ||
this.next(); | ||
return this.finishNode(node, "PlaceholderExpression"); | ||
} | ||
|
||
node.name = name; | ||
|
||
if (!liberal && node.name === "await" && this.state.inAsync) { | ||
this.raise(node.start, "invalid use of await inside of an async function"); | ||
} | ||
|
||
node.loc.identifierName = node.name; | ||
|
||
this.next(); | ||
return this.finishNode(node, "Identifier"); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
_ |
64 changes: 64 additions & 0 deletions
64
test/fixtures/syntactic-placeholder/basic/basic/expected.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
{ | ||
"type": "File", | ||
"start": 0, | ||
"end": 1, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 1 | ||
} | ||
}, | ||
"program": { | ||
"type": "Program", | ||
"start": 0, | ||
"end": 1, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 1 | ||
} | ||
}, | ||
"sourceType": "script", | ||
"body": [ | ||
{ | ||
"type": "ExpressionStatement", | ||
"start": 0, | ||
"end": 1, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 1 | ||
} | ||
}, | ||
"expression": { | ||
"type": "PlaceholderExpression", | ||
"start": 0, | ||
"end": 1, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 1 | ||
} | ||
} | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
_1 |
65 changes: 65 additions & 0 deletions
65
test/fixtures/syntactic-placeholder/basic/indexed/expected.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{ | ||
"type": "File", | ||
"start": 0, | ||
"end": 2, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 2 | ||
} | ||
}, | ||
"program": { | ||
"type": "Program", | ||
"start": 0, | ||
"end": 2, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 2 | ||
} | ||
}, | ||
"sourceType": "script", | ||
"body": [ | ||
{ | ||
"type": "ExpressionStatement", | ||
"start": 0, | ||
"end": 2, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 2 | ||
} | ||
}, | ||
"expression": { | ||
"type": "PlaceholderExpression", | ||
"start": 0, | ||
"end": 2, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 2 | ||
} | ||
}, | ||
"index": 1 | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
test/fixtures/syntactic-placeholder/basic/spread-indexed/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-> [..._1] |
Oops, something went wrong.