Skip to content

Commit 7680bae

Browse files
authoredOct 1, 2022
fix: ImportEqualsDeclaration should be exportable (#1336)
1 parent 3526c0a commit 7680bae

File tree

6 files changed

+43
-25
lines changed

6 files changed

+43
-25
lines changed
 

‎deno/ts_morph.d.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -3096,6 +3096,8 @@ export declare class Node<NodeType extends ts.Node = ts.Node> {
30963096
static readonly isInferKeyword: (node: Node | undefined) => node is Node<ts.Token<SyntaxKind.InferKeyword>>;
30973097
/** Gets if the node is a InterfaceDeclaration. */
30983098
static readonly isInterfaceDeclaration: (node: Node | undefined) => node is InterfaceDeclaration;
3099+
/** Gets if the node is a JSDoc. */
3100+
static readonly isJSDoc: (node: Node | undefined) => node is JSDoc;
30993101
/** Gets if the node is a JSDocAllType. */
31003102
static readonly isJSDocAllType: (node: Node | undefined) => node is JSDocAllType;
31013103
/** Gets if the node is a JSDocAugmentsTag. */
@@ -4151,11 +4153,6 @@ export declare class Node<NodeType extends ts.Node = ts.Node> {
41514153
* @param node - Node to check.
41524154
*/
41534155
static isIterationStatement(node: Node | undefined): node is IterationStatement;
4154-
/**
4155-
* Gets if the node is a JSDoc.
4156-
* @param node - Node to check.
4157-
*/
4158-
static isJSDoc(node: Node | undefined): node is JSDoc;
41594156
/**
41604157
* Gets if the node is a JSDocableNode.
41614158
* @param node - Node to check.
@@ -7353,7 +7350,7 @@ export declare class ImportDeclaration extends ImportDeclarationBase<ts.ImportDe
73537350
getParentOrThrow(): NonNullable<NodeParentType<ts.ImportDeclaration>>;
73547351
}
73557352

7356-
declare const ImportEqualsDeclarationBase: Constructor<JSDocableNode> & Constructor<NamedNode> & typeof Statement;
7353+
declare const ImportEqualsDeclarationBase: Constructor<ExportableNode> & Constructor<ModifierableNode> & Constructor<JSDocableNode> & Constructor<NamedNode> & typeof Statement;
73577354

73587355
export declare class ImportEqualsDeclaration extends ImportEqualsDeclarationBase<ts.ImportEqualsDeclaration> {
73597356
/** Gets if this import equals declaration is type only. */

‎deno/ts_morph.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -4247,6 +4247,7 @@ class Node {
42474247
case SyntaxKind.ClassDeclaration:
42484248
case SyntaxKind.EnumDeclaration:
42494249
case SyntaxKind.FunctionDeclaration:
4250+
case SyntaxKind.ImportEqualsDeclaration:
42504251
case SyntaxKind.InterfaceDeclaration:
42514252
case SyntaxKind.ModuleDeclaration:
42524253
case SyntaxKind.TypeAliasDeclaration:
@@ -4261,6 +4262,7 @@ class Node {
42614262
case SyntaxKind.ClassDeclaration:
42624263
case SyntaxKind.EnumDeclaration:
42634264
case SyntaxKind.FunctionDeclaration:
4265+
case SyntaxKind.ImportEqualsDeclaration:
42644266
case SyntaxKind.InterfaceDeclaration:
42654267
case SyntaxKind.ModuleDeclaration:
42664268
case SyntaxKind.TypeAliasDeclaration:
@@ -4484,9 +4486,6 @@ class Node {
44844486
return false;
44854487
}
44864488
}
4487-
static isJSDoc(node) {
4488-
return (node === null || node === void 0 ? void 0 : node.getKind()) === SyntaxKind.JSDoc;
4489-
}
44904489
static isJSDocable(node) {
44914490
switch (node === null || node === void 0 ? void 0 : node.getKind()) {
44924491
case SyntaxKind.ArrowFunction:
@@ -4734,6 +4733,7 @@ class Node {
47344733
case SyntaxKind.FunctionDeclaration:
47354734
case SyntaxKind.FunctionExpression:
47364735
case SyntaxKind.GetAccessor:
4736+
case SyntaxKind.ImportEqualsDeclaration:
47374737
case SyntaxKind.IndexSignature:
47384738
case SyntaxKind.InterfaceDeclaration:
47394739
case SyntaxKind.MethodDeclaration:
@@ -5523,6 +5523,7 @@ Node.isImportSpecifier = Node.is(SyntaxKind.ImportSpecifier);
55235523
Node.isImportTypeAssertionContainer = Node.is(SyntaxKind.ImportTypeAssertionContainer);
55245524
Node.isInferKeyword = Node.is(SyntaxKind.InferKeyword);
55255525
Node.isInterfaceDeclaration = Node.is(SyntaxKind.InterfaceDeclaration);
5526+
Node.isJSDoc = Node.is(SyntaxKind.JSDoc);
55265527
Node.isJSDocAllType = Node.is(SyntaxKind.JSDocAllType);
55275528
Node.isJSDocAugmentsTag = Node.is(SyntaxKind.JSDocAugmentsTag);
55285529
Node.isJSDocAuthorTag = Node.is(SyntaxKind.JSDocAuthorTag);
@@ -12615,7 +12616,7 @@ function getErrorWhenNamespaceImportsExist() {
1261512616
return new errors.InvalidOperationError("Cannot add a named import to an import declaration that has a namespace import.");
1261612617
}
1261712618

12618-
const createBase$r = (ctor) => JSDocableNode(NamedNode(ctor));
12619+
const createBase$r = (ctor) => ExportableNode(ModifierableNode(JSDocableNode(NamedNode(ctor))));
1261912620
const ImportEqualsDeclarationBase = createBase$r(Statement);
1262012621
class ImportEqualsDeclaration extends ImportEqualsDeclarationBase {
1262112622
isTypeOnly() {

‎packages/ts-morph/lib/ts-morph.d.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -3096,6 +3096,8 @@ export declare class Node<NodeType extends ts.Node = ts.Node> {
30963096
static readonly isInferKeyword: (node: Node | undefined) => node is Node<ts.Token<SyntaxKind.InferKeyword>>;
30973097
/** Gets if the node is a InterfaceDeclaration. */
30983098
static readonly isInterfaceDeclaration: (node: Node | undefined) => node is InterfaceDeclaration;
3099+
/** Gets if the node is a JSDoc. */
3100+
static readonly isJSDoc: (node: Node | undefined) => node is JSDoc;
30993101
/** Gets if the node is a JSDocAllType. */
31003102
static readonly isJSDocAllType: (node: Node | undefined) => node is JSDocAllType;
31013103
/** Gets if the node is a JSDocAugmentsTag. */
@@ -4151,11 +4153,6 @@ export declare class Node<NodeType extends ts.Node = ts.Node> {
41514153
* @param node - Node to check.
41524154
*/
41534155
static isIterationStatement(node: Node | undefined): node is IterationStatement;
4154-
/**
4155-
* Gets if the node is a JSDoc.
4156-
* @param node - Node to check.
4157-
*/
4158-
static isJSDoc(node: Node | undefined): node is JSDoc;
41594156
/**
41604157
* Gets if the node is a JSDocableNode.
41614158
* @param node - Node to check.
@@ -7353,7 +7350,7 @@ export declare class ImportDeclaration extends ImportDeclarationBase<ts.ImportDe
73537350
getParentOrThrow(): NonNullable<NodeParentType<ts.ImportDeclaration>>;
73547351
}
73557352

7356-
declare const ImportEqualsDeclarationBase: Constructor<JSDocableNode> & Constructor<NamedNode> & typeof Statement;
7353+
declare const ImportEqualsDeclarationBase: Constructor<ExportableNode> & Constructor<ModifierableNode> & Constructor<JSDocableNode> & Constructor<NamedNode> & typeof Statement;
73577354

73587355
export declare class ImportEqualsDeclaration extends ImportEqualsDeclarationBase<ts.ImportEqualsDeclaration> {
73597356
/** Gets if this import equals declaration is type only. */

‎packages/ts-morph/src/compiler/ast/common/Node.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -2591,6 +2591,7 @@ export class Node<NodeType extends ts.Node = ts.Node> {
25912591
case SyntaxKind.ClassDeclaration:
25922592
case SyntaxKind.EnumDeclaration:
25932593
case SyntaxKind.FunctionDeclaration:
2594+
case SyntaxKind.ImportEqualsDeclaration:
25942595
case SyntaxKind.InterfaceDeclaration:
25952596
case SyntaxKind.ModuleDeclaration:
25962597
case SyntaxKind.TypeAliasDeclaration:
@@ -2615,6 +2616,7 @@ export class Node<NodeType extends ts.Node = ts.Node> {
26152616
case SyntaxKind.ClassDeclaration:
26162617
case SyntaxKind.EnumDeclaration:
26172618
case SyntaxKind.FunctionDeclaration:
2619+
case SyntaxKind.ImportEqualsDeclaration:
26182620
case SyntaxKind.InterfaceDeclaration:
26192621
case SyntaxKind.ModuleDeclaration:
26202622
case SyntaxKind.TypeAliasDeclaration:
@@ -2987,13 +2989,8 @@ export class Node<NodeType extends ts.Node = ts.Node> {
29872989
}
29882990
}
29892991

2990-
/**
2991-
* Gets if the node is a JSDoc.
2992-
* @param node - Node to check.
2993-
*/
2994-
static isJSDoc(node: compiler.Node | undefined): node is compiler.JSDoc {
2995-
return node?.getKind() === SyntaxKind.JSDoc;
2996-
}
2992+
/** Gets if the node is a JSDoc. */
2993+
static readonly isJSDoc: (node: compiler.Node | undefined) => node is compiler.JSDoc = Node.is(SyntaxKind.JSDoc);
29972994

29982995
/**
29992996
* Gets if the node is a JSDocableNode.
@@ -3440,6 +3437,7 @@ export class Node<NodeType extends ts.Node = ts.Node> {
34403437
case SyntaxKind.FunctionDeclaration:
34413438
case SyntaxKind.FunctionExpression:
34423439
case SyntaxKind.GetAccessor:
3440+
case SyntaxKind.ImportEqualsDeclaration:
34433441
case SyntaxKind.IndexSignature:
34443442
case SyntaxKind.InterfaceDeclaration:
34453443
case SyntaxKind.MethodDeclaration:

‎packages/ts-morph/src/compiler/ast/module/ImportEqualsDeclaration.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { errors, ts } from "@ts-morph/common";
22
import { insertIntoParentTextRange, removeChildren } from "../../../manipulation";
33
import { ModuleReference } from "../aliases";
4-
import { JSDocableNode, NamedNode } from "../base";
4+
import { ExportableNode, JSDocableNode, ModifierableNode, NamedNode } from "../base";
55
import { Node } from "../common";
66
import { Statement } from "../statement";
77
import { SourceFile } from "./SourceFile";
88

9-
const createBase = <T extends typeof Statement>(ctor: T) => JSDocableNode(NamedNode(ctor));
9+
const createBase = <T extends typeof Statement>(ctor: T) => ExportableNode(ModifierableNode(JSDocableNode(NamedNode(ctor))));
1010
export const ImportEqualsDeclarationBase = createBase(Statement);
1111
export class ImportEqualsDeclaration extends ImportEqualsDeclarationBase<ts.ImportEqualsDeclaration> {
1212
/** Gets if this import equals declaration is type only. */

‎packages/ts-morph/src/tests/compiler/ast/module/importEqualsDeclarationTests.ts

+25
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,29 @@ describe("ImportEqualsDeclaration", () => {
200200
.to.throw();
201201
});
202202
});
203+
204+
describe(nameof<ImportEqualsDeclaration>("hasExportKeyword"), () => {
205+
function doTest(text: string, expected: boolean) {
206+
const { firstChild } = getInfoFromText<ImportEqualsDeclaration>(text);
207+
expect(firstChild.hasExportKeyword()).to.equal(expected);
208+
}
209+
210+
it("should get if has export keyword", () => {
211+
doTest("import test = Namespace.Test;", false);
212+
doTest("export import test = Namespace.Test;", true);
213+
});
214+
});
215+
216+
describe(nameof<ImportEqualsDeclaration>("setIsExported"), () => {
217+
function doTest(text: string, value: boolean, expected: string) {
218+
const { firstChild, sourceFile } = getInfoFromText<ImportEqualsDeclaration>(text);
219+
firstChild.setIsExported(value);
220+
expect(sourceFile.getText()).to.equal(expected);
221+
}
222+
223+
it("should add and remove", () => {
224+
doTest("import test = Namespace.Test;", true, "export import test = Namespace.Test;");
225+
doTest("export import test = Namespace.Test;", false, "import test = Namespace.Test;");
226+
});
227+
});
203228
});

0 commit comments

Comments
 (0)
Please sign in to comment.