UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

43 lines (41 loc) 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const ts = require("typescript"); const createWrappedNode_1 = require("./../createWrappedNode"); const errors = require("./../errors"); describe("createWrappedNode", () => { it("should throw an exception if passing in a node not created with setParentNodes set to true.", () => { const sourceFile = ts.createSourceFile("file.ts", "class MyClass {}", ts.ScriptTarget.ES2016, false); let child; ts.forEachChild(sourceFile, node => child = node); chai_1.expect(child.parent).to.equal(undefined); chai_1.expect(() => { createWrappedNode_1.createWrappedNode(child); }).to.throw(errors.InvalidOperationError, "Please ensure the node was created from a source file with 'setParentNodes' set to 'true'."); }); it("should get a wrapped node", () => { const compilerSourceFile = ts.createSourceFile("file.ts", "class MyClass {}", ts.ScriptTarget.ES2016, true); const child = compilerSourceFile.getChildren()[0]; const node = createWrappedNode_1.createWrappedNode(child); const sourceFile = node.getSourceFile(); chai_1.expect(node.getKind()).to.equal(ts.SyntaxKind.SyntaxList); chai_1.expect(sourceFile.getClasses().length).to.equal(1); }); it("should get a wrapped node when also providing the source file", () => { const compilerSourceFile = ts.createSourceFile("file.ts", "class MyClass {}", ts.ScriptTarget.ES2016, true); const child = compilerSourceFile.getChildren()[0]; const node = createWrappedNode_1.createWrappedNode(child, compilerSourceFile); const sourceFile = node.getSourceFile(); // testing for something arbitrary chai_1.expect(sourceFile.getClasses().length).to.equal(1); }); it("should get a wrapped node when also providing the wrapped source file", () => { const sourceFile = createWrappedNode_1.createWrappedNode(ts.createSourceFile("file.ts", "class MyClass {}", ts.ScriptTarget.ES2016, true)); const firstClass = sourceFile.getClasses()[0]; const node = createWrappedNode_1.createWrappedNode(firstClass.compilerNode, sourceFile); chai_1.expect(node).to.equal(firstClass); chai_1.expect(node.getSourceFile()).to.equal(sourceFile); }); }); //# sourceMappingURL=createWrappedNodeTests.js.map