Skip to content

Commit

Permalink
feat: #782 - Add Node#getProject()
Browse files Browse the repository at this point in the history
Closes #782.
  • Loading branch information
dsherret committed Feb 8, 2020
1 parent c997339 commit 35f6bcf
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"fs-extra": "^8.1.0",
"is-negated-glob": "^1.0.0",
"multimatch": "^4.0.0",
"typescript": "~3.7.2"
"typescript": "~3.7.5"
},
"devDependencies": {
"@ts-morph/scripts": "~0.2.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/common/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,10 @@ typescript@^3.5.3, typescript@~3.7.2:
version "3.7.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.2.tgz#27e489b95fa5909445e9fef5ee48d81697ad18fb"

typescript@~3.7.5:
version "3.7.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.5.tgz#0692e21f65fd4108b9330238aac11dd2e177a1ae"

unc-path-regex@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/unc-path-regex/-/unc-path-regex-0.1.2.tgz#e73dd3d7b0d7c5ed86fbac6b0ae7d8c6a69d50fa"
Expand Down
2 changes: 2 additions & 0 deletions packages/ts-morph/lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3421,6 +3421,8 @@ export declare class Node<NodeType extends ts.Node = ts.Node> {
getCombinedModifierFlags(): ts.ModifierFlags;
/** Gets the source file. */
getSourceFile(): SourceFile;
/** Gets the project. */
getProject(): Project;
/**
* Gets a compiler node property wrapped in a Node.
* @param propertyName - Property name.
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-morph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"ts-nameof": "^4.1.0",
"ts-node": "8.4.1",
"ttypescript": "1.5.8",
"typescript": "3.7.2"
"typescript": "3.7.5"
},
"standard-version": {
"tagPrefix": "",
Expand Down
10 changes: 7 additions & 3 deletions packages/ts-morph/src/compiler/ast/common/Node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CodeBlockWriter } from "../../../codeBlockWriter";
import { errors, ArrayUtils, StoredComparer, getSyntaxKindName, StringUtils, ts, SyntaxKind, SymbolFlags } from "@ts-morph/common";
import { ProjectContext } from "../../../ProjectContext";
import { Project } from "../../../Project";
import * as compiler from "../../../compiler";
import { getNextMatchingPos, getNextNonWhitespacePos, getPreviousNonWhitespacePos, getPreviousMatchingPos, getTextFromTextChanges, insertIntoParentTextRange,
replaceSourceFileTextForFormatting, replaceSourceFileTextStraight, hasNewLineInRange } from "../../../manipulation";
Expand Down Expand Up @@ -1059,13 +1060,16 @@ export class Node<NodeType extends ts.Node = ts.Node> {
return ts.getCombinedModifierFlags(this.compilerNode as any as ts.Declaration);
}

/**
* Gets the source file.
*/
/** Gets the source file. */
getSourceFile(): SourceFile {
return this._sourceFile;
}

/** Gets the project. */
getProject(): Project {
return this._context.project;
}

/**
* Gets a compiler node property wrapped in a Node.
* @param propertyName - Property name.
Expand Down
18 changes: 17 additions & 1 deletion packages/ts-morph/src/tests/compiler/ast/common/nodeTests.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { expect } from "chai";
import { assert, IsExact, IsNullable } from "conditional-type-checks";
import { errors, NewLineKind, SyntaxKind, ts, SymbolFlags } from "@ts-morph/common";
import { CodeBlockWriter } from "../../../../codeBlockWriter";
import { CallExpression, ClassDeclaration, EnumDeclaration, FormatCodeSettings, FunctionDeclaration, Identifier, InterfaceDeclaration, Node,
PropertyAccessExpression, PropertySignature, SourceFile, TypeParameterDeclaration, ForEachDescendantTraversalControl, VariableStatement, ForStatement,
ForOfStatement, ForInStatement, NumericLiteral, StringLiteral, ExpressionStatement, NodeParentType } from "../../../../compiler";
import { hasParsedTokens } from "../../../../compiler/ast/utils";
import { Project } from "../../../../Project";
import { errors, NewLineKind, SyntaxKind, ts, SymbolFlags } from "@ts-morph/common";
import { createWrappedNode } from "../../../../utils/compiler/createWrappedNode";
import { WriterFunction } from "../../../../types";
import { getInfoFromText } from "../../testHelpers";

Expand Down Expand Up @@ -2167,4 +2168,19 @@ class MyClass {
);
});
});

describe(nameof<Node>(n => n.getProject), () => {
it("should get the project", () => {
const project = new Project({ useInMemoryFileSystem: true });
const sourceFile = project.createSourceFile("test.ts", "");
expect(sourceFile.getProject()).to.equal(project);
});

it("should throw if the node was created via createWrappedNode", () => {
const project = new Project({ useInMemoryFileSystem: true });
const sourceFile = project.createSourceFile("test.ts", "");
const node = createWrappedNode(sourceFile.compilerNode);
expect(() => node.getProject()).to.throw();
});
});
});
6 changes: 5 additions & 1 deletion packages/ts-morph/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1766,7 +1766,11 @@ type-detect@^4.0.0, type-detect@^4.0.5:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"

typescript@3.7.2, typescript@^3.5.3, typescript@~3.7.2:
typescript@3.7.5:
version "3.7.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.5.tgz#0692e21f65fd4108b9330238aac11dd2e177a1ae"

typescript@^3.5.3, typescript@~3.7.2:
version "3.7.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.2.tgz#27e489b95fa5909445e9fef5ee48d81697ad18fb"

Expand Down

0 comments on commit 35f6bcf

Please sign in to comment.