Skip to content

Commit

Permalink
fix: ImportEqualsDeclaration should be exportable (#1336)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Oct 1, 2022
1 parent 3526c0a commit 7680bae
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 25 deletions.
9 changes: 3 additions & 6 deletions deno/ts_morph.d.ts
Expand Up @@ -3096,6 +3096,8 @@ export declare class Node<NodeType extends ts.Node = ts.Node> {
static readonly isInferKeyword: (node: Node | undefined) => node is Node<ts.Token<SyntaxKind.InferKeyword>>;
/** Gets if the node is a InterfaceDeclaration. */
static readonly isInterfaceDeclaration: (node: Node | undefined) => node is InterfaceDeclaration;
/** Gets if the node is a JSDoc. */
static readonly isJSDoc: (node: Node | undefined) => node is JSDoc;
/** Gets if the node is a JSDocAllType. */
static readonly isJSDocAllType: (node: Node | undefined) => node is JSDocAllType;
/** Gets if the node is a JSDocAugmentsTag. */
Expand Down Expand Up @@ -4151,11 +4153,6 @@ export declare class Node<NodeType extends ts.Node = ts.Node> {
* @param node - Node to check.
*/
static isIterationStatement(node: Node | undefined): node is IterationStatement;
/**
* Gets if the node is a JSDoc.
* @param node - Node to check.
*/
static isJSDoc(node: Node | undefined): node is JSDoc;
/**
* Gets if the node is a JSDocableNode.
* @param node - Node to check.
Expand Down Expand Up @@ -7353,7 +7350,7 @@ export declare class ImportDeclaration extends ImportDeclarationBase<ts.ImportDe
getParentOrThrow(): NonNullable<NodeParentType<ts.ImportDeclaration>>;
}

declare const ImportEqualsDeclarationBase: Constructor<JSDocableNode> & Constructor<NamedNode> & typeof Statement;
declare const ImportEqualsDeclarationBase: Constructor<ExportableNode> & Constructor<ModifierableNode> & Constructor<JSDocableNode> & Constructor<NamedNode> & typeof Statement;

export declare class ImportEqualsDeclaration extends ImportEqualsDeclarationBase<ts.ImportEqualsDeclaration> {
/** Gets if this import equals declaration is type only. */
Expand Down
9 changes: 5 additions & 4 deletions deno/ts_morph.js
Expand Up @@ -4247,6 +4247,7 @@ class Node {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ImportEqualsDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.TypeAliasDeclaration:
Expand All @@ -4261,6 +4262,7 @@ class Node {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ImportEqualsDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.TypeAliasDeclaration:
Expand Down Expand Up @@ -4484,9 +4486,6 @@ class Node {
return false;
}
}
static isJSDoc(node) {
return (node === null || node === void 0 ? void 0 : node.getKind()) === SyntaxKind.JSDoc;
}
static isJSDocable(node) {
switch (node === null || node === void 0 ? void 0 : node.getKind()) {
case SyntaxKind.ArrowFunction:
Expand Down Expand Up @@ -4734,6 +4733,7 @@ class Node {
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
case SyntaxKind.GetAccessor:
case SyntaxKind.ImportEqualsDeclaration:
case SyntaxKind.IndexSignature:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.MethodDeclaration:
Expand Down Expand Up @@ -5523,6 +5523,7 @@ Node.isImportSpecifier = Node.is(SyntaxKind.ImportSpecifier);
Node.isImportTypeAssertionContainer = Node.is(SyntaxKind.ImportTypeAssertionContainer);
Node.isInferKeyword = Node.is(SyntaxKind.InferKeyword);
Node.isInterfaceDeclaration = Node.is(SyntaxKind.InterfaceDeclaration);
Node.isJSDoc = Node.is(SyntaxKind.JSDoc);
Node.isJSDocAllType = Node.is(SyntaxKind.JSDocAllType);
Node.isJSDocAugmentsTag = Node.is(SyntaxKind.JSDocAugmentsTag);
Node.isJSDocAuthorTag = Node.is(SyntaxKind.JSDocAuthorTag);
Expand Down Expand Up @@ -12615,7 +12616,7 @@ function getErrorWhenNamespaceImportsExist() {
return new errors.InvalidOperationError("Cannot add a named import to an import declaration that has a namespace import.");
}

const createBase$r = (ctor) => JSDocableNode(NamedNode(ctor));
const createBase$r = (ctor) => ExportableNode(ModifierableNode(JSDocableNode(NamedNode(ctor))));
const ImportEqualsDeclarationBase = createBase$r(Statement);
class ImportEqualsDeclaration extends ImportEqualsDeclarationBase {
isTypeOnly() {
Expand Down
9 changes: 3 additions & 6 deletions packages/ts-morph/lib/ts-morph.d.ts
Expand Up @@ -3096,6 +3096,8 @@ export declare class Node<NodeType extends ts.Node = ts.Node> {
static readonly isInferKeyword: (node: Node | undefined) => node is Node<ts.Token<SyntaxKind.InferKeyword>>;
/** Gets if the node is a InterfaceDeclaration. */
static readonly isInterfaceDeclaration: (node: Node | undefined) => node is InterfaceDeclaration;
/** Gets if the node is a JSDoc. */
static readonly isJSDoc: (node: Node | undefined) => node is JSDoc;
/** Gets if the node is a JSDocAllType. */
static readonly isJSDocAllType: (node: Node | undefined) => node is JSDocAllType;
/** Gets if the node is a JSDocAugmentsTag. */
Expand Down Expand Up @@ -4151,11 +4153,6 @@ export declare class Node<NodeType extends ts.Node = ts.Node> {
* @param node - Node to check.
*/
static isIterationStatement(node: Node | undefined): node is IterationStatement;
/**
* Gets if the node is a JSDoc.
* @param node - Node to check.
*/
static isJSDoc(node: Node | undefined): node is JSDoc;
/**
* Gets if the node is a JSDocableNode.
* @param node - Node to check.
Expand Down Expand Up @@ -7353,7 +7350,7 @@ export declare class ImportDeclaration extends ImportDeclarationBase<ts.ImportDe
getParentOrThrow(): NonNullable<NodeParentType<ts.ImportDeclaration>>;
}

declare const ImportEqualsDeclarationBase: Constructor<JSDocableNode> & Constructor<NamedNode> & typeof Statement;
declare const ImportEqualsDeclarationBase: Constructor<ExportableNode> & Constructor<ModifierableNode> & Constructor<JSDocableNode> & Constructor<NamedNode> & typeof Statement;

export declare class ImportEqualsDeclaration extends ImportEqualsDeclarationBase<ts.ImportEqualsDeclaration> {
/** Gets if this import equals declaration is type only. */
Expand Down
12 changes: 5 additions & 7 deletions packages/ts-morph/src/compiler/ast/common/Node.ts
Expand Up @@ -2591,6 +2591,7 @@ export class Node<NodeType extends ts.Node = ts.Node> {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ImportEqualsDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.TypeAliasDeclaration:
Expand All @@ -2615,6 +2616,7 @@ export class Node<NodeType extends ts.Node = ts.Node> {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ImportEqualsDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.TypeAliasDeclaration:
Expand Down Expand Up @@ -2987,13 +2989,8 @@ export class Node<NodeType extends ts.Node = ts.Node> {
}
}

/**
* Gets if the node is a JSDoc.
* @param node - Node to check.
*/
static isJSDoc(node: compiler.Node | undefined): node is compiler.JSDoc {
return node?.getKind() === SyntaxKind.JSDoc;
}
/** Gets if the node is a JSDoc. */
static readonly isJSDoc: (node: compiler.Node | undefined) => node is compiler.JSDoc = Node.is(SyntaxKind.JSDoc);

/**
* Gets if the node is a JSDocableNode.
Expand Down Expand Up @@ -3440,6 +3437,7 @@ export class Node<NodeType extends ts.Node = ts.Node> {
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
case SyntaxKind.GetAccessor:
case SyntaxKind.ImportEqualsDeclaration:
case SyntaxKind.IndexSignature:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.MethodDeclaration:
Expand Down
@@ -1,12 +1,12 @@
import { errors, ts } from "@ts-morph/common";
import { insertIntoParentTextRange, removeChildren } from "../../../manipulation";
import { ModuleReference } from "../aliases";
import { JSDocableNode, NamedNode } from "../base";
import { ExportableNode, JSDocableNode, ModifierableNode, NamedNode } from "../base";
import { Node } from "../common";
import { Statement } from "../statement";
import { SourceFile } from "./SourceFile";

const createBase = <T extends typeof Statement>(ctor: T) => JSDocableNode(NamedNode(ctor));
const createBase = <T extends typeof Statement>(ctor: T) => ExportableNode(ModifierableNode(JSDocableNode(NamedNode(ctor))));
export const ImportEqualsDeclarationBase = createBase(Statement);
export class ImportEqualsDeclaration extends ImportEqualsDeclarationBase<ts.ImportEqualsDeclaration> {
/** Gets if this import equals declaration is type only. */
Expand Down
Expand Up @@ -200,4 +200,29 @@ describe("ImportEqualsDeclaration", () => {
.to.throw();
});
});

describe(nameof<ImportEqualsDeclaration>("hasExportKeyword"), () => {
function doTest(text: string, expected: boolean) {
const { firstChild } = getInfoFromText<ImportEqualsDeclaration>(text);
expect(firstChild.hasExportKeyword()).to.equal(expected);
}

it("should get if has export keyword", () => {
doTest("import test = Namespace.Test;", false);
doTest("export import test = Namespace.Test;", true);
});
});

describe(nameof<ImportEqualsDeclaration>("setIsExported"), () => {
function doTest(text: string, value: boolean, expected: string) {
const { firstChild, sourceFile } = getInfoFromText<ImportEqualsDeclaration>(text);
firstChild.setIsExported(value);
expect(sourceFile.getText()).to.equal(expected);
}

it("should add and remove", () => {
doTest("import test = Namespace.Test;", true, "export import test = Namespace.Test;");
doTest("export import test = Namespace.Test;", false, "import test = Namespace.Test;");
});
});
});

0 comments on commit 7680bae

Please sign in to comment.