Skip to content

Commit 603a5ff

Browse files
christopherthielenblakeembrey
authored andcommitted
Use npm @types/* for 3rd party type definitions (#310)
1 parent 178f68c commit 603a5ff

File tree

22 files changed

+170
-19389
lines changed

22 files changed

+170
-19389
lines changed

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
"node": ">=0.10.0"
3131
},
3232
"dependencies": {
33+
"@types/fs-extra": "0.0.33",
34+
"@types/handlebars": "^4.0.31",
35+
"@types/highlight.js": "^9.1.8",
36+
"@types/lodash": "^4.14.37",
37+
"@types/marked": "0.0.28",
38+
"@types/minimatch": "^2.0.29",
39+
"@types/shelljs": "^0.3.32",
3340
"fs-extra": "^0.30.0",
3441
"handlebars": "4.0.5",
3542
"highlight.js": "^9.0.0",

src/lib/converter/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export class Context
230230
return;
231231
}
232232

233-
var isDeclaration = ts.isDeclarationFile(node);
233+
var isDeclaration = node.isDeclarationFile;
234234
if (isDeclaration) {
235235
var lib = this.converter.getDefaultLib();
236236
var isLib = node.fileName.substr(-lib.length) == lib;

src/lib/converter/converter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {Context} from "./context";
88
import {ConverterComponent, ConverterNodeComponent, ConverterTypeComponent, ITypeTypeConverter, ITypeNodeConverter} from "./components";
99
import {CompilerHost} from "./utils/compiler-host";
1010
import {Component, Option, ChildableComponent, IComponentClass} from "../utils/component"
11+
import {normalizePath} from "../utils/fs";
1112

1213

1314
/**
@@ -330,7 +331,7 @@ export class Converter extends ChildableComponent<Application, ConverterComponen
330331
*/
331332
convert(fileNames:string[]):IConverterResult {
332333
for (var i = 0, c = fileNames.length; i < c; i++) {
333-
fileNames[i] = ts.normalizePath(ts.normalizeSlashes(fileNames[i]));
334+
fileNames[i] = normalizePath(ts.normalizeSlashes(fileNames[i]));
334335
}
335336

336337
var program = ts.createProgram(fileNames, this.application.options.getCompilerOptions(), this.compilerHost);

src/lib/converter/plugins/GitHubPlugin.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ import {BasePath} from "../utils/base-path";
77
import {Converter} from "../converter";
88
import {Context} from "../context";
99

10+
// This should be removed when @typings/shelljs typings are updated to the shelljs version being used
11+
declare module "shelljs" {
12+
// `stdout` was added in:
13+
// https://github.com/shelljs/shelljs/commit/8a7f7ceec4d3a77a9309d935755675ac368b1eda#diff-c3bfabb5e6987aa21bc75ffd95a162d6
14+
// As of 2016-10-16, DefinitelyTyped's defs are for shelljs v0.3.0, but we're on 0.7.0
15+
interface ExecOutputReturnValue {
16+
stdout: string;
17+
stderr: string;
18+
}
19+
}
1020

1121
/**
1222
* Stores data of a repository.

src/lib/converter/ts-internal.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import * as ts from "typescript";
2+
3+
/**
4+
* Expose the internal TypeScript APIs that are used by TypeDoc
5+
*/
6+
declare module "typescript" {
7+
interface Symbol {
8+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/types.ts#L2166
9+
id?: number;
10+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/types.ts#L2168
11+
parent?: ts.Symbol;
12+
}
13+
14+
interface Node {
15+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/types.ts#L469
16+
symbol?: ts.Symbol;
17+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/types.ts#L472
18+
localSymbol?: ts.Symbol;
19+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/types.ts#L471
20+
nextContainer?: ts.Node;
21+
}
22+
23+
/**
24+
* These functions are in "core" and are marked as @internal:
25+
* https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/core.ts#L4-L5
26+
*/
27+
28+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/core.ts#L411
29+
// function hasProperty<T>(map: ts.MapLike<T>, key: string): boolean;
30+
31+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/core.ts#L655-L656
32+
export function createCompilerDiagnostic(message: ts.DiagnosticMessage, ...args: any[]): ts.Diagnostic;
33+
export function createCompilerDiagnostic(message: ts.DiagnosticMessage): ts.Diagnostic;
34+
35+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/core.ts#L701
36+
function compareValues<T>(a: T, b: T): number; // Actually returns a ts.Comparison which is internal
37+
38+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/core.ts#L790
39+
function normalizeSlashes(path: string): string;
40+
41+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/core.ts#L795
42+
function getRootLength(path: string): number;
43+
44+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/core.ts#L845
45+
// function normalizePath(path: string): string;
46+
47+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/core.ts#L852-L854
48+
function getDirectoryPath(path: ts.Path): ts.Path;
49+
function getDirectoryPath(path: string): string;
50+
function getDirectoryPath(path: string): any;
51+
52+
/**
53+
* These functions are in "utilities" and are marked as @internal:
54+
* https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/utilities.ts#L3-L4
55+
*/
56+
57+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/utilities.ts#L188
58+
function getSourceFileOfNode(node: ts.Node): ts.SourceFile;
59+
60+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/utilities.ts#L333
61+
function getTextOfNode(node: ts.Node, includeTrivia?: boolean): string;
62+
63+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/utilities.ts#L438
64+
function declarationNameToString(name: ts.DeclarationName);
65+
66+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/utilities.ts#L598
67+
function getJsDocComments(node: ts.Node, sourceFileOfNode: ts.SourceFile);
68+
69+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/utilities.ts#L1487
70+
function isBindingPattern(node: ts.Node): node is ts.BindingPattern;
71+
72+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/utilities.ts#L1696
73+
function getClassExtendsHeritageClauseElement(node: ts.ClassLikeDeclaration | ts.InterfaceDeclaration);
74+
75+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/utilities.ts#L1701
76+
function getClassImplementsHeritageClauseElements(node: ts.ClassLikeDeclaration);
77+
78+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/utilities.ts#L1706
79+
function getInterfaceBaseTypeNodes(node: ts.InterfaceDeclaration);
80+
81+
82+
/**
83+
* https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/types.ts#L2789-L2924
84+
* This is large enum of char codes.
85+
*
86+
* Faking the enum as a var (only certain codes are used by TypeDoc)
87+
*/
88+
var CharacterCodes: {
89+
[key: string]: number;
90+
doubleQuote: number;
91+
space: number;
92+
minus: number;
93+
at: number;
94+
};
95+
96+
/**
97+
* https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/types.ts#L2334
98+
* Duplicating the interface definition :(
99+
*/
100+
// interface IntrinsicType extends ts.Type {
101+
// intrinsicName: string;
102+
// }
103+
104+
const optionDeclarations: CommandLineOption[];
105+
106+
/**
107+
* Command line options
108+
*/
109+
interface CommandLineOption {
110+
name: string;
111+
type: string;
112+
shortName: string;
113+
description: DiagnosticsEnumValue;
114+
paramType: DiagnosticsEnumValue;
115+
}
116+
117+
const Diagnostics: {
118+
[key: string]: DiagnosticsEnumValue;
119+
FILE: DiagnosticsEnumValue;
120+
DIRECTORY: DiagnosticsEnumValue;
121+
};
122+
123+
interface DiagnosticsEnumValue {
124+
code: number;
125+
category: ts.DiagnosticCategory;
126+
key: string;
127+
message: string;
128+
}
129+
130+
}

src/lib/converter/types/intrinsic.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@ import {Type, IntrinsicType} from "../../models/index";
44
import {Component, ConverterTypeComponent, ITypeTypeConverter} from "../components";
55
import {Context} from "../context";
66

7+
// TypeScript has an @internal enum set for the intrinsic types:
8+
// https://github.com/Microsoft/TypeScript/blob/v2.0.5/src/compiler/types.ts#L2297-L2298
9+
// It is not included in the typescript typings, so the enum is cast as `any` to access the `Intrinsic` set.
10+
const IntrinsicTypeFlags = (ts.TypeFlags as any).Intrinsic;
11+
if (IntrinsicTypeFlags === undefined) {
12+
throw new Error("Internal TypeScript API missing: TypeFlags.Intrinsic");
13+
}
714

815
@Component({name:'type:intrinsic'})
9-
export class IntrinsicConverter extends ConverterTypeComponent implements ITypeTypeConverter<ts.IntrinsicType>
16+
export class IntrinsicConverter extends ConverterTypeComponent implements ITypeTypeConverter<ts.Type>
1017
{
1118
/**
1219
* Test whether this converter can handle the given TypeScript type.
1320
*/
14-
supportsType(context:Context, type:ts.IntrinsicType):boolean {
15-
return !!(type.flags & ts.TypeFlags.Intrinsic);
21+
supportsType(context:Context, type:ts.Type):boolean {
22+
return !!(type.flags & IntrinsicTypeFlags);
1623
}
1724

1825

@@ -28,7 +35,8 @@ export class IntrinsicConverter extends ConverterTypeComponent implements ITypeT
2835
* @param type The intrinsic type that should be converted.
2936
* @returns The type reflection representing the given intrinsic type.
3037
*/
31-
convertType(context:Context, type:ts.IntrinsicType):IntrinsicType {
32-
return new IntrinsicType(type.intrinsicName);
38+
convertType(context:Context, type:ts.Type):IntrinsicType {
39+
let intrinsicName = context.program.getTypeChecker().typeToString(type);
40+
return new IntrinsicType(intrinsicName);
3341
}
3442
}

src/lib/converter/utils/compiler-host.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as ts from "typescript";
22
import * as Path from "path";
33

44
import {ConverterComponent} from "../components";
5+
import {normalizePath} from "../../utils/fs";
56

67

78
/**
@@ -56,7 +57,7 @@ export class CompilerHost extends ConverterComponent implements ts.CompilerHost
5657
*/
5758
getDefaultLibFileName(options:ts.CompilerOptions):string {
5859
var lib = this.owner.getDefaultLib();
59-
var path = ts.getDirectoryPath(ts.normalizePath(require.resolve('typescript')));
60+
var path = ts.getDirectoryPath(normalizePath(require.resolve('typescript')));
6061
return Path.join(path, lib);
6162
}
6263

src/lib/utils/fs.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var existingDirectories:ts.MapLike<boolean> = {};
1515
* @returns The normalized path.
1616
*/
1717
export function normalizePath(path:string) {
18-
return ts.normalizePath(path);
18+
return path.replace(/\\/g, "/");
1919
}
2020

2121

@@ -26,7 +26,7 @@ export function normalizePath(path:string) {
2626
* @returns TRUE if the given directory exists, FALSE otherwise.
2727
*/
2828
export function directoryExists(directoryPath: string): boolean {
29-
if (ts.hasProperty(existingDirectories, directoryPath)) {
29+
if (existingDirectories.hasOwnProperty(directoryPath)) {
3030
return true;
3131
}
3232

@@ -65,7 +65,7 @@ export function ensureDirectoriesExist(directoryPath: string) {
6565
*/
6666
export function writeFile(fileName:string, data:string, writeByteOrderMark:boolean, onError?:(message:string) => void) {
6767
try {
68-
ensureDirectoriesExist(ts.getDirectoryPath(ts.normalizePath(fileName)));
68+
ensureDirectoriesExist(ts.getDirectoryPath(normalizePath(fileName)));
6969
ts.sys.writeFile(fileName, data, writeByteOrderMark);
7070
} catch (e) {
7171
if (onError) onError(e.message);

0 commit comments

Comments
 (0)