module-syntax.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // ES6 Module syntax tests -- verifies syntax of import and export statements
  6. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. function testModuleScript(source, message, shouldFail) {
  8. let testfunc = () => WScript.LoadModule(source, 'samethread');
  9. if (shouldFail) {
  10. let caught = false;
  11. // We can't use assert.throws here because the SyntaxError used to construct the thrown error
  12. // is from a different context so it won't be strictly equal to our SyntaxError.
  13. try {
  14. testfunc();
  15. } catch(e) {
  16. caught = true;
  17. // Compare toString output of SyntaxError and other context SyntaxError constructor.
  18. assert.areEqual(e.constructor.toString(), SyntaxError.toString(), message);
  19. }
  20. assert.isTrue(caught, `Expected error not thrown: ${message}`);
  21. } else {
  22. assert.doesNotThrow(testfunc, message);
  23. }
  24. }
  25. var tests = [
  26. {
  27. name: "All valid (non-default) export statements",
  28. body: function () {
  29. assert.doesNotThrow(function () { WScript.LoadModuleFile('.\\module\\ValidExportStatements.js', 'samethread'); }, "Valid export statements");
  30. }
  31. },
  32. {
  33. name: "Valid default export statements",
  34. body: function () {
  35. testModuleScript('export default function () { };', 'Unnamed function expression default export');
  36. testModuleScript('export default function fn2 () { }', 'Named function expression default export');
  37. testModuleScript('export default function* () { };', 'Unnamed generator function expression default export');
  38. testModuleScript('export default function* gn2 () { }', 'Named generator function expression default export');
  39. testModuleScript('export default class { };', 'Unnamed class expression default export');
  40. testModuleScript('export default class cl2 { }', 'Named class default expression export');
  41. testModuleScript('export default 1;', 'Primitive type default export');
  42. testModuleScript('var a; export default a = 10;', 'Variable in assignment expression default export');
  43. testModuleScript('export default () => 3', 'Simple default lambda expression export statement');
  44. testModuleScript('function foo() { }; export default foo', 'Named function statement default export');
  45. testModuleScript('function* g() { }; export default g', 'Named generator function statement default export');
  46. testModuleScript('class c { }; export default c', 'Named class statement default export');
  47. testModuleScript("var _ = { method: function() { return 'method_result'; }, method2: function() { return 'method2_result'; } }; export default _", 'Export object with methods - framework model');
  48. }
  49. },
  50. {
  51. name: "Syntax error export statements",
  52. body: function () {
  53. testModuleScript('export const const1;', 'Syntax error if const decl is missing initializer', true);
  54. testModuleScript('function foo() { }; export foo;', "Syntax error if we're trying to export an identifier without default or curly braces", true);
  55. testModuleScript('export function () { }', 'Syntax error if function declaration is missing binding identifier', true);
  56. testModuleScript('export function* () { }', 'Syntax error if generator declaration is missing binding identifier', true);
  57. testModuleScript('export class { }', 'Syntax error if class declaration is missing binding identifier', true);
  58. testModuleScript('function foo() { }; export [ foo ];', 'Syntax error if we use brackets instead of curly braces in export statement', true);
  59. testModuleScript('function foo() { export default function() { } }', 'Syntax error if export statement is in a nested function', true);
  60. // testModuleScript("eval('export default function() { }');", 'Syntax error if export statement is in eval', true);
  61. testModuleScript('function foo() { }; export { , foo };', 'Syntax error if named export list contains an empty element', true);
  62. testModuleScript('function foo() { }; () => { export { foo }; }', 'Syntax error if export statement is in arrow function', true);
  63. testModuleScript('function foo() { }; try { export { foo }; } catch(e) { }', 'Syntax error if export statement is in try catch statement', true);
  64. testModuleScript('function foo() { }; { export { foo }; }', 'Syntax error if export statement is in any block', true);
  65. testModuleScript('export default 1, 2, 3;', "Export default takes an assignment expression which doesn't allow comma expressions", true);
  66. testModuleScript('export 12;', 'Syntax error if export is followed by non-identifier', true);
  67. testModuleScript("export 'string_constant';", 'Syntax error if export is followed by string constant', true);
  68. testModuleScript('export ', 'Syntax error if export is followed by EOF', true);
  69. testModuleScript('function foo() { }; export { foo as 100 };', 'Syntax error in named export clause if trying to export as numeric constant', true);
  70. }
  71. },
  72. {
  73. name: "Syntax error import statements",
  74. body: function () {
  75. // testModuleScript(`eval('import foo from "ValidExportStatements.js";');`, 'Syntax error if import statement is in eval', true);
  76. testModuleScript('function foo() { import foo from "ValidExportStatements.js"; }', 'Syntax error if import statement is in nested function', true);
  77. testModuleScript('import foo, bar from "ValidExportStatements.js";', 'Syntax error if import statement has multiple default bindings', true);
  78. testModuleScript('import foo;', 'Syntax error if import statement is missing from clause', true);
  79. testModuleScript('import * as foo, from "ValidExportStatements.js";', 'Syntax error if import statement has comma after namespace import', true);
  80. testModuleScript('import * as foo, bar from "ValidExportStatements.js";', 'Syntax error if import statement has default binding after namespace import', true);
  81. testModuleScript('import * as foo, { bar } from "ValidExportStatements.js";', 'Syntax error if import statement has named import list after namespace import', true);
  82. testModuleScript('import { foo }, from "ValidExportStatements.js";', 'Syntax error if import statement has comma after named import list', true);
  83. testModuleScript('import { foo }, bar from "ValidExportStatements.js";', 'Syntax error if import statement has default binding after named import list', true);
  84. testModuleScript('import { foo }, * as ns1 from "ValidExportStatements.js";', 'Syntax error if import statement has namespace import after named import list', true);
  85. testModuleScript('import { foo }', 'Syntax error if import statement is missing from clause', true);
  86. testModuleScript('import [ foo ] from "ValidExportStatements.js";', 'Syntax error if named import clause uses brackets', true);
  87. testModuleScript('import * foo from "ValidExportStatements.js";', 'Syntax error if namespace import is missing "as" keyword', true);
  88. testModuleScript('import * as "foo" from "ValidExportStatements.js";', 'Syntax error if namespace imported binding name is not identifier', true);
  89. testModuleScript('import { , foo } from "ValidExportStatements.js";', 'Syntax error if named import list contains an empty element', true);
  90. testModuleScript('import foo from "ValidExportStatements.js"; foo = 12;', 'Imported default bindings are constant bindings', true);
  91. testModuleScript('import foo from "ValidExportStatements.js"; import foo from "ValidExportStatements.js";', 'Default import cannot be bound to the same symbol', true);
  92. testModuleScript('import { foo } from "ValidExportStatements.js"; foo = 12;', 'Imported named bindings are constant bindings', true);
  93. testModuleScript('import { foo } from "ValidExportStatements.js"; import { foo } from "ValidExportStatements.js";', 'Multiple named imports cannot be bound to the same symbol', true);
  94. testModuleScript('import * as foo from "ValidExportStatements.js"; foo = 12;', 'Namespace import bindings are constant bindings', true);
  95. testModuleScript('import * as foo from "ValidExportStatements.js"; import * as foo from "ValidExportStatements.js";', 'Multiple namespace imports cannot be bound to the same symbol', true);
  96. testModuleScript('import { foo as foo22 } from "ValidExportStatements.js"; foo22 = 12;', 'Renamed import bindings are constant bindings', true);
  97. testModuleScript('import { foo as bar, bar } from "ValidExportStatements.js";', 'Named import clause may not contain multiple binding identifiers with the same name', true);
  98. testModuleScript('import foo from "ValidExportStatements.js"; import * as foo from "ValidExportStatements.js";', 'Imported bindings cannot be overwritten by later imports', true);
  99. testModuleScript('() => { import arrow from ""; }', 'Syntax error if import statement is in arrow function', true);
  100. testModuleScript('try { import _try from ""; } catch(e) { }', 'Syntax error if import statement is in try catch statement', true);
  101. testModuleScript('{ import in_block from ""; }', 'Syntax error if import statement is in any block', true);
  102. testModuleScript('import {', 'Named import clause which has EOF after left curly', true);
  103. testModuleScript('import { foo', 'Named import clause which has EOF after identifier', true);
  104. testModuleScript('import { foo as ', 'Named import clause which has EOF after identifier as', true);
  105. testModuleScript('import { foo as bar ', 'Named import clause which has EOF after identifier as identifier', true);
  106. testModuleScript('import { foo as bar, ', 'Named import clause which has EOF after identifier as identifier comma', true);
  107. testModuleScript('import { switch } from "module";', 'Named import clause which has non-identifier token as the first token', true);
  108. testModuleScript('import { foo bar } from "module";', 'Named import clause missing "as" token', true);
  109. testModuleScript('import { foo as switch } from "module";', 'Named import clause with non-identifier token after "as"', true);
  110. testModuleScript('import { foo, , } from "module";', 'Named import clause with too many trailing commas', true);
  111. }
  112. },
  113. {
  114. name: "All valid re-export statements",
  115. body: function () {
  116. assert.doesNotThrow(function () { WScript.LoadModuleFile('.\\module\\ValidReExportStatements.js', 'samethread'); }, "Valid re-export statements");
  117. }
  118. },
  119. ];
  120. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });