1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { expect } from 'chai'; 17import { readFileSync } from "fs"; 18import * as ts from "typescript"; 19import { isStrictMode, setGlobalStrict } from '../../src/strictMode'; 20import { isFunctionLikeDeclaration } from '../../src/syntaxCheckHelper'; 21import { getCompileOptions } from '../utils/base'; 22import * as jshelpers from '../../src/jshelpers'; 23 24function createSourceFile(filename: string): ts.SourceFile { 25 let sourceFile = ts.createSourceFile( 26 filename, 27 readFileSync(filename).toString(), 28 ts.ScriptTarget.ES5, 29 /*setParentNodes */ true 30 ); 31 32 return sourceFile; 33} 34 35function recordNodes(node: ts.Node, flag: boolean): boolean { 36 node.forEachChild(childNode => { 37 expect(isStrictMode(childNode) == flag).to.be.true; 38 recordNodes(childNode, flag); 39 }); 40 41 return false; 42} 43 44describe("strict_mode", function () { 45 it('global strict mode', function () { 46 let node = createSourceFile("tests/strictmode/global.js"); 47 setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(node, getCompileOptions())); 48 expect(isStrictMode(node)).to.be.true; 49 recordNodes(node, true); 50 }); 51 52 it('function strict mode', function () { 53 let node = createSourceFile("tests/strictmode/function.js"); 54 setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(node, getCompileOptions())); 55 expect(isStrictMode(node)).to.be.false; 56 node.forEachChild(childNode => { 57 if (isFunctionLikeDeclaration(childNode)) { 58 let funcId = <ts.Identifier>(<ts.FunctionDeclaration>childNode).name; 59 let funcName = jshelpers.getTextOfIdentifierOrLiteral(funcId); 60 if (funcName == "add") { 61 recordNodes(childNode, true); 62 } else { 63 recordNodes(childNode, false); 64 } 65 } else { 66 recordNodes(childNode, false); 67 } 68 }); 69 }); 70 71 it('function nest1 strict mode', function () { 72 let node = createSourceFile("tests/strictmode/function_nest1.js"); 73 setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(node, getCompileOptions())); 74 expect(isStrictMode(node)).to.be.false; 75 node.forEachChild(childNode => { 76 if (isFunctionLikeDeclaration(childNode)) { 77 recordNodes(childNode, true); 78 } else { 79 recordNodes(childNode, false); 80 } 81 }); 82 }); 83 84 it('function nest2 strict mode', function () { 85 let node = createSourceFile("tests/strictmode/function_nest2.js"); 86 setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(node, getCompileOptions())); 87 expect(isStrictMode(node)).to.be.false; 88 node.forEachChild(childNode => { 89 if (isFunctionLikeDeclaration(childNode)) { 90 childNode.body!.forEachChild(funcNode => { 91 if (isFunctionLikeDeclaration(funcNode)) { 92 recordNodes(funcNode, true); 93 } else { 94 recordNodes(funcNode, false); 95 } 96 }); 97 } else { 98 recordNodes(childNode, false); 99 } 100 }); 101 }); 102}); 103 104