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