Hashbang.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  6. let invalidScripts = [
  7. [" #!\n", "Hashbang must be the first token (even before whitespace)"],
  8. ["\n#!\n", "Hashbang must be the first token (even before whitespace)"],
  9. ["##!\n", "Hashbang token is '#!'"],
  10. [";#!\n", "Hashbang must be the first token"],
  11. ["'use strict'#!\n", "Hashbang must come before 'use strict'"],
  12. ["#!\n#!\n", "Only one hashbang may exist because it has to be the first token"],
  13. ["function foo() {\n#!\n}", "Hashbang must be the first token in the script (not local function)"],
  14. ["function foo() {#!\n}", "Hashbang must be the first token in the script (not local function)"],
  15. ["#\\041\n", "Hashbang can't be made of encoded characters"],
  16. ["#\\u0021\n", "Hashbang can't be made of encoded characters"],
  17. ["#\\u{21}\n", "Hashbang can't be made of encoded characters"],
  18. ["#\\x21\n", "Hashbang can't be made of encoded characters"],
  19. ["\\043!\n", "Hashbang can't be made of encoded characters"],
  20. ["\\u0023!\n", "Hashbang can't be made of encoded characters"],
  21. ["\\u{23}!\n", "Hashbang can't be made of encoded characters"],
  22. ["\\x23!\n", "Hashbang can't be made of encoded characters"],
  23. ["\\u0023\\u0021\n", "Hashbang can't be made of encoded characters"],
  24. ["Function('#!\n','')", "Hashbang is not valid in function evaluator contexts"],
  25. ["new Function('#!\n','')", "Hashbang is not valid in function evaluator contexts"],
  26. ["{\n#!\n}\n", "Hashbang not valid in block"],
  27. ["#!/*\nthrow 123;\n*/\nthrow 456;", "Hashbang comments out a single line"],
  28. ["\\\\ single line comment\n#! hashbang\n", "Single line comment may not preceed hashbang"],
  29. ["/**/#! hashbang\n", "Multi-line comment may not preceed hashbang"],
  30. ["/**/\n#! hashbang\n", "Multi-line comment may not preceed hashbang"],
  31. ];
  32. var tests = [
  33. {
  34. name: "Valid hashbang in ordinary script",
  35. body: function () {
  36. assert.areEqual(2, WScript.LoadScript("#! throw 'error';\nthis.prop=2;").prop);
  37. assert.areEqual(3, WScript.LoadScript("#! throw 'error'\u{000D}this.prop=3;").prop);
  38. assert.areEqual(4, WScript.LoadScript("#! throw 'error'\u{2028}this.prop=4;").prop);
  39. assert.areEqual(5, WScript.LoadScript("#! throw 'error'\u{2029}this.prop=5;").prop);
  40. }
  41. },
  42. {
  43. name: "Valid hashbang in module script",
  44. body: function () {
  45. WScript.RegisterModuleSource('module_hashbang_valid.js', "#! export default 123;\n export default 456;");
  46. testRunner.LoadModule(`
  47. import {default as prop} from 'module_hashbang_valid.js';
  48. assert.areEqual(456, prop);
  49. `, 'samethread', false, false);
  50. }
  51. },
  52. {
  53. name: "Valid hashbang in eval",
  54. body: function () {
  55. assert.areEqual(undefined, eval('#!'));
  56. assert.areEqual(undefined, eval('#!\n'));
  57. assert.areEqual(1, eval('#!\n1'));
  58. assert.areEqual(undefined, eval('#!2\n'));
  59. }
  60. },
  61. {
  62. name: "Valid hashbang in indirect eval",
  63. body: function () {
  64. let _eval = eval;
  65. assert.areEqual(undefined, _eval('#!'));
  66. assert.areEqual(undefined, _eval('#!\n'));
  67. assert.areEqual(1, _eval('#!\n1'));
  68. assert.areEqual(undefined, _eval('#!2\n'));
  69. }
  70. },
  71. {
  72. name: "Invalid hashbang in ordinary script",
  73. body: function () {
  74. for (a of invalidScripts) {
  75. assert.throws(()=>WScript.LoadScript(a[0]), SyntaxError, a[1]);
  76. }
  77. }
  78. },
  79. {
  80. name: "Invalid hashbang in module script",
  81. body: function () {
  82. for (a of invalidScripts) {
  83. assert.throws(()=>WScript.LoadModule(a[0]), SyntaxError, a[1]);
  84. }
  85. }
  86. },
  87. {
  88. name: "Invalid hashbang in eval",
  89. body: function () {
  90. for (a of invalidScripts) {
  91. assert.throws(()=>eval(a[0]), SyntaxError, a[1]);
  92. }
  93. }
  94. },
  95. {
  96. name: "Invalid hashbang in indirect eval",
  97. body: function () {
  98. let _eval = eval;
  99. for (a of invalidScripts) {
  100. assert.throws(()=>_eval(a[0]), SyntaxError, a[1]);
  101. }
  102. }
  103. },
  104. ];
  105. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });