Skip to content

Commit

Permalink
feat: upgrade to ts 4.8 (#1316)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Sep 3, 2022
1 parent a9ba0ec commit 8a87a1b
Show file tree
Hide file tree
Showing 57 changed files with 12,762 additions and 8,257 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -8,11 +8,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: denoland/setup-deno@v1
- uses: actions/setup-node@v2
with:
node-version: '16'
check-latest: true
- uses: bvm/gh-action@v1.1.0
- name: Build
run: |
npm ci
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019 David Sherret
Copyright (c) 2017-2022 David Sherret

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
14 changes: 0 additions & 14 deletions bvm.json

This file was deleted.

2 changes: 1 addition & 1 deletion deno/bootstrap/ts_morph_bootstrap.js
@@ -1,4 +1,4 @@
import { DocumentRegistry, StringUtils, ts, FileUtils, Memoize, TransactionalFileSystem, TsConfigResolver, errors, InMemoryFileSystemHost, RealFileSystemHost, CompilerOptionsContainer, createHosts, runtime, createModuleResolutionHost } from '../common/mod.ts';
import { DocumentRegistry, StringUtils, ts, FileUtils, TransactionalFileSystem, TsConfigResolver, errors, InMemoryFileSystemHost, RealFileSystemHost, CompilerOptionsContainer, createHosts, runtime, createModuleResolutionHost, Memoize } from '../common/mod.ts';
export { CompilerOptionsContainer, InMemoryFileSystemHost, ResolutionHosts, SettingsContainer, ts } from '../common/mod.ts';

/******************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion deno/common/DenoRuntime.ts
Expand Up @@ -100,7 +100,7 @@ class DenoRuntimeFileSystem {
return this._toStat(stat);
}

private _toStat(stat: Deno.FileInfo) {
private _toStat(stat: any) {
return {
isFile() {
return stat.isFile;
Expand Down
3 changes: 0 additions & 3 deletions deno/common/ts_morph_common.d.ts
Expand Up @@ -1062,9 +1062,6 @@ export declare function nameof<TObject>(key: keyof TObject): string;
export declare class ObjectUtils {
private constructor();
static clone<T>(obj: T): T;
static assign<T, U>(a: T, b: U): T & U;
static assign<T, U, V>(a: T, b: U, c: V): T & U & V;
private static es5Assign;
}

export declare class StringUtils {
Expand Down
62 changes: 20 additions & 42 deletions deno/common/ts_morph_common.js

Large diffs are not rendered by default.

616 changes: 497 additions & 119 deletions deno/common/typescript.d.ts

Large diffs are not rendered by default.

13,550 changes: 8,834 additions & 4,716 deletions deno/common/typescript.js

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions deno/ts_morph.d.ts
Expand Up @@ -1100,7 +1100,7 @@ export interface DecoratableNode {
insertDecorators(index: number, structures: ReadonlyArray<OptionalKind<DecoratorStructure>>): Decorator[];
}

declare type DecoratableNodeExtensionType = Node<ts.Node>;
declare type DecoratableNodeExtensionType = Node<ts.Node> & ModifierableNode;
export declare function DotDotDotTokenableNode<T extends Constructor<DotDotDotTokenableNodeExtensionType>>(Base: T): Constructor<DotDotDotTokenableNode> & T;

export interface DotDotDotTokenableNode {
Expand Down Expand Up @@ -1370,7 +1370,7 @@ export declare function ModifierableNode<T extends Constructor<ModifierableNodeE

export interface ModifierableNode {
/** Gets the node's modifiers. */
getModifiers(): Node[];
getModifiers(): Node<ts.Modifier>[];
/**
* Gets the first modifier of the specified syntax kind or throws if none found.
* @param kind - Syntax kind.
Expand Down Expand Up @@ -2939,7 +2939,6 @@ export declare abstract class CompilerCommentNode implements ts.Node {
end: number;
kind: SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia;
flags: ts.NodeFlags;
decorators?: ts.NodeArray<ts.Decorator> | undefined;
modifiers?: ts.NodeArray<ts.Modifier> | undefined;
parent: ts.Node;
protected constructor();
Expand Down Expand Up @@ -6358,9 +6357,9 @@ declare const JsxAttributeBase: Constructor<NamedNode> & typeof Node;

export declare class JsxAttribute extends JsxAttributeBase<ts.JsxAttribute> {
/** Gets the JSX attribute's initializer or throws if it doesn't exist. */
getInitializerOrThrow(): StringLiteral | JsxExpression;
getInitializerOrThrow(): StringLiteral | JsxElement | JsxSelfClosingElement | JsxFragment | JsxExpression;
/** Gets the JSX attribute's initializer or returns undefined if it doesn't exist. */
getInitializer(): StringLiteral | JsxExpression | undefined;
getInitializer(): JsxElement | JsxExpression | JsxFragment | JsxSelfClosingElement | StringLiteral | undefined;
/**
* Sets the initializer.
* @param textOrWriterFunction - Text or writer function to set the initializer with.
Expand Down Expand Up @@ -6696,7 +6695,7 @@ export interface ImplementedKindToNodeMappings {
[SyntaxKind.VariableDeclaration]: VariableDeclaration;
[SyntaxKind.VariableDeclarationList]: VariableDeclarationList;
[SyntaxKind.VariableStatement]: VariableStatement;
[SyntaxKind.JSDocComment]: JSDoc;
[SyntaxKind.JSDoc]: JSDoc;
[SyntaxKind.TypeOfExpression]: TypeOfExpression;
[SyntaxKind.WhileStatement]: WhileStatement;
[SyntaxKind.WithStatement]: WithStatement;
Expand Down
49 changes: 27 additions & 22 deletions deno/ts_morph.js
@@ -1,7 +1,7 @@
import { errors, SyntaxKind, ts, NewLineKind, EmitHint, ScriptKind, ScriptTarget, SettingsContainer, KeyValueCache, getCompilerOptionsFromTsConfig as getCompilerOptionsFromTsConfig$1, ObjectUtils, StringUtils, getSyntaxKindName, ArrayUtils, nameof, EventContainer, FileUtils, libFolderInMemoryPath, Memoize, SymbolFlags, TypeFormatFlags, getEmitModuleResolutionKind, createHosts, ObjectFlags, TypeFlags, matchGlobs, ModuleResolutionKind, SortedKeyValueArray, LocaleStringComparer, WeakCache, DocumentRegistry, createModuleResolutionHost, TransactionalFileSystem, TsConfigResolver, CompilerOptionsContainer, InMemoryFileSystemHost, RealFileSystemHost, IterableUtils, runtime } from './common/mod.ts';
import { errors, SyntaxKind, ts, NewLineKind, EmitHint, ScriptTarget, ScriptKind, SettingsContainer, KeyValueCache, getCompilerOptionsFromTsConfig as getCompilerOptionsFromTsConfig$1, StringUtils, getSyntaxKindName, ArrayUtils, nameof, ObjectUtils, EventContainer, FileUtils, libFolderInMemoryPath, Memoize, SymbolFlags, TypeFormatFlags, getEmitModuleResolutionKind, createHosts, ObjectFlags, TypeFlags, matchGlobs, ModuleResolutionKind, SortedKeyValueArray, LocaleStringComparer, WeakCache, DocumentRegistry, createModuleResolutionHost, TransactionalFileSystem, TsConfigResolver, CompilerOptionsContainer, InMemoryFileSystemHost, RealFileSystemHost, IterableUtils, runtime } from './common/mod.ts';
export { CompilerOptionsContainer, DiagnosticCategory, EmitHint, InMemoryFileSystemHost, LanguageVariant, ModuleKind, ModuleResolutionKind, NewLineKind, NodeFlags, ObjectFlags, ResolutionHosts, ScriptKind, ScriptTarget, SettingsContainer, SymbolFlags, SyntaxKind, TypeFlags, TypeFormatFlags, ts } from './common/mod.ts';
import CodeBlockWriter from 'https://deno.land/x/code_block_writer@11.0.0/mod.ts';
export { default as CodeBlockWriter } from 'https://deno.land/x/code_block_writer@11.0.0/mod.ts';
import CodeBlockWriter from 'https://deno.land/x/code_block_writer@11.0.3/mod.ts';
export { default as CodeBlockWriter } from 'https://deno.land/x/code_block_writer@11.0.3/mod.ts';

class AdvancedIterator {
constructor(iterator) {
Expand Down Expand Up @@ -535,7 +535,7 @@ function callBaseGetStructure(basePrototype, node, structure) {
else
newStructure = {};
if (structure != null)
ObjectUtils.assign(newStructure, structure);
Object.assign(newStructure, structure);
return newStructure;
}

Expand Down Expand Up @@ -924,22 +924,22 @@ function getRangeWithoutCommentsFromArray(array, index, length, expectedKind) {

function fromConstructorDeclarationOverload(node) {
const structure = {};
ObjectUtils.assign(structure, fromScopedNode(node));
Object.assign(structure, fromScopedNode(node));
return structure;
}
function fromFunctionDeclarationOverload(node) {
const structure = {};
ObjectUtils.assign(structure, fromAmbientableNode(node));
ObjectUtils.assign(structure, fromExportableNode(node));
Object.assign(structure, fromAmbientableNode(node));
Object.assign(structure, fromExportableNode(node));
return structure;
}
function fromMethodDeclarationOverload(node) {
const structure = {};
ObjectUtils.assign(structure, fromStaticableNode(node));
ObjectUtils.assign(structure, fromAbstractableNode(node));
ObjectUtils.assign(structure, fromScopedNode(node));
ObjectUtils.assign(structure, fromQuestionTokenableNode(node));
ObjectUtils.assign(structure, fromOverrideableNode(node));
Object.assign(structure, fromStaticableNode(node));
Object.assign(structure, fromAbstractableNode(node));
Object.assign(structure, fromScopedNode(node));
Object.assign(structure, fromQuestionTokenableNode(node));
Object.assign(structure, fromOverrideableNode(node));
return structure;
}

Expand Down Expand Up @@ -5856,19 +5856,19 @@ function DecoratableNode(Base) {
return errors.throwIfNullOrUndefined(this.getDecorator(nameOrFindFunction), () => getNotFoundErrorMessageForNameOrFindFunction("decorator", nameOrFindFunction));
}
getDecorators() {
var _a, _b;
return (_b = (_a = this.compilerNode.decorators) === null || _a === void 0 ? void 0 : _a.map(d => this._getNodeFromCompilerNode(d))) !== null && _b !== void 0 ? _b : [];
return getCompilerNodeDecorators(this.compilerNode).map(d => this._getNodeFromCompilerNode(d));
}
addDecorator(structure) {
return this.insertDecorator(getEndIndexFromArray(this.compilerNode.decorators), structure);
return this.insertDecorator(getEndIndexFromArray(getCompilerNodeDecorators(this.compilerNode)), structure);
}
addDecorators(structures) {
return this.insertDecorators(getEndIndexFromArray(this.compilerNode.decorators), structures);
return this.insertDecorators(getEndIndexFromArray(getCompilerNodeDecorators(this.compilerNode)), structures);
}
insertDecorator(index, structure) {
return this.insertDecorators(index, [structure])[0];
}
insertDecorators(index, structures) {
var _a, _b, _c, _d;
if (ArrayUtils.isNullOrEmpty(structures))
return [];
const decoratorLines = getDecoratorLines(this, structures);
Expand All @@ -5886,7 +5886,7 @@ function DecoratableNode(Base) {
nextFormattingKind: previousDecorator == null ? formattingKind : FormattingKind.None,
});
insertIntoParentTextRange({
parent: decorators.length === 0 ? this : decorators[0].getParentSyntaxListOrThrow(),
parent: (_d = (_b = (_a = decorators[0]) === null || _a === void 0 ? void 0 : _a.getParentSyntaxListOrThrow()) !== null && _b !== void 0 ? _b : (_c = this.getModifiers()[0]) === null || _c === void 0 ? void 0 : _c.getParentSyntaxListOrThrow()) !== null && _d !== void 0 ? _d : this,
insertPos: decorators[index - 1] == null ? this.getStart() : decorators[index - 1].getEnd(),
newText: decoratorCode,
});
Expand All @@ -5907,6 +5907,10 @@ function DecoratableNode(Base) {
}
};
}
function getCompilerNodeDecorators(node) {
var _a;
return ts.canHaveDecorators(node) ? (_a = ts.getDecorators(node)) !== null && _a !== void 0 ? _a : [] : [];
}
function getDecoratorLines(node, structures) {
const lines = [];
for (const structure of structures) {
Expand Down Expand Up @@ -9016,7 +9020,8 @@ function ModifierableNode(Base) {
return this;
}
addModifier(text) {
const modifiers = this.getModifiers();
const rawModifiers = this.getModifiers();
const modifiers = this.getModifiers().filter(m => m.getKind() !== SyntaxKind.Decorator);
const existingModifier = modifiers.find(m => m.getText() === text);
if (existingModifier != null)
return existingModifier;
Expand All @@ -9033,7 +9038,7 @@ function ModifierableNode(Base) {
startPos = insertPos + 1;
}
insertIntoParentTextRange({
parent: modifiers.length === 0 ? this : modifiers[0].getParentSyntaxListOrThrow(),
parent: rawModifiers.length === 0 ? this : rawModifiers[0].getParentSyntaxListOrThrow(),
insertPos,
newText,
});
Expand Down Expand Up @@ -13665,7 +13670,7 @@ function insertOverloads(opts) {
const index = verifyAndGetIndex(opts.index, overloadsCount);
const mainIndex = firstIndex + index;
const thisStructure = opts.getThisStructure(implementationNode);
const structures = opts.structures.map(structure => ObjectUtils.assign(ObjectUtils.assign({}, thisStructure), structure));
const structures = opts.structures.map(structure => Object.assign(Object.assign({}, thisStructure), structure));
const writer = implementationNode._getWriterWithQueuedIndentation();
for (const structure of structures) {
if (writer.getLength() > 0)
Expand Down Expand Up @@ -17818,13 +17823,13 @@ class LanguageService {
_getFilledSettings(settings) {
if (settings["_filled"])
return settings;
settings = ObjectUtils.assign(this._context.getFormatCodeSettings(), settings);
settings = Object.assign(this._context.getFormatCodeSettings(), settings);
fillDefaultFormatCodeSettings(settings, this._context.manipulationSettings);
settings["_filled"] = true;
return settings;
}
_getFilledUserPreferences(userPreferences) {
return ObjectUtils.assign(this._context.getUserPreferences(), userPreferences);
return Object.assign(this._context.getUserPreferences(), userPreferences);
}
}

Expand Down
12 changes: 8 additions & 4 deletions docs/emitting.md
Expand Up @@ -89,17 +89,21 @@ project.emit({
},
});

function visitSourceFile(sourceFile: ts.SourceFile, context: ts.TransformationContext, visitNode: (node: ts.Node) => ts.Node) {
function visitSourceFile(
sourceFile: ts.SourceFile,
context: ts.TransformationContext,
visitNode: (node: ts.Node, context: ts.TransformationContext) => ts.Node,
) {
return visitNodeAndChildren(sourceFile) as ts.SourceFile;

function visitNodeAndChildren(node: ts.Node): ts.Node {
return ts.visitEachChild(visitNode(node), visitNodeAndChildren, context);
return ts.visitEachChild(visitNode(node, context), visitNodeAndChildren, context);
}
}

function numericLiteralToStringLiteral(node: ts.Node) {
function numericLiteralToStringLiteral(node: ts.Node, context: ts.TransformationContext) {
if (ts.isNumericLiteral(node))
return ts.createStringLiteral(node.text);
return context.factory.createStringLiteral(node.text);
return node;
}
```
Expand Down
6 changes: 3 additions & 3 deletions docs/manipulation/transforms.md
Expand Up @@ -67,16 +67,16 @@ sourceFile.transform(traversal => {

const node = traversal.visitChildren();
if (ts.isFunctionDeclaration(node)) {
return ts.updateFunctionDeclaration(
return traversal.factory.updateFunctionDeclaration(
node,
[],
[],
undefined,
ts.createIdentifier("newName"),
traversal.factory.createIdentifier("newName"),
[],
[],
undefined,
ts.createBlock([]),
traversal.factory.createBlock([]),
);
}
return node;
Expand Down
20 changes: 3 additions & 17 deletions docs/utilities.md
Expand Up @@ -46,24 +46,10 @@ But sometimes you might want to print a compiler node. There's a `printNode` uti
```ts
import { printNode, ts } from "ts-morph";

// Source: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API
const tsFunctionDeclaration = ts.createFunctionDeclaration(
/*decorators*/ undefined,
/*modifiers*/ [ts.createToken(SyntaxKind.ExportKeyword)],
/*asteriskToken*/ undefined,
"myFunction",
/*typeParameters*/ undefined,
/*parameters*/ [],
/*returnType*/ ts.createKeywordTypeNode(SyntaxKind.NumberKeyword),
ts.createBlock([ts.createReturn(ts.createLiteral(5))], /*multiline*/ true),
);
// get a compiler node from somewhere
const compilerNode: ts.Node = ...;
// optionally provide a source file and there is some printing options on this
const functionText = printNode(tsFunctionDeclaration);
const functionText = printNode(compilerNode);

console.log(functionText);
// outputs:
// ========
// export function myFunction(): number {
// return 5;
// }
```
2 changes: 1 addition & 1 deletion packages/bootstrap/LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019 David Sherret
Copyright (c) 2017-2022 David Sherret

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down

0 comments on commit 8a87a1b

Please sign in to comment.