From a2172e1a99ccb34bcfe96d0c55675f6fff81ac51 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 20 May 2017 13:11:37 +0800 Subject: [PATCH] fix parsing of `yield` as object key (#1976) fixes #1974 --- lib/parse.js | 3 ++- test/compress/yield.js | 9 +++++++++ test/mocha/yield.js | 11 +++++------ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index b9800dbc..35c5fd27 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -2411,7 +2411,8 @@ function parse($TEXT, options) { unexpected(tmp); } case "name": - if (tmp.value === "yield" && S.input.has_directive("use strict") && !is_in_generator()) { + if (tmp.value == "yield" && !is_token(peek(), "punc", ":") + && S.input.has_directive("use strict") && !is_in_generator()) { token_error(tmp, "Unexpected yield identifier inside strict mode"); } case "string": diff --git a/test/compress/yield.js b/test/compress/yield.js index d1e7ece3..d64605b1 100644 --- a/test/compress/yield.js +++ b/test/compress/yield.js @@ -190,3 +190,12 @@ yield_sub: { } expect_exact: 'function*foo(){yield x["foo"];(yield x)["foo"];yield(yield obj.foo())["bar"]()}' } + +yield_as_ES5_property: { + input: { + "use strict"; + console.log({yield: 42}.yield); + } + expect_exact: '"use strict";console.log({yield:42}.yield);' + expect_stdout: "42" +} diff --git a/test/mocha/yield.js b/test/mocha/yield.js index 3665372d..22b5bb07 100644 --- a/test/mocha/yield.js +++ b/test/mocha/yield.js @@ -65,9 +65,9 @@ describe("Yield", function() { ); }); - it("Should not allow yield to be used as symbol, identifier or property outside generators in strict mode", function() { + it("Should not allow yield to be used as symbol, identifier or shorthand property outside generators in strict mode", function() { var tests = [ - // Fail as as_symbol + // Fail in as_symbol '"use strict"; import yield from "bar";', '"use strict"; yield = 123;', '"use strict"; yield: "123";', @@ -79,13 +79,12 @@ describe("Yield", function() { '"use strict"; var yield = "foo";', '"use strict"; class yield {}', - // Fail as maybe_assign + // Fail in maybe_assign '"use strict"; var foo = yield;', '"use strict"; var foo = bar = yield', - // Fail as as_property_name + // Fail in as_property_name '"use strict"; var foo = {yield};', - '"use strict"; var bar = {yield: "foo"};' ]; var fail = function(e) { @@ -103,4 +102,4 @@ describe("Yield", function() { assert.throws(test(tests[i]), fail, tests[i]); } }); -}); \ No newline at end of file +});