Skip to content

Declaration file #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bin/*
node_modules/*
coverage/*
*.log
17 changes: 17 additions & 0 deletions lib/__tests__/__snapshots__/fileDeclaration.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`a file declaration matches the snapshot 1`] = `
// Filename: MyFile.d.ts


`;

exports[`a file declaration with imports and members 1`] = `
import {MyNamedImport} from 'MyNamedImport';

export const MY_CONST: MyNamedImport;

export const YOUR_CONST: "Literally";


`;
2 changes: 1 addition & 1 deletion lib/__tests__/exportDefault.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ContextFlags, create, emit} from "../index"
import {create, emit} from "../index"

describe("an export default assignment", () => {
it("matches the snapshot", () => {
Expand Down
20 changes: 20 additions & 0 deletions lib/__tests__/fileDeclaration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {create, DeclarationFlags, emit, type} from "../index"

describe("a file declaration", () => {
it("matches the snapshot", () => {
const file = create.file('MyFile.d.ts')

expect(emit(file)).toMatchSnapshot();
});

it("with imports and members", () => {
const importName = 'MyNamedImport'
const file = create.file()

file.imports.push(create.importNamed(importName, '', importName))
file.members.push(create.const('MY_CONST', create.interface(importName), DeclarationFlags.Export))
file.members.push(create.const('YOUR_CONST', type.stringLiteral('Literally'), DeclarationFlags.Export))

expect(emit(file)).toMatchSnapshot();
});
});
2 changes: 1 addition & 1 deletion lib/__tests__/tripleSlashDirective.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ContextFlags, TripleSlashDirective, create, emit} from "../index"
import {TripleSlashDirective, create, emit} from "../index"

describe("an emitted module with triple slash directives", () => {
it("matches the snapshot", () => {
Expand Down
44 changes: 39 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ export interface DeclarationBase {
flags?: DeclarationFlags;
}

export interface FileDeclaration extends DeclarationBase {
kind: "file"
name?: string
imports: Import[]
members: FileMemberDeclaration[]
}

export interface EnumMemberDeclaration extends DeclarationBase {
kind: "enum-value";
name: string;
Expand Down Expand Up @@ -251,11 +258,11 @@ export function isPrimitiveType(x: Type): x is PrimitiveType {
return true;
}
// StringLiteral
if (typeof x === 'string') {
if (typeof x === 'string' || x instanceof String) {
return true;
}
// NumberLiteral
if (typeof x === 'number') {
if (typeof x === 'number' || x instanceof Number) {
return true;
}
return false;
Expand All @@ -277,7 +284,8 @@ export type Import = ImportAllDeclaration | ImportDefaultDeclaration | ImportNam

export type NamespaceMember = InterfaceDeclaration | TypeAliasDeclaration | ClassDeclaration | NamespaceDeclaration | ConstDeclaration | VariableDeclaration | FunctionDeclaration | EnumDeclaration;
export type ModuleMember = InterfaceDeclaration | TypeAliasDeclaration | ClassDeclaration | NamespaceDeclaration | ConstDeclaration | VariableDeclaration | FunctionDeclaration | Import | ExportEqualsDeclaration | ExportDefaultDeclaration;
export type TopLevelDeclaration = NamespaceMember | ExportEqualsDeclaration | ExportDefaultDeclaration | ExportNameDeclaration | ModuleDeclaration | EnumDeclaration | Import;
export type TopLevelDeclaration = FileDeclaration | NamespaceMember | ExportEqualsDeclaration | ExportDefaultDeclaration | ExportNameDeclaration | ModuleDeclaration | EnumDeclaration | Import;
export type FileMemberDeclaration = NamespaceMember | ExportEqualsDeclaration | ExportDefaultDeclaration | ExportNameDeclaration | ModuleDeclaration | EnumDeclaration

export enum DeclarationFlags {
None = 0,
Expand All @@ -303,6 +311,15 @@ export const config = {
};

export const create = {
file (name?: string): FileDeclaration {
return {
kind: 'file',
name,
imports: [],
members: []
}
},

interface(name: string, flags = DeclarationFlags.None): InterfaceDeclaration {
return {
name,
Expand Down Expand Up @@ -841,7 +858,7 @@ export function emit(rootDecl: TopLevelDeclaration, { rootFlags = ContextFlags.N
for (const param of member.parameters) {
if (!first) print(', ');
first = false;
writeParameter(param);
writeParameter(param);
}
print('): ');
writeReference(member.returnType);
Expand All @@ -859,8 +876,9 @@ export function emit(rootDecl: TopLevelDeclaration, { rootFlags = ContextFlags.N
print(';');
newline();
return;
default:
never(member, `Unknown member kind ${(member as ObjectTypeMember).kind}`);
}
never(member, `Unknown member kind ${(member as ObjectTypeMember).kind}`);
}
}

Expand Down Expand Up @@ -1286,11 +1304,27 @@ export function emit(rootDecl: TopLevelDeclaration, { rootFlags = ContextFlags.N
newline();
}

function writeFile(f: FileDeclaration) {
if (typeof f.name === 'string') {
print('// Filename: ' + f.name)
newline()
}
for (const i of f.imports) {
writeDeclaration(i)
}
for (const m of f.members) {
newline()
writeDeclaration(m)
}
}

function writeDeclaration(d: TopLevelDeclaration) {
if (typeof d === 'string') {
return print(d);
} else {
switch (d.kind) {
case "file":
return writeFile(d)
case "interface":
return writeInterface(d);
case "function":
Expand Down
Loading