Skip to content

Commit

Permalink
feat: TypeScript 5.5 (#1545)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Upgraded to TypeScript 5.5
  • Loading branch information
dsherret authored Jun 22, 2024
1 parent 2bee533 commit 868755d
Show file tree
Hide file tree
Showing 32 changed files with 1,554 additions and 2,438 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ npm install --save-dev ts-morph
Or if you're using Deno:

```
deno add @ts-morph/ts-morph
deno add ts-morph@jsr:@ts-morph/ts-morph
```

## Documentation Progress
Expand Down
6 changes: 3 additions & 3 deletions dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"dist-deno"
],
"plugins": [
"https://plugins.dprint.dev/typescript-0.88.6.wasm",
"https://plugins.dprint.dev/json-0.19.1.wasm",
"https://plugins.dprint.dev/markdown-0.16.3.wasm"
"https://plugins.dprint.dev/typescript-0.91.1.wasm",
"https://plugins.dprint.dev/json-0.19.3.wasm",
"https://plugins.dprint.dev/markdown-0.17.1.wasm"
]
}
1,086 changes: 354 additions & 732 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"packages/ts-morph"
],
"devDependencies": {
"dprint": "^0.43.2",
"every-ts": "^2.0.0"
"dprint": "^0.46.3"
}
}
16 changes: 8 additions & 8 deletions packages/bootstrap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@
},
"devDependencies": {
"@rollup/plugin-typescript": "^11.1.6",
"@types/chai": "^4.3.12",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.25",
"@types/chai": "^4.3.16",
"@types/mocha": "^10.0.7",
"@types/node": "^20.14.8",
"chai": "^4.3.10",
"cross-env": "^7.0.3",
"mocha": "^10.3.0",
"rimraf": "^5.0.5",
"rollup": "=4.12.1",
"mocha": "^10.4.0",
"rimraf": "^5.0.7",
"rollup": "=4.18.0",
"ts-node": "^10.9.2",
"tslib": "^2.6.2",
"typescript": "~5.4.2"
"tslib": "^2.6.3",
"typescript": "~5.5.2"
},
"publishConfig": {
"access": "public"
Expand Down
2,338 changes: 933 additions & 1,405 deletions packages/common/lib/typescript.d.ts

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@
},
"dependencies": {
"fast-glob": "^3.3.2",
"minimatch": "^9.0.3",
"minimatch": "^9.0.4",
"mkdirp": "^3.0.1",
"path-browserify": "^1.0.1"
},
"devDependencies": {
"@rollup/plugin-typescript": "^11.1.6",
"@types/chai": "^4.3.12",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.25",
"@types/chai": "^4.3.16",
"@types/mocha": "^10.0.7",
"@types/node": "^20.14.8",
"chai": "^4.3.10",
"cross-env": "^7.0.3",
"mocha": "^10.3.0",
"rimraf": "^5.0.5",
"rollup": "=4.12.1",
"mocha": "^10.4.0",
"rimraf": "^5.0.7",
"rollup": "=4.18.0",
"ts-node": "^10.9.2",
"tslib": "^2.6.2",
"typescript": "5.4.2"
"tslib": "^2.6.3",
"typescript": "5.5.2"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 0 additions & 4 deletions packages/common/scripts/bundleLocalTs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,4 @@ nodeWithTypeArgs.insertProperty(0, {
name: "_nodeWithTypeArgumentsBrand",
type: "any",
});
for (const qualifiedName of tsFile.getDescendantsOfKind(tsMorph.ts.SyntaxKind.QualifiedName)) {
if (qualifiedName.getLeft().getText() === "ts")
qualifiedName.replaceWithText(qualifiedName.getText().replace(/^ts\./, ""));
}
tsFile.saveSync();
322 changes: 176 additions & 146 deletions packages/common/src/data/libFiles.ts

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions packages/scripts/utils/Memoize.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/** Decorator for memoizing the result of a method. */
export function Memoize(target: (...args: any[]) => void) {
if (target instanceof Function) {
if (target instanceof Function)
return getNewFunction(target);
} else {
else
throw new Error("Not implemented.");
}
}

const weakMap = new WeakMap<object, Map<string, unknown>>();
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-morph/breaking-changes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Breaking Changes
# Breaking Changes

View [CHANGELOG.md](CHANGELOG.md) for more detail on releases. This file is only a high level overview of breaking changes.

Expand Down
4 changes: 3 additions & 1 deletion packages/ts-morph/lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9525,6 +9525,8 @@ export declare class EmitOutput {
private constructor();
/** TypeScript compiler emit result. */
get compilerObject(): ts.EmitOutput;
/** Gets the diagnostics. */
getDiagnostics(): Diagnostic<ts.Diagnostic>[];
/** Gets if the emit was skipped. */
getEmitSkipped(): boolean;
/** Gets the output files. */
Expand Down Expand Up @@ -10071,7 +10073,7 @@ export declare class Type<TType extends ts.Type = ts.Type> {
* Gets the object flags.
* @remarks Returns 0 for a non-object type.
*/
getObjectFlags(): 0 | ObjectFlags.Class | ObjectFlags.Interface | ObjectFlags.Reference | ObjectFlags.Tuple | ObjectFlags.Anonymous | ObjectFlags.Mapped | ObjectFlags.Instantiated | ObjectFlags.ObjectLiteral | ObjectFlags.EvolvingArray | ObjectFlags.ObjectLiteralPatternWithComputedProperties | ObjectFlags.ReverseMapped | ObjectFlags.JsxAttributes | ObjectFlags.JSLiteral | ObjectFlags.FreshLiteral | ObjectFlags.ArrayLiteral | ObjectFlags.ClassOrInterface | ObjectFlags.ContainsSpread | ObjectFlags.ObjectRestType | ObjectFlags.InstantiationExpressionType;
getObjectFlags(): 0 | ObjectFlags.Class | ObjectFlags.Interface | ObjectFlags.Reference | ObjectFlags.Tuple | ObjectFlags.Anonymous | ObjectFlags.Mapped | ObjectFlags.Instantiated | ObjectFlags.ObjectLiteral | ObjectFlags.EvolvingArray | ObjectFlags.ObjectLiteralPatternWithComputedProperties | ObjectFlags.ReverseMapped | ObjectFlags.JsxAttributes | ObjectFlags.JSLiteral | ObjectFlags.FreshLiteral | ObjectFlags.ArrayLiteral | ObjectFlags.ClassOrInterface | ObjectFlags.ContainsSpread | ObjectFlags.ObjectRestType | ObjectFlags.InstantiationExpressionType | ObjectFlags.SingleSignatureType;
}

export declare class TypeParameter extends Type<ts.TypeParameter> {
Expand Down
16 changes: 8 additions & 8 deletions packages/ts-morph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@
},
"devDependencies": {
"@rollup/plugin-typescript": "^11.1.6",
"@types/chai": "^4.3.12",
"@types/diff": "^5.0.9",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.25",
"@types/chai": "^4.3.16",
"@types/diff": "^5.2.1",
"@types/mocha": "^10.0.7",
"@types/node": "^20.14.8",
"chai": "^4.3.10",
"conditional-type-checks": "^1.0.6",
"cross-env": "^7.0.3",
"diff": "^5.2.0",
"mocha": "10.3.0",
"rimraf": "^5.0.5",
"rollup": "=4.12.1",
"mocha": "10.4.0",
"rimraf": "^5.0.7",
"rollup": "=4.18.0",
"ts-node": "10.9.2",
"typescript": "~5.3.2"
"typescript": "~5.5.2"
},
"standard-version": {
"tagPrefix": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-morph/readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ts-morph
# ts-morph

[![npm version](https://badge.fury.io/js/ts-morph.svg)](https://badge.fury.io/js/ts-morph)
[![CI](https:/dsherret/ts-morph/workflows/CI/badge.svg)](https:/dsherret/ts-morph/actions?query=workflow%3ACI)
Expand Down
4 changes: 2 additions & 2 deletions packages/ts-morph/scripts/buildDeno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fileSystem.writeFileSync(
fileSystem.writeFileSync(
`${destPath}/readme.md`,
`
# ts_morph
# ts-morph
To use:
Expand All @@ -59,7 +59,7 @@ import { Project } from "ts-morph";
Or use \`@ts-morph/bootstrap\`:
\`\`\`
deno add @ts-morph/ts-morph
deno add @ts-morph/bootstrap
\`\`\`
\`\`\`ts
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-morph/scripts/readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Scripts
# Scripts

This folder contains the code that verifies, manipulates, and generates code for ts-morph.
20 changes: 9 additions & 11 deletions packages/ts-morph/src/compiler/ast/common/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { Expression } from "../expression";
import { KindToNodeMappings } from "../kindToNodeMappings.generated";
import { SourceFile } from "../module";
import { Statement, StatementedNode } from "../statement";
import { ExtendedParser, hasParsedTokens } from "../utils";
import { ExtendedParser } from "../utils";
import { SyntaxList } from "./SyntaxList";
import { TextRange } from "./TextRange";
import { ForEachDescendantTraversalControl, TransformTraversalControl } from "./TraversalControl";
Expand Down Expand Up @@ -133,10 +133,6 @@ export class Node<NodeType extends ts.Node = ts.Node> {
if (parent != null)
parent._wrappedChildCount--;

const parentSyntaxList = this._getParentSyntaxListIfWrapped();
if (parentSyntaxList != null)
parentSyntaxList._wrappedChildCount--;

this.#storeTextForForgetting();
this._context.compilerFactory.removeNodeFromCache(this);
this._clearInternals();
Expand All @@ -156,7 +152,9 @@ export class Node<NodeType extends ts.Node = ts.Node> {
* @internal
*/
_hasWrappedChildren() {
return this._wrappedChildCount > 0;
// always consider syntax lists as having warpped children because tracking
// them is more work than it's worth
return this._wrappedChildCount > 0 || this.#compilerNode?.kind === SyntaxKind.SyntaxList;
}

/**
Expand Down Expand Up @@ -1290,7 +1288,7 @@ export class Node<NodeType extends ts.Node = ts.Node> {
*/
_getParentSyntaxListIfWrapped(): SyntaxList | undefined {
const parent = this.getParent();
if (parent == null || !hasParsedTokens(parent.compilerNode))
if (parent == null || !ExtendedParser.hasParsedTokens(parent.compilerNode))
return undefined;
return this.getParentSyntaxList();
}
Expand Down Expand Up @@ -1926,15 +1924,15 @@ export class Node<NodeType extends ts.Node = ts.Node> {
* Gets the compiler children of the node.
* @internal
*/
_getCompilerChildren(): ts.Node[] {
_getCompilerChildren(): readonly ts.Node[] {
return ExtendedParser.getCompilerChildren(this.compilerNode, this._sourceFile.compilerNode);
}

/**
* Gets the compiler children of the node using .forEachChild
* @internal
*/
_getCompilerForEachChildren(): ts.Node[] {
_getCompilerForEachChildren(): readonly ts.Node[] {
return ExtendedParser.getCompilerForEachChildren(this.compilerNode, this._sourceFile.compilerNode);
}

Expand All @@ -1943,15 +1941,15 @@ export class Node<NodeType extends ts.Node = ts.Node> {
* @internal
*/
_getCompilerChildrenFast() {
return hasParsedTokens(this.compilerNode) ? this._getCompilerChildren() : this._getCompilerForEachChildren();
return ExtendedParser.hasParsedTokens(this.compilerNode) ? this._getCompilerChildren() : this._getCompilerForEachChildren();
}

/**
* Gets the compiler children of the specified kind.
* @internal
*/
_getCompilerChildrenOfKind(kind: SyntaxKind) {
const children: ts.Node[] = useParseTreeSearchForKind(this, kind) ? this._getCompilerForEachChildren() : this._getCompilerChildren();
const children: readonly ts.Node[] = useParseTreeSearchForKind(this, kind) ? this._getCompilerForEachChildren() : this._getCompilerChildren();
return children.filter(c => c.kind === kind);
}

Expand Down
33 changes: 19 additions & 14 deletions packages/ts-morph/src/compiler/ast/utils/ExtendedParser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { SyntaxKind, ts } from "@ts-morph/common";
import { CommentNodeParser, ContainerNodes } from "./CommentNodeParser";
import { hasParsedTokens } from "./hasParsedTokens";

const forEachChildSaver = new WeakMap<ts.Node, ts.Node[]>();
const getChildrenSaver = new WeakMap<ts.Node, ts.Node[]>();
const getChildrenSaver = new WeakMap<ts.Node, readonly ts.Node[]>();

/**
* Parser that parses around nodes for comments.
Expand All @@ -14,8 +13,13 @@ export class ExtendedParser {
return CommentNodeParser.getOrParseChildren(container, sourceFile);
}

static hasParsedTokens(node: ts.Node) {
// if this is true, it means the compiler has previously parsed the tokens
return getChildrenSaver.has(node) || node.kind == SyntaxKind.SyntaxList;
}

static getCompilerChildrenFast(node: ts.Node, sourceFile: ts.SourceFile) {
if (hasParsedTokens(node))
if (ExtendedParser.hasParsedTokens(node))
return ExtendedParser.getCompilerChildren(node, sourceFile);

return ExtendedParser.getCompilerForEachChildren(node, sourceFile);
Expand Down Expand Up @@ -43,20 +47,21 @@ export class ExtendedParser {
}
}

static getCompilerChildren(node: ts.Node, sourceFile: ts.SourceFile) {
if (isStatementMemberOrPropertyHoldingSyntaxList()) {
let result = getChildrenSaver.get(node);
if (result == null) {
static getCompilerChildren(node: ts.Node, sourceFile: ts.SourceFile): readonly ts.Node[] {
let result = getChildrenSaver.get(node);
if (result == null) {
if (isStatementMemberOrPropertyHoldingSyntaxList()) {
// @code-fence-allow(getChildren): This merges in comment nodes.
result = [...node.getChildren(sourceFile)]; // make a copy; do not modify the compiler api's array
mergeInComments(result, CommentNodeParser.getOrParseChildren(node as ts.SyntaxList, sourceFile));
getChildrenSaver.set(node, result);
const newArray = [...node.getChildren(sourceFile)]; // make a copy; do not modify the compiler api's array
mergeInComments(newArray, CommentNodeParser.getOrParseChildren(node as ts.SyntaxList, sourceFile));
result = newArray;
} else {
// @code-fence-allow(getChildren): No need to merge in comment nodes.
result = node.getChildren(sourceFile);
}
return result;
getChildrenSaver.set(node, result);
}

// @code-fence-allow(getChildren): No need to merge in comment nodes.
return node.getChildren(sourceFile);
return result;

function isStatementMemberOrPropertyHoldingSyntaxList() {
if (node.kind !== ts.SyntaxKind.SyntaxList)
Expand Down
6 changes: 0 additions & 6 deletions packages/ts-morph/src/compiler/ast/utils/hasParsedTokens.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/ts-morph/src/compiler/ast/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./CommentNodeParser";
export * from "./ExtendedParser";
export * from "./hasParsedTokens";
export * from "./isComment";
2 changes: 1 addition & 1 deletion packages/ts-morph/src/compiler/tools/LanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export class LanguageService {
function getCompilerEmitOutput(): ts.EmitOutput {
const program = compilerObject.getProgram();
if (program == null || program.getSourceFile(filePath) == null)
return { emitSkipped: true, outputFiles: [] };
return { emitSkipped: true, outputFiles: [], diagnostics: [] };
return compilerObject.getEmitOutput(filePath, emitOnlyDtsFiles);
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/ts-morph/src/compiler/tools/results/EmitOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export class EmitOutput {
return this.#compilerObject;
}

/** Gets the diagnostics. */
@Memoize
getDiagnostics() {
return this.compilerObject.diagnostics.map(d => this.#context.compilerFactory.getDiagnostic(d));
}

/**
* Gets if the emit was skipped.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class ChangeChildOrderParentHandler implements NodeHandler {
this.#compilerFactory.replaceCompilerNode(currentNode, newNode);
}

#getChildrenInNewOrder(children: ts.Node[]) {
#getChildrenInNewOrder(children: readonly ts.Node[]) {
const result = [...children];
const movingNode = result.splice(this.#oldIndex, 1)[0];
result.splice(this.#newIndex, 0, movingNode);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ArrayUtils, SyntaxKind, ts } from "@ts-morph/common";
import { Node, SourceFile } from "../../compiler";
import { ExtendedParser, hasParsedTokens } from "../../compiler/ast/utils";
import { ExtendedParser } from "../../compiler/ast/utils";
import { CompilerFactory } from "../../factories";
import { AdvancedIterator } from "../../utils";
import { NodeHandler } from "./NodeHandler";
Expand Down Expand Up @@ -36,7 +36,7 @@ export class NodeHandlerHelper {
];
}

getCompilerChildren(currentNode: Node, newNode: ts.Node, newSourceFile: ts.SourceFile): [ts.Node[], ts.Node[]] {
getCompilerChildren(currentNode: Node, newNode: ts.Node, newSourceFile: ts.SourceFile): [readonly ts.Node[], readonly ts.Node[]] {
const currentCompilerNode = currentNode.compilerNode;
const currentSourceFile = currentNode._sourceFile.compilerNode;

Expand All @@ -49,10 +49,10 @@ export class NodeHandlerHelper {
/**
* Gets the children of the node according to whether the tokens have previously been parsed.
*/
getChildrenFast(currentNode: Node, newNode: ts.Node, newSourceFile: ts.SourceFile): [ts.Node[], ts.Node[]] {
getChildrenFast(currentNode: Node, newNode: ts.Node, newSourceFile: ts.SourceFile): [readonly ts.Node[], readonly ts.Node[]] {
const currentCompilerNode = currentNode.compilerNode;
const currentSourceFile = currentNode._sourceFile.compilerNode;
if (hasParsedTokens(currentCompilerNode)) {
if (ExtendedParser.hasParsedTokens(currentCompilerNode)) {
return [
ExtendedParser.getCompilerChildren(currentCompilerNode, currentSourceFile),
ExtendedParser.getCompilerChildren(newNode, newSourceFile),
Expand Down
Loading

0 comments on commit 868755d

Please sign in to comment.