Skip to content
This repository was archived by the owner on May 19, 2018. It is now read-only.

NumberLiteralSeparator: Stage 1 feature plugin. Closes gh-538 #541

Merged
merged 4 commits into from
May 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix-up per review
Signed-off-by: Rick Waldron <[email protected]>
  • Loading branch information
rwaldron committed May 26, 2017
commit 244cade8fd3395b4477f562d87439cfa679a51a4
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ require("babylon").parse("code", {
- `functionBind`
- `functionSent`
- `dynamicImport`
- `numericSeparator` ([proposal](https://github.com/samuelgoto/proposal-numeric-separator))

### FAQ

Expand Down
27 changes: 15 additions & 12 deletions src/tokenizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,21 +571,24 @@ export default class Tokenizer extends LocationParser {

for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
const code = this.input.charCodeAt(this.state.pos);
const prev = this.input.charCodeAt(this.state.pos - 1);
const next = this.input.charCodeAt(this.state.pos + 1);

let val;
if (code === 95) {
if (forbiddenNumericLiteralSeparatorSibling.includes(prev) ||
forbiddenNumericLiteralSeparatorSibling.includes(next) ||
Number.isNaN(next)) {
this.raise(this.state.pos, "Invalid NumericLiteralSeparator");
}

// Ignore this _ character
++this.state.pos;
continue;
if (this.hasPlugin("numericSeparator")) {
const prev = this.input.charCodeAt(this.state.pos - 1);
const next = this.input.charCodeAt(this.state.pos + 1);
if (code === 95) {
if (forbiddenNumericLiteralSeparatorSibling.includes(prev) ||
forbiddenNumericLiteralSeparatorSibling.includes(next) ||
Number.isNaN(next)) {
this.raise(this.state.pos, "Invalid NumericLiteralSeparator");
Copy link
Member

@hzoo hzoo May 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might actually create a different error message for multi _, if it's at the end or the beginning, or around invalid chars but we can totally do that later too.

Could be a good 2nd PR

}

// Ignore this _ character
++this.state.pos;
continue;
}
}

if (code >= 97) {
val = code - 97 + 10; // a
} else if (code >= 65) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["numericSeparator"]
}