From 15b9b1360881282eaa32bebce70eb50c1cde1eb0 Mon Sep 17 00:00:00 2001 From: "William C. Johnson" Date: Fri, 6 Oct 2017 22:04:52 -0400 Subject: [PATCH] Disallow whitespace around safe tilde call operator Addresses https://github.com/wcjohnson/lightscript/issues/19 --- src/plugins/tildeCall.js | 9 ++++++++- .../safe/whitespace-illegal-1/actual.js | 1 + .../safe/whitespace-illegal-1/options.json | 7 +++++++ .../safe/whitespace-illegal-2/actual.js | 1 + .../safe/whitespace-illegal-2/options.json | 7 +++++++ 5 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/tilde-call-expression/safe/whitespace-illegal-1/actual.js create mode 100644 test/fixtures/tilde-call-expression/safe/whitespace-illegal-1/options.json create mode 100644 test/fixtures/tilde-call-expression/safe/whitespace-illegal-2/actual.js create mode 100644 test/fixtures/tilde-call-expression/safe/whitespace-illegal-2/options.json diff --git a/src/plugins/tildeCall.js b/src/plugins/tildeCall.js index 746eae09ad..04deca4d0e 100644 --- a/src/plugins/tildeCall.js +++ b/src/plugins/tildeCall.js @@ -16,7 +16,11 @@ export default function(parser) { node.callee = this.parseSubscripts(callee, this.state.start, this.state.startLoc, true); // Allow safe tilde calls (a~b?(c)) - if (this.hasPlugin("safeCallExpression") && this.eat(tt.question)) { + if ( + this.hasPlugin("safeCallExpression") && + this.state.lastTokEnd === (this.state.pos - 1) && + this.eat(tt.question) + ) { node.optional = true; } @@ -31,6 +35,9 @@ export default function(parser) { return false; } } else { + if (node.optional && this.state.lastTokEnd !== (this.state.pos - 1)) { + this.unexpected(null, "Whitespace is forbidden after `?` in an optional call."); + } this.expect(tt.parenL); node.arguments = this.parseCallExpressionArguments(tt.parenR, false); node.arguments.unshift(firstArg); diff --git a/test/fixtures/tilde-call-expression/safe/whitespace-illegal-1/actual.js b/test/fixtures/tilde-call-expression/safe/whitespace-illegal-1/actual.js new file mode 100644 index 0000000000..b2d8f5558b --- /dev/null +++ b/test/fixtures/tilde-call-expression/safe/whitespace-illegal-1/actual.js @@ -0,0 +1 @@ +a~b ?(c) diff --git a/test/fixtures/tilde-call-expression/safe/whitespace-illegal-1/options.json b/test/fixtures/tilde-call-expression/safe/whitespace-illegal-1/options.json new file mode 100644 index 0000000000..0144d54157 --- /dev/null +++ b/test/fixtures/tilde-call-expression/safe/whitespace-illegal-1/options.json @@ -0,0 +1,7 @@ +{ + "alternatives": { + "default": { + "throws": "Unexpected token, expected ( (1:4)" + } + } +} diff --git a/test/fixtures/tilde-call-expression/safe/whitespace-illegal-2/actual.js b/test/fixtures/tilde-call-expression/safe/whitespace-illegal-2/actual.js new file mode 100644 index 0000000000..b51f11bd65 --- /dev/null +++ b/test/fixtures/tilde-call-expression/safe/whitespace-illegal-2/actual.js @@ -0,0 +1 @@ +a~b? (c) diff --git a/test/fixtures/tilde-call-expression/safe/whitespace-illegal-2/options.json b/test/fixtures/tilde-call-expression/safe/whitespace-illegal-2/options.json new file mode 100644 index 0000000000..41d4490fdf --- /dev/null +++ b/test/fixtures/tilde-call-expression/safe/whitespace-illegal-2/options.json @@ -0,0 +1,7 @@ +{ + "alternatives": { + "default": { + "throws": "Whitespace is forbidden after `?` in an optional call. (1:5)" + } + } +}