mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
Update ts_library_builder (#1920)
This commit is contained in:
parent
3cc90d9bcf
commit
5ae78eb1de
6 changed files with 45 additions and 7 deletions
|
@ -104,3 +104,10 @@ export type TextDecoder = textEncoding.TextDecoder;
|
|||
window.performance = new performanceUtil.Performance();
|
||||
|
||||
window.workerMain = workers.workerMain;
|
||||
|
||||
// below are interfaces that are available in TypeScript but
|
||||
// have different signatures
|
||||
export interface ImportMeta {
|
||||
url: string;
|
||||
main: boolean;
|
||||
}
|
||||
|
|
6
js/lib.web_assembly.d.ts
vendored
6
js/lib.web_assembly.d.ts
vendored
|
@ -170,10 +170,4 @@ declare namespace WebAssembly {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO Move ImportMeta intos its own lib.import_meta.d.ts file?
|
||||
interface ImportMeta {
|
||||
url: string;
|
||||
main: boolean;
|
||||
}
|
||||
|
||||
/* eslint-enable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any */
|
||||
|
|
|
@ -73,6 +73,21 @@ export function addTypeAlias(
|
|||
});
|
||||
}
|
||||
|
||||
/** Add a declaration of an interface to a node */
|
||||
export function addInterfaceDeclaration(
|
||||
node: StatementedNode,
|
||||
interfaceDeclaration: InterfaceDeclaration
|
||||
) {
|
||||
const interfaceStructure = interfaceDeclaration.getStructure();
|
||||
|
||||
return node.addInterface({
|
||||
name: interfaceStructure.name,
|
||||
properties: interfaceStructure.properties,
|
||||
docs: interfaceStructure.docs,
|
||||
hasDeclareKeyword: true
|
||||
});
|
||||
}
|
||||
|
||||
/** Add a declaration of a variable to a node */
|
||||
export function addVariableDeclaration(
|
||||
node: StatementedNode,
|
||||
|
|
|
@ -2,6 +2,7 @@ import { writeFileSync } from "fs";
|
|||
import { join } from "path";
|
||||
import * as prettier from "prettier";
|
||||
import {
|
||||
InterfaceDeclaration,
|
||||
ExpressionStatement,
|
||||
NamespaceDeclarationKind,
|
||||
Project,
|
||||
|
@ -11,6 +12,7 @@ import {
|
|||
TypeGuards
|
||||
} from "ts-morph";
|
||||
import {
|
||||
addInterfaceDeclaration,
|
||||
addInterfaceProperty,
|
||||
addSourceComment,
|
||||
addTypeAlias,
|
||||
|
@ -219,6 +221,7 @@ export function mergeGlobal({
|
|||
node: ExpressionStatement;
|
||||
}
|
||||
>();
|
||||
const globalInterfaces: InterfaceDeclaration[] = [];
|
||||
|
||||
// For every augmentation of the global variable in source file, we want
|
||||
// to extract the type and add it to the global variable map
|
||||
|
@ -243,6 +246,8 @@ export function mergeGlobal({
|
|||
}
|
||||
}
|
||||
}
|
||||
} else if (TypeGuards.isInterfaceDeclaration(node) && node.isExported()) {
|
||||
globalInterfaces.push(node);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -277,6 +282,11 @@ export function mergeGlobal({
|
|||
);
|
||||
}
|
||||
|
||||
// We need to copy over any interfaces
|
||||
for (const interfaceDeclaration of globalInterfaces) {
|
||||
addInterfaceDeclaration(targetSourceFile, interfaceDeclaration);
|
||||
}
|
||||
|
||||
// We need to ensure that we only namespace each source file once, so we
|
||||
// will use this map for tracking that.
|
||||
const sourceFileMap = new Map<SourceFile, string>();
|
||||
|
|
|
@ -162,7 +162,7 @@ function buildLibraryMerge() {
|
|||
assert(targetSourceFile.getNamespace("moduleF") != null);
|
||||
assert.equal(targetSourceFile.getNamespaces().length, 4);
|
||||
assert(targetSourceFile.getInterface("FooBar") != null);
|
||||
assert.equal(targetSourceFile.getInterfaces().length, 1);
|
||||
assert.equal(targetSourceFile.getInterfaces().length, 2);
|
||||
const variableDeclarations = targetSourceFile.getVariableDeclarations();
|
||||
assert.equal(variableDeclarations[0].getType().getText(), `FooBar`);
|
||||
assert.equal(variableDeclarations[1].getType().getText(), `FooBar`);
|
||||
|
@ -188,6 +188,14 @@ function buildLibraryMerge() {
|
|||
assert.equal(typeAliases[0].getName(), "Bar");
|
||||
assert.equal(typeAliases[0].getType().getText(), "moduleC.Bar");
|
||||
assert.equal(typeAliases.length, 1);
|
||||
const exportedInterface = targetSourceFile.getInterfaceOrThrow("FizzBuzz");
|
||||
const interfaceProperties = exportedInterface.getStructure().properties;
|
||||
assert(interfaceProperties != null);
|
||||
assert.equal(interfaceProperties!.length, 2);
|
||||
assert.equal(interfaceProperties![0].name, "foo");
|
||||
assert.equal(interfaceProperties![0].type, "string");
|
||||
assert.equal(interfaceProperties![1].name, "bar");
|
||||
assert.equal(interfaceProperties![1].type, "number");
|
||||
}
|
||||
|
||||
function testInlineFiles() {
|
||||
|
|
4
tools/ts_library_builder/testdata/globals.ts
vendored
4
tools/ts_library_builder/testdata/globals.ts
vendored
|
@ -9,3 +9,7 @@ foobarbaz.process = moduleE.process;
|
|||
foobarbaz.reprocess = moduleD.reprocess;
|
||||
foobarbaz.Bar = moduleC.Bar;
|
||||
export type Bar = moduleC.Bar;
|
||||
export interface FizzBuzz {
|
||||
foo: string;
|
||||
bar: number;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue