Skip to content

Commit ae81016

Browse files
committed
Fixed incorrect keywords for non-exported enums (both simple and constant)
Fixes #205
1 parent eab1f86 commit ae81016

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

src/generate-output.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function compareStatementText(a: StatementText, b: StatementText): number {
141141
}
142142

143143
function needAddDeclareKeyword(statement: ts.Statement, nodeText: string): boolean {
144-
// for some reason TypeScript allows to do not write `declare` keyword for ClassDeclaration, FunctionDeclaration and VariableDeclaration
144+
// for some reason TypeScript allows to not write `declare` keyword for ClassDeclaration, FunctionDeclaration and VariableDeclaration
145145
// if it already has `export` keyword - so we need to add it
146146
// to avoid TS1046: Top-level declarations in .d.ts files must start with either a 'declare' or 'export' modifier.
147147
if (ts.isClassDeclaration(statement) && (/^class\b/.test(nodeText) || /^abstract\b/.test(nodeText))) {
@@ -156,6 +156,10 @@ function needAddDeclareKeyword(statement: ts.Statement, nodeText: string): boole
156156
return true;
157157
}
158158

159+
if (ts.isEnumDeclaration(statement) && (/^(const)\b/.test(nodeText) || /^(enum)\b/.test(nodeText))) {
160+
return true;
161+
}
162+
159163
return false;
160164
}
161165

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// note it doesn't have `declare` keyword
2+
export const enum ConstEnum2 {}
3+
4+
// note it doesn't have `declare` keyword
5+
export enum Enum2 {}

tests/e2e/test-cases/disable-non-direct-exports/input.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ import {
77
Type,
88
variable,
99
} from './module';
10+
import { Enum2, ConstEnum2 } from './enums';
1011

1112
export interface ExportedInterface {
1213
class: Class;
1314
constEnum: ConstEnum;
15+
constEnum2: ConstEnum2;
1416
enum: Enum;
17+
enum2: Enum2;
1518
func: typeof func;
1619
interface: Interface;
1720
type: Type;

tests/e2e/test-cases/disable-non-direct-exports/output.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ declare function func(): void;
99
interface Interface {
1010
}
1111
declare type Type = string;
12+
declare const enum ConstEnum2 {
13+
}
14+
declare enum Enum2 {
15+
}
1216
export interface ExportedInterface {
1317
class: Class;
1418
constEnum: ConstEnum;
19+
constEnum2: ConstEnum2;
1520
enum: Enum;
21+
enum2: Enum2;
1622
func: typeof func;
1723
interface: Interface;
1824
type: Type;

0 commit comments

Comments
 (0)