Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

Commit

Permalink
Fix build errors and crashes
Browse files Browse the repository at this point in the history
* A type may not be named 'object'
* Some strictnullchecks errors
* Cannot subclass Error unless target is ES6
  • Loading branch information
Andy Hanson committed Feb 15, 2017
1 parent f74d9af commit faa66e6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
26 changes: 8 additions & 18 deletions lib/index.ts
Expand Up @@ -3,7 +3,7 @@ import * as _ from 'underscore';
import * as dom from 'dts-dom';
import { create, reservedWords } from 'dts-dom';

type object = { valueOf: 'oops '; prototype?: object; (): void;[s: string]: any; };
type ObjectLike = { valueOf: 'oops '; prototype?: object; (): void;[s: string]: any; };

const enum ValueTypes {
None = 0,
Expand Down Expand Up @@ -63,7 +63,7 @@ export function generateModuleDeclarationFile(nameHint: string, root: any) {
if (decls.length === 1 && decls[0].kind === 'namespace') {
// Hoist out all the declarations and export them
const members = (decls[0] as dom.NamespaceDeclaration).members;
for (const m of members) m.flags |= dom.DeclarationFlags.Export;
for (const m of members) m.flags = m.flags! | dom.DeclarationFlags.Export;
return members.map(m => dom.emit(m)).join('');
} else {
// Going to have to write an export=
Expand All @@ -78,7 +78,7 @@ export function generateIdentifierDeclarationFile(name: string, value: any): str
return result.map(d => dom.emit(d)).join('\r\n');
}

const walkStack = new Map<any, boolean>();
const walkStack = new Set<any>();

const reservedFunctionProperties = Object.getOwnPropertyNames(function () { });
function getKeysOfObject(obj: object) {
Expand Down Expand Up @@ -117,11 +117,7 @@ function isLegalIdentifier(s: string) {
return reservedWords.indexOf(s) < 0;
}

function isCallable(obj: {}) {
return typeof obj === 'function';
}

function isClasslike(obj: object): boolean {
function isClasslike(obj: ObjectLike): boolean {
return !!(obj.prototype && Object.getOwnPropertyNames(obj.prototype).length > 1);
}

Expand All @@ -136,7 +132,7 @@ function getTopLevelDeclarations(name: string, obj: any): dom.NamespaceMember[]

if (!isLegalIdentifier(name)) return [];

walkStack.set(obj);
walkStack.add(obj);
keyStack.push(name);
const res = getResult();
keyStack.pop();
Expand Down Expand Up @@ -199,7 +195,6 @@ function getTopLevelDeclarations(name: string, obj: any): dom.NamespaceMember[]

// If anything in here is classlike or functionlike, write it as a namespace.
// Otherwise, write as a 'const'
const types = getValueTypes(obj);
const keys = getKeysOfObject(obj);
let constituentTypes = ValueTypes.None;
for (const k of keys) {
Expand Down Expand Up @@ -254,7 +249,7 @@ function getTypeOfValue(value: any): dom.Type {
if (value === null) {
return dom.type.any;
} else {
walkStack.set(value);
walkStack.add(value);
const members = getPropertyDeclarationsOfObject(value);
walkStack.delete(value);
members.sort(declarationComparer);
Expand All @@ -268,7 +263,7 @@ function getTypeOfValue(value: any): dom.Type {
}

function getPropertyDeclarationsOfObject(obj: any): dom.ObjectTypeMember[] {
walkStack.set(obj);
walkStack.add(obj);
const keys = getKeysOfObject(obj);
const result = keys.map(getProperty);
walkStack.delete(obj);
Expand Down Expand Up @@ -313,7 +308,6 @@ function getClassInstanceMembers(ctor: any): dom.ClassMember[] {
return [];
}

const body = parseFunctionBody(ctor);
const members: dom.ClassMember[] = [];

function visit(node: ts.Node) {
Expand Down Expand Up @@ -395,7 +389,7 @@ function getParameterListAndReturnType(obj: Function, fn: ts.FunctionExpression)

}

function inferParameterType(fn: ts.FunctionExpression, param: ts.ParameterDeclaration): dom.Type {
function inferParameterType(_fn: ts.FunctionExpression, _param: ts.ParameterDeclaration): dom.Type {
// TODO: Inspect function body for clues
return dom.type.any;
}
Expand All @@ -412,7 +406,3 @@ function parseFunctionBody(fn: any): ts.FunctionExpression {
function isNativeFunction(fn: any) {
return fn.toString().indexOf('{ [native code] }') > 0;
}

function makeIdentifier(s: string) {
return s.replace(/-/g, '_');
}
16 changes: 12 additions & 4 deletions lib/run.ts
Expand Up @@ -5,6 +5,8 @@ import writeDefinitelyTypedPackage from './definitely-typed';
import * as yargs from 'yargs';
import * as fs from 'fs';
import * as path from 'path';
import { install } from 'source-map-support';
install();

const templatesDirectory = path.join(__dirname, "..", "..", "templates");

Expand All @@ -20,6 +22,8 @@ interface Options {
dt?: string | boolean;
stdout?: boolean;
overwrite?: boolean;

version?: boolean;
}

const args: Options = yargs
Expand All @@ -32,6 +36,7 @@ const args: Options = yargs
.alias('s', 'stdout')
.alias('o', 'overwrite')
.alias('t', 'template')
.alias('v', 'version')
.argv;

class ArgsError extends Error {
Expand All @@ -40,11 +45,13 @@ class ArgsError extends Error {
}
}

const opts: Options = yargs.argv;

let result: string | undefined;
let name: string = 'dts_gen_expr';
try {
if (args.version) {
console.log(require("../../package.json").version);
process.exit(0);
}

if (+!!args.dt + +!!args.file + +!!args.stdout > 1) {
throw new ArgsError('Cannot specify more than one output mode');
}
Expand All @@ -56,13 +63,14 @@ try {
if (typeof args.module === 'boolean') throw new ArgsError('Must specify a value for "-module"');
if (args.overwrite !== undefined && args.overwrite !== true) throw new ArgsError('-overwrite does not accept an argument');

let name: string;
if (args.module) {
if (args.name) throw new ArgsError('Cannot use -name with -module');
name = args.module;
(module as any).paths.unshift(process.cwd() + '/node_modules');
result = guess.generateModuleDeclarationFile(args.module, require(args.module));
} else if (args.expression) {
name = args.name || name;
name = args.name || 'dts_gen_expr';
result = guess.generateIdentifierDeclarationFile(name, eval(args.expression));
} else if (args.identifier) {
if (args.name) throw new ArgsError('Cannot use -name with -identifier');
Expand Down
7 changes: 5 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "dts-gen",
"version": "0.4.20",
"version": "0.5.0",
"description": "TypeScript Definition File Generator",
"main": "bin/lib/index.js",
"bin": {
Expand Down Expand Up @@ -42,5 +42,8 @@
"bugs": {
"url": "https://github.com/Microsoft/dts-gen/issues"
},
"homepage": "https://github.com/Microsoft/dts-gen#readme"
"homepage": "https://github.com/Microsoft/dts-gen#readme",
"engines": {
"node": ">=6.0.0"
}
}
4 changes: 2 additions & 2 deletions tests/test.ts
Expand Up @@ -15,8 +15,8 @@ class MyClass {
constructor(public arg: number) {

}
prototypeMethod(p: any) { }
static staticMethod(s: any) { }
prototypeMethod(_p: any) { }
static staticMethod(_s: any) { }
static staticNum = 32;
instanceStr = 'inst';
}
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Expand Up @@ -9,9 +9,11 @@
"dom"
],
"module": "commonjs",
"target": "es5",
"target": "es6",
"noImplicitAny": true,
"strictNullChecks": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"sourceMap": false,
"outDir": "./bin",
"newLine": "LF"
Expand Down

0 comments on commit faa66e6

Please sign in to comment.