module-syntax.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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('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 _default() { }; export default _default', '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('function foo() { }; export { , foo };', 'Syntax error if named export list contains an empty element', true);
  61. testModuleScript('function foo() { }; () => { export { foo }; }', 'Syntax error if export statement is in arrow function', true);
  62. testModuleScript('function foo() { }; try { export { foo }; } catch(e) { }', 'Syntax error if export statement is in try catch statement', true);
  63. testModuleScript('function foo() { }; { export { foo }; }', 'Syntax error if export statement is in any block', true);
  64. testModuleScript('export default 1, 2, 3;', "Export default takes an assignment expression which doesn't allow comma expressions", true);
  65. testModuleScript('export 12;', 'Syntax error if export is followed by non-identifier', true);
  66. testModuleScript("export 'string_constant';", 'Syntax error if export is followed by string constant', true);
  67. testModuleScript('export ', 'Syntax error if export is followed by EOF', true);
  68. testModuleScript('function foo() { }; export { foo as 100 };', 'Syntax error in named export clause if trying to export as numeric constant', true);
  69. }
  70. },
  71. {
  72. name: "Syntax error import statements",
  73. body: function () {
  74. testModuleScript('function foo() { import foo from "ValidExportStatements.js"; }', 'Syntax error if import statement is in nested function', true);
  75. testModuleScript('import foo, bar from "ValidExportStatements.js";', 'Syntax error if import statement has multiple default bindings', true);
  76. testModuleScript('import { foo, foo } from "ValidExportStatements.js";', 'Redeclaration error if multiple imports have the same local name', true);
  77. testModuleScript('import { foo, bar as foo } from "ValidExportStatements.js";', 'Redeclaration error if multiple imports have the same local name', true);
  78. testModuleScript('const foo = 12; import { foo } from "ValidExportStatements.js";', 'Syntax error if module body has a const declaration bound to the same name as a module import', true);
  79. testModuleScript('function foo() { }; import { foo } from "ValidExportStatements.js";', 'Syntax error if module body has a function declaration bound to the same name as a module import', true);
  80. testModuleScript('import foo;', 'Syntax error if import statement is missing from clause', true);
  81. testModuleScript('import * as foo, from "ValidExportStatements.js";', 'Syntax error if import statement has comma after namespace import', true);
  82. testModuleScript('import * as foo, bar from "ValidExportStatements.js";', 'Syntax error if import statement has default binding after namespace import', true);
  83. testModuleScript('import * as foo, { bar } from "ValidExportStatements.js";', 'Syntax error if import statement has named import list after namespace import', true);
  84. testModuleScript('import { foo }, from "ValidExportStatements.js";', 'Syntax error if import statement has comma after named import list', true);
  85. testModuleScript('import { foo }, bar from "ValidExportStatements.js";', 'Syntax error if import statement has default binding after named import list', true);
  86. testModuleScript('import { foo }, * as ns1 from "ValidExportStatements.js";', 'Syntax error if import statement has namespace import after named import list', true);
  87. testModuleScript('import { foo }', 'Syntax error if import statement is missing from clause', true);
  88. testModuleScript('import [ foo ] from "ValidExportStatements.js";', 'Syntax error if named import clause uses brackets', true);
  89. testModuleScript('import * foo from "ValidExportStatements.js";', 'Syntax error if namespace import is missing "as" keyword', true);
  90. testModuleScript('import * as "foo" from "ValidExportStatements.js";', 'Syntax error if namespace imported binding name is not identifier', true);
  91. testModuleScript('import { , foo } from "ValidExportStatements.js";', 'Syntax error if named import list contains an empty element', true);
  92. testModuleScript('import foo from "ValidExportStatements.js"; import foo from "ValidExportStatements.js";', 'Default import cannot be bound to the same symbol', 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"; import * as foo from "ValidExportStatements.js";', 'Multiple namespace imports cannot be bound to the same symbol', true);
  95. testModuleScript('import { foo as bar, bar } from "ValidExportStatements.js";', 'Named import clause may not contain multiple binding identifiers with the same name', true);
  96. testModuleScript('import foo from "ValidExportStatements.js"; import * as foo from "ValidExportStatements.js";', 'Imported bindings cannot be overwritten by later imports', true);
  97. testModuleScript('() => { import arrow from ""; }', 'Syntax error if import statement is in arrow function', true);
  98. testModuleScript('try { import _try from ""; } catch(e) { }', 'Syntax error if import statement is in try catch statement', true);
  99. testModuleScript('{ import in_block from ""; }', 'Syntax error if import statement is in any block', true);
  100. testModuleScript('import {', 'Named import clause which has EOF after left curly', true);
  101. testModuleScript('import { foo', 'Named import clause which has EOF after identifier', true);
  102. testModuleScript('import { foo as ', 'Named import clause which has EOF after identifier as', true);
  103. testModuleScript('import { foo as bar ', 'Named import clause which has EOF after identifier as identifier', true);
  104. testModuleScript('import { foo as bar, ', 'Named import clause which has EOF after identifier as identifier comma', true);
  105. testModuleScript('import { switch } from "module";', 'Named import clause which has non-identifier token as the first token', true);
  106. testModuleScript('import { foo bar } from "module";', 'Named import clause missing "as" token', true);
  107. testModuleScript('import { foo as switch } from "module";', 'Named import clause with non-identifier token after "as"', true);
  108. testModuleScript('import { foo, , } from "module";', 'Named import clause with too many trailing commas', true);
  109. }
  110. },
  111. {
  112. name: "Runtime error import statements",
  113. body: function () {
  114. testModuleScript('import foo from "ValidExportStatements.js"; assert.throws(()=>{ foo =12; }, TypeError, "assignment to const");', 'Imported default bindings are constant bindings', false);
  115. testModuleScript('import { foo } from "ValidExportStatements.js"; assert.throws(()=>{ foo = 12; }, TypeError, "assignment to const");', 'Imported named bindings are constant bindings', false);
  116. testModuleScript('import * as foo from "ValidExportStatements.js"; assert.throws(()=>{ foo = 12; }, TypeError, "assignment to const");', 'Namespace import bindings are constant bindings', false);
  117. testModuleScript('import { foo as foo22 } from "ValidExportStatements.js"; assert.throws(()=>{ foo22 = 12; }, TypeError, "assignment to const");', 'Renamed import bindings are constant bindings', false);
  118. }
  119. },
  120. {
  121. name: "All valid re-export statements",
  122. body: function () {
  123. assert.doesNotThrow(function () { WScript.LoadModuleFile('ValidReExportStatements.js', 'samethread'); }, "Valid re-export statements");
  124. }
  125. },
  126. {
  127. name: "HTML comments do not parse in module code",
  128. body: function () {
  129. testModuleScript("<!--\n", "HTML open comment does not parse in module code", true);
  130. testModuleScript("\n-->", "HTML close comment does not parse in module code", true);
  131. testModuleScript("<!-- -->", "HTML comment does not parse in module code", true);
  132. testModuleScript("/* */ -->", "HTML comment after delimited comment does not parse in module code", true);
  133. testModuleScript("/* */\n-->", "HTML comment after delimited comment does not parse in module code", true);
  134. }
  135. }
  136. ];
  137. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });