Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions javascript/extractor/src/com/semmle/jcorn/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2296,9 +2296,11 @@ protected Identifier parseIdent(boolean liberal) {
&& (this.options.ecmaVersion() >= 6
|| inputSubstring(this.start, this.end).indexOf("\\") == -1))
this.raiseRecoverable(this.start, "The keyword '" + this.value + "' is reserved");
if (!isPrivateField && this.inGenerator && this.value.equals("yield"))
// `yield` and `await` are only restricted as bindings and references. `liberal` means we
// are parsing a property name, where they are ordinary identifiers.
if (!liberal && this.inGenerator && this.value.equals("yield"))
this.raiseRecoverable(this.start, "Can not use 'yield' as identifier inside a generator");
if (!isPrivateField && this.inAsync && this.value.equals("await"))
if (!liberal && this.inAsync && this.value.equals("await"))
Comment on lines +2301 to +2303
this.raiseRecoverable(
this.start, "Can not use 'await' as identifier inside an async function");
name = String.valueOf(this.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
ExportExtensionsTests.class,
AutoBuildTests.class,
RobustnessTests.class,
NumericSeparatorTests.class
NumericSeparatorTests.class,
AwaitYieldIdentifierTests.class
})
public class AllTests {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.semmle.js.extractor.test;

import com.semmle.jcorn.CustomParser;
import com.semmle.jcorn.Options;
import com.semmle.jcorn.SyntaxError;
import org.junit.Assert;
import org.junit.Test;

/**
* Tests for using <code>await</code> and <code>yield</code> as property names inside async
* functions and generators, where they are only restricted as bindings and references.
*/
public class AwaitYieldIdentifierTests {
private void testSucceed(String input) {
new CustomParser(new Options().esnext(true), input, 0).parse();
}

private void testFail(String input, String msg) {
try {
new CustomParser(new Options().esnext(true), input, 0).parse();
Assert.fail("Expected syntax error, but parsing succeeded.");
} catch (SyntaxError e) {
Assert.assertEquals(msg, e.getMessage());
}
}

@Test
public void awaitAsPropertyName() {
testSucceed("async function f(pool) { return await pool.await(); }");
testSucceed("async function f(o) { return o?.await; }");
testSucceed("async function f() { return { await: 42 }; }");
testSucceed("async function f() { class C { await() {} } }");
testSucceed("async function f(o) { o.await = 42; }");
}

@Test
public void yieldAsPropertyName() {
testSucceed("function* g(o) { yield o.yield(); }");
testSucceed("function* g() { return { yield: 42 }; }");
testSucceed("function* g() { class C { yield() {} } }");
}

@Test
public void awaitAsIdentifier() {
testFail(
"async function f() { var await = 42; }",
"Can not use 'await' as identifier inside an async function (1:25)");
testFail(
"async function f() { return { await }; }",
"'await' can not be used as shorthand property (1:30)");
}

@Test
public void yieldAsIdentifier() {
testFail(
"function* g() { var yield = 42; }",
"Can not use 'yield' as identifier inside a generator (1:20)");
testFail(
"function* g() { return { yield }; }",
"'yield' can not be used as shorthand property (1:25)");
}
}
17 changes: 17 additions & 0 deletions javascript/extractor/tests/es2017/input/await-yield-property.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const o = { await: 42, yield: 43 };

async function f(pool) {
console.log(o.await);
o.await = 1;
return await pool.await();
}

function* g() {
yield o.yield;
yield o?.yield;
}

class C {
async await() {}
*yield() {}
}
Loading