|
| 1 | +if (typeof exports !== "undefined") { |
| 2 | + var test = require("./driver.js").test |
| 3 | + var testFail = require("./driver.js").testFail |
| 4 | +} |
| 5 | + |
| 6 | +// Top-level using declaration with commonjs |
| 7 | +test("using x = resource;", { |
| 8 | + type: "Program", |
| 9 | + body: [{ |
| 10 | + type: "VariableDeclaration", |
| 11 | + declarations: [{ |
| 12 | + type: "VariableDeclarator", |
| 13 | + id: { |
| 14 | + type: "Identifier", |
| 15 | + name: "x" |
| 16 | + }, |
| 17 | + init: { |
| 18 | + type: "Identifier", |
| 19 | + name: "resource" |
| 20 | + } |
| 21 | + }], |
| 22 | + kind: "using" |
| 23 | + }], |
| 24 | + sourceType: "script" |
| 25 | +}, {ecmaVersion: 17, sourceType: "commonjs"}); |
| 26 | + |
| 27 | +// Top-level new.target with commonjs |
| 28 | +test("new.target", { |
| 29 | + type: "Program", |
| 30 | + body: [{ |
| 31 | + type: "ExpressionStatement", |
| 32 | + expression: { |
| 33 | + type: "MetaProperty", |
| 34 | + meta: {type: "Identifier", name: "new"}, |
| 35 | + property: {type: "Identifier", name: "target"} |
| 36 | + } |
| 37 | + }], |
| 38 | + sourceType: "script" |
| 39 | +}, {ecmaVersion: 6, sourceType: "commonjs"}); |
| 40 | + |
| 41 | +// new.target at top-level scope with commonjs |
| 42 | +test("let y = () => new.target", { |
| 43 | + type: "Program", |
| 44 | + body: [{ |
| 45 | + type: "VariableDeclaration", |
| 46 | + declarations: [{ |
| 47 | + type: "VariableDeclarator", |
| 48 | + id: { |
| 49 | + type: "Identifier", |
| 50 | + name: "y" |
| 51 | + }, |
| 52 | + init: { |
| 53 | + type: "ArrowFunctionExpression", |
| 54 | + body: { |
| 55 | + type: "MetaProperty", |
| 56 | + meta: {type: "Identifier", name: "new"}, |
| 57 | + property: {type: "Identifier", name: "target"} |
| 58 | + }, |
| 59 | + params: [] |
| 60 | + } |
| 61 | + }], |
| 62 | + kind: "let" |
| 63 | + }], |
| 64 | + sourceType: "script" |
| 65 | +}, {ecmaVersion: 6, sourceType: "commonjs"}); |
| 66 | + |
| 67 | +// Top-level return statement with commonjs |
| 68 | +test("return {} / 2", { |
| 69 | + type: "Program", |
| 70 | + body: [{ |
| 71 | + type: "ReturnStatement", |
| 72 | + argument: { |
| 73 | + type: "BinaryExpression", |
| 74 | + left: { |
| 75 | + type: "ObjectExpression", |
| 76 | + properties: [] |
| 77 | + }, |
| 78 | + right: { |
| 79 | + type: "Literal", |
| 80 | + value: 2 |
| 81 | + }, |
| 82 | + operator: "/" |
| 83 | + } |
| 84 | + }], |
| 85 | + sourceType: "script" |
| 86 | +}, {sourceType: "commonjs"}); |
| 87 | + |
| 88 | +// Illegal return statement with commonjs |
| 89 | +testFail(`class X { static { return; } }`, "'return' outside of function (1:19)", {ecmaVersion: 13, sourceType: "commonjs"}); |
| 90 | + |
| 91 | +// Top-level await using declaration with commonjs |
| 92 | +testFail("await using x = resource;", "Await using cannot appear outside of async function (1:0)", {ecmaVersion: 17, sourceType: "commonjs"}); |
| 93 | + |
| 94 | +// Disallowing allowAwaitOutsideFunction with commonjs |
| 95 | +testFail("await 1", "Cannot use allowAwaitOutsideFunction with sourceType: commonjs", {allowAwaitOutsideFunction: true, sourceType: "commonjs"}); |
| 96 | +testFail("x", "Cannot use allowAwaitOutsideFunction with sourceType: commonjs", {allowAwaitOutsideFunction: true, sourceType: "commonjs"}); |
| 97 | +test("x", {}, {allowAwaitOutsideFunction: false, sourceType: "commonjs"}); |
0 commit comments