Skip to content

Commit

Permalink
reactor: #574 - Target ES2015.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The library now targets ES2015.
  • Loading branch information
dsherret committed Mar 22, 2019
1 parent 752aaf1 commit 1e56a01
Show file tree
Hide file tree
Showing 44 changed files with 71 additions and 389 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "6"
- "7"
before_script:
- yarn run lint
- yarn run ensure-no-project-compile-errors
Expand Down
2 changes: 1 addition & 1 deletion docs/details/exports.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const project = new Project();
project.addExistingSourceFiles("**/*.ts");
const mainFile = project.getSourceFileOrThrow("main.ts");

for (const [name, declarations] of mainFile.getExportedDeclarations().entries()) {
for (const [name, declarations] of mainFile.getExportedDeclarations()) {
console.log(`${name}: ${declarations.map(d => d.getText()).join(", ")}`)
}
```
Expand Down
8 changes: 0 additions & 8 deletions lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,14 +689,6 @@ export interface CompilerOptionsFromTsConfigResult {
* @param options - Options.
*/
export declare function getCompilerOptionsFromTsConfig(filePath: string, options?: CompilerOptionsFromTsConfigOptions): CompilerOptionsFromTsConfigResult;
export interface ReadonlyMap<K, V> {
readonly size: number;
get(key: K): V | undefined;
has(key: K): boolean;
entries(): IterableIterator<[K, V]>;
keys(): IterableIterator<K>;
values(): IterableIterator<V>;
}

/**
* Type guards for checking the type of a node.
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
"globby": "^8.0.1",
"is-negated-glob": "^1.0.0",
"multimatch": "^2.1.0",
"tslib": "^1.9.0",
"typescript": "^3.0.1"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions scripts/inspectors/TsMorphInspector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Project, Node, ClassDeclaration, InterfaceDeclaration, PropertyAccessExpression, PropertyAssignment, ComputedPropertyName,
Directory, TypeGuards } from "ts-morph";
import { Memoize, ArrayUtils, createHashSet } from "../../src/utils";
import { Memoize, ArrayUtils } from "../../src/utils";
import { isNodeClass } from "../common";
import { WrappedNode, Mixin, Structure, KindToWrapperMapping } from "./tsMorph";
import { WrapperFactory } from "./WrapperFactory";
Expand Down Expand Up @@ -47,7 +47,7 @@ export class TsMorphInspector {
}
}

return ArrayUtils.from(nodes.values());
return Array.from(nodes.values());

function getDependencyNode(node: WrappedNode) {
if (nodes.has(node))
Expand All @@ -65,12 +65,12 @@ export class TsMorphInspector {

@Memoize
getMixins(): Mixin[] {
const mixins = createHashSet<Mixin>();
const mixins = new Set<Mixin>();
for (const wrappedNode of this.getWrappedNodes()) {
for (const mixin of wrappedNode.getMixins())
mixins.add(mixin);
}
return ArrayUtils.from(mixins.values());
return Array.from(mixins.values());
}

@Memoize
Expand Down
2 changes: 1 addition & 1 deletion scripts/inspectors/tsMorph/Structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Structure {

@Memoize
getDescendantBaseStructures() {
return ArrayUtils.from(getDescendantBaseStructures(this));
return Array.from(getDescendantBaseStructures(this));

function* getDescendantBaseStructures(structure: Structure): IterableIterator<Structure> {
const baseStructures = structure.getBaseStructures();
Expand Down
1 change: 0 additions & 1 deletion scripts/refactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import { InspectorFactory } from "./inspectors";
import { SyntaxKind, TypeGuards } from "ts-morph";
import { ArrayUtils } from "../src/utils";

const factory = new InspectorFactory();
const tsaInspector = factory.getTsMorphInspector();
Expand Down
3 changes: 1 addition & 2 deletions scripts/verification/ensureMixinNotAppliedMultipleTimes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
* -----------------------------------------------------------
*/
import { TsMorphInspector, WrappedNode, Mixin } from "../inspectors";
import { createHashSet } from "../../src/utils";
import { Problem } from "./Problem";

export function ensureMixinNotAppliedMultipleTimes(inspector: TsMorphInspector, addProblem: (problem: Problem) => void) {
for (const node of inspector.getWrappedNodes())
findDuplicateMixins(node);

function findDuplicateMixins(node: WrappedNode) {
const foundMixins = createHashSet<string>();
const foundMixins = new Set<string>();
node.getMixins().forEach(inspectMixin);

function inspectMixin(mixin: Mixin) {
Expand Down
16 changes: 8 additions & 8 deletions src/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CompilerOptionsContainer, ManipulationSettings, ManipulationSettingsCon
import { SourceFileStructure } from "./structures";
import { WriterFunction } from "./types";
import { ts, CompilerOptions } from "./typescript";
import { ArrayUtils, FileUtils, matchGlobs, TsConfigResolver, createHashSet } from "./utils";
import { ArrayUtils, FileUtils, matchGlobs, TsConfigResolver } from "./utils";

export interface ProjectOptions {
/** Compiler options */
Expand Down Expand Up @@ -115,7 +115,7 @@ export class Project {
* not specifying to not add the source files.
*/
resolveSourceFileDependencies() {
const sourceFiles = createHashSet<SourceFile>();
const sourceFiles = new Set<SourceFile>();
const onSourceFileAdded = (sourceFile: SourceFile) => sourceFiles.add(sourceFile);
const { compilerFactory, inProjectCoordinator } = this._context;

Expand All @@ -133,7 +133,7 @@ export class Project {
for (const sourceFile of result.unchangedSourceFiles)
sourceFiles.delete(sourceFile);

return ArrayUtils.from(sourceFiles.values());
return Array.from(sourceFiles.values());
}

/**
Expand Down Expand Up @@ -197,7 +197,7 @@ export class Project {
* Gets all the directories.
*/
getDirectories() {
return ArrayUtils.from(this._getProjectDirectoriesByDirectoryDepth());
return Array.from(this._getProjectDirectoriesByDirectoryDepth());
}

/**
Expand Down Expand Up @@ -371,12 +371,12 @@ export class Project {
const sourceFiles = this._getProjectSourceFilesByDirectoryDepth();

if (typeof globPatterns === "string" || globPatterns instanceof Array)
return ArrayUtils.from(getFilteredSourceFiles());
return Array.from(getFilteredSourceFiles());
else
return ArrayUtils.from(sourceFiles);
return Array.from(sourceFiles);

function* getFilteredSourceFiles() {
const sourceFilePaths = ArrayUtils.from(getSourceFilePaths());
const sourceFilePaths = Array.from(getSourceFilePaths());
const matchedPaths = matchGlobs(sourceFilePaths, globPatterns!, fileSystemWrapper.getCurrentDirectory());

for (const matchedPath of matchedPaths)
Expand Down Expand Up @@ -463,7 +463,7 @@ export class Project {
}

private _getUnsavedSourceFiles() {
return ArrayUtils.from(getUnsavedIterator(this._context.compilerFactory.getSourceFilesByDirectoryDepth()));
return Array.from(getUnsavedIterator(this._context.compilerFactory.getSourceFilesByDirectoryDepth()));

function* getUnsavedIterator(sourceFiles: IterableIterator<SourceFile>) {
for (const sourceFile of sourceFiles) {
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/ast/base/ModuledNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FormattingKind, removeChildrenWithFormatting } from "../../../manipulat
import { ImportDeclarationStructure, ExportDeclarationStructure, ExportAssignmentStructure, OptionalKind } from "../../../structures";
import { Constructor } from "../../../types";
import { ts, SyntaxKind } from "../../../typescript";
import { ArrayUtils, TypeGuards, createMap, ReadonlyMap } from "../../../utils";
import { ArrayUtils, TypeGuards } from "../../../utils";
import { Symbol } from "../../symbols";
import { ExportedDeclarations } from "../aliases";
import { Node } from "../common";
Expand Down Expand Up @@ -336,12 +336,12 @@ export function ModuledNode<T extends Constructor<ModuledNodeExtensionType>>(Bas
}

getExportedDeclarations(): ReadonlyMap<string, ExportedDeclarations[]> {
const result = createMap<string, ExportedDeclarations[]>();
const result = new Map<string, ExportedDeclarations[]>();
const exportSymbols = this.getExportSymbols();

for (const symbol of exportSymbols) {
for (const declaration of symbol.getDeclarations()) {
const declarations = ArrayUtils.from(getDeclarationHandlingImportsAndExports(declaration));
const declarations = Array.from(getDeclarationHandlingImportsAndExports(declaration));
result.set(symbol.getName(), declarations as ExportedDeclarations[]);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/ast/common/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ export class Node<NodeType extends ts.Node = ts.Node> implements TextRange {
* Gets the node's descendants.
*/
getDescendants(): Node[] {
return ArrayUtils.from(this._getDescendantsIterator());
return Array.from(this._getDescendantsIterator());
}

/**
Expand Down Expand Up @@ -991,7 +991,7 @@ export class Node<NodeType extends ts.Node = ts.Node> implements TextRange {
*/
getAncestors(includeSyntaxLists: boolean): Node[];
getAncestors(includeSyntaxLists = false) {
return ArrayUtils.from(this._getAncestorsIterator(includeSyntaxLists));
return Array.from(this._getAncestorsIterator(includeSyntaxLists));
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/compiler/ast/module/SourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export class SourceFile extends SourceFileBase<ts.SourceFile> {

/** @internal */
_getReferencesForCopyInternal(): [StringLiteral, SourceFile][] {
return ArrayUtils.from(this._referenceContainer.getLiteralsReferencingOtherSourceFilesEntries());
return Array.from(this._referenceContainer.getLiteralsReferencingOtherSourceFilesEntries());
}

/** @internal */
Expand Down Expand Up @@ -335,8 +335,8 @@ export class SourceFile extends SourceFileBase<ts.SourceFile> {
/** @internal */
_getReferencesForMoveInternal(): SourceFileReferences {
return {
literalReferences: ArrayUtils.from(this._referenceContainer.getLiteralsReferencingOtherSourceFilesEntries()),
referencingLiterals: ArrayUtils.from(this._referenceContainer.getReferencingLiteralsInOtherSourceFiles())
literalReferences: Array.from(this._referenceContainer.getLiteralsReferencingOtherSourceFilesEntries()),
referencingLiterals: Array.from(this._referenceContainer.getReferencingLiteralsInOtherSourceFiles())
};
}

Expand Down Expand Up @@ -466,21 +466,21 @@ export class SourceFile extends SourceFileBase<ts.SourceFile> {
* Get any source files that reference this source file.
*/
getReferencingSourceFiles() {
return ArrayUtils.from(this._referenceContainer.getDependentSourceFiles());
return Array.from(this._referenceContainer.getDependentSourceFiles());
}

/**
* Gets the import and exports in other source files that reference this source file.
*/
getReferencingNodesInOtherSourceFiles() {
return ArrayUtils.from(this._referenceContainer.getReferencingNodesInOtherSourceFiles());
return Array.from(this._referenceContainer.getReferencingNodesInOtherSourceFiles());
}

/**
* Gets the string literals in other source files that reference this source file.
*/
getReferencingLiteralsInOtherSourceFiles() {
return ArrayUtils.from(this._referenceContainer.getReferencingLiteralsInOtherSourceFiles());
return Array.from(this._referenceContainer.getReferencingLiteralsInOtherSourceFiles());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/tools/LanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DefaultFileSystemHost } from "../../fileSystem";
import { ProjectContext } from "../../ProjectContext";
import { getTextFromFormattingEdits, replaceSourceFileTextForRename } from "../../manipulation";
import { CompilerOptions, EditorSettings, ScriptTarget, ts } from "../../typescript";
import { ArrayUtils, FileUtils, fillDefaultEditorSettings, fillDefaultFormatCodeSettings, KeyValueCache, ObjectUtils, StringUtils } from "../../utils";
import { FileUtils, fillDefaultEditorSettings, fillDefaultFormatCodeSettings, KeyValueCache, ObjectUtils, StringUtils } from "../../utils";
import { Node, TextRange } from "../ast/common";
import { SourceFile } from "../ast/module";
import { FormatCodeSettings, UserPreferences, RenameOptions } from "./inputs";
Expand Down Expand Up @@ -193,7 +193,7 @@ export class LanguageService {
*/
findReferencesAsNodes(node: Node) {
const referencedSymbols = this.findReferences(node);
return ArrayUtils.from(getReferencingNodes());
return Array.from(getReferencingNodes());

function* getReferencingNodes() {
for (const referencedSymbol of referencedSymbols) {
Expand Down
4 changes: 2 additions & 2 deletions src/factories/CompilerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SourceFileStructure } from "../structures";
import { WriterFunction } from "../types";
import { SyntaxKind, ts, TypeFlags } from "../typescript";
import { replaceSourceFileForCacheUpdate } from "../manipulation";
import { ArrayUtils, EventContainer, FileUtils, KeyValueCache, WeakCache, StringUtils, getTextFromStringOrWriter } from "../utils";
import { EventContainer, FileUtils, KeyValueCache, WeakCache, StringUtils, getTextFromStringOrWriter } from "../utils";
import { DirectoryCache } from "./DirectoryCache";
import { ForgetfulNodeCache } from "./ForgetfulNodeCache";
import { kindToWrapperMappings } from "./kindToWrapperMappings";
Expand Down Expand Up @@ -71,7 +71,7 @@ export class CompilerFactory {
* Gets the source file paths from the internal cache.
*/
getSourceFilePaths() {
return ArrayUtils.from(this.sourceFileCacheByFilePath.getKeys());
return Array.from(this.sourceFileCacheByFilePath.getKeys());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/factories/DirectoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { SourceFile } from "../compiler";
import { Directory } from "../fileSystem/Directory";
import { ProjectContext } from "../ProjectContext";
import { ArrayUtils, FileUtils, KeyValueCache, SortedKeyValueArray, LocaleStringComparer } from "../utils";
import { FileUtils, KeyValueCache, SortedKeyValueArray, LocaleStringComparer } from "../utils";

/**
* Cache for the directories.
Expand Down Expand Up @@ -44,7 +44,7 @@ export class DirectoryCache {
const dirLevels = new KeyValueCache<number, Directory[]>();
let depth = 0;
this.getOrphans().forEach(addToDirLevels);
depth = Math.min(...ArrayUtils.from(dirLevels.getKeys()));
depth = Math.min(...Array.from(dirLevels.getKeys()));

while (dirLevels.getSize() > 0) {
for (const dir of dirLevels.get(depth) || []) {
Expand Down
6 changes: 3 additions & 3 deletions src/factories/ForgetfulNodeCache.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Node } from "../compiler";
import * as errors from "../errors";
import { SyntaxKind, ts } from "../typescript";
import { createHashSet, HashSet, KeyValueCache } from "../utils";
import { KeyValueCache } from "../utils";

/**
* Extension of KeyValueCache that allows for "forget points."
*/
export class ForgetfulNodeCache extends KeyValueCache<ts.Node, Node> {
private readonly forgetStack: HashSet<Node>[] = [];
private readonly forgetStack: Set<Node>[] = [];

getOrCreate<TCreate extends Node>(key: ts.Node, createFunc: () => TCreate) {
return super.getOrCreate(key, () => {
Expand All @@ -19,7 +19,7 @@ export class ForgetfulNodeCache extends KeyValueCache<ts.Node, Node> {
}

setForgetPoint() {
this.forgetStack.push(createHashSet());
this.forgetStack.push(new Set());
}

forgetLastPoint() {
Expand Down
6 changes: 3 additions & 3 deletions src/factories/InProjectCoordinator.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { SourceFile } from "../compiler";
import { Directory } from "../fileSystem";
import { CompilerFactory } from "./CompilerFactory";
import { ArrayUtils, FileUtils, createHashSet } from "../utils";
import { FileUtils } from "../utils";

/**
* Holds information about whether source files or directories are in the project.
*
* todo: Move this to a different folder.
*/
export class InProjectCoordinator {
private readonly notInProjectFiles = createHashSet<SourceFile>();
private readonly notInProjectFiles = new Set<SourceFile>();

constructor(private readonly compilerFactory: CompilerFactory) {
compilerFactory.onSourceFileRemoved(sourceFile => {
Expand Down Expand Up @@ -116,7 +116,7 @@ export class InProjectCoordinator {
markAncestorDirs(directory);

function markAncestorDirs(dir: Directory) {
const ancestorDirs = ArrayUtils.from(getAncestorsUpToOneInProject(dir));
const ancestorDirs = Array.from(getAncestorsUpToOneInProject(dir));

// only mark the ancestor directories as being in the project if the top one is in the project
const topAncestor = ancestorDirs[ancestorDirs.length - 1];
Expand Down
4 changes: 2 additions & 2 deletions src/fileSystem/Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class Directory {
* Gets the source files in the current directory and all the descendant directories.
*/
getDescendantSourceFiles() {
return ArrayUtils.from(this._getDescendantSourceFilesIterator());
return Array.from(this._getDescendantSourceFilesIterator());
}

/**
Expand All @@ -221,7 +221,7 @@ export class Directory {
* Gets the descendant directories.
*/
getDescendantDirectories() {
return ArrayUtils.from(this._getDescendantDirectoriesIterator());
return Array.from(this._getDescendantDirectoriesIterator());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/fileSystem/FileSystemWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class Directory {
}

getAncestors() {
return ArrayUtils.from(this.getAncestorsIterator());
return Array.from(this.getAncestorsIterator());
}

*getAncestorsIterator() {
Expand Down

0 comments on commit 1e56a01

Please sign in to comment.