Comparing version 0.1.4 to 0.1.5
107
bin/index.js
"use strict"; | ||
(function (MemberFlags) { | ||
MemberFlags[MemberFlags["None"] = 0] = "None"; | ||
MemberFlags[MemberFlags["Private"] = 1] = "Private"; | ||
MemberFlags[MemberFlags["Protected"] = 2] = "Protected"; | ||
MemberFlags[MemberFlags["Static"] = 4] = "Static"; | ||
MemberFlags[MemberFlags["Optional"] = 8] = "Optional"; | ||
MemberFlags[MemberFlags["Export"] = 16] = "Export"; | ||
})(exports.MemberFlags || (exports.MemberFlags = {})); | ||
var MemberFlags = exports.MemberFlags; | ||
(function (DeclarationFlags) { | ||
DeclarationFlags[DeclarationFlags["None"] = 0] = "None"; | ||
DeclarationFlags[DeclarationFlags["Private"] = 1] = "Private"; | ||
DeclarationFlags[DeclarationFlags["Protected"] = 2] = "Protected"; | ||
DeclarationFlags[DeclarationFlags["Static"] = 4] = "Static"; | ||
DeclarationFlags[DeclarationFlags["Optional"] = 8] = "Optional"; | ||
DeclarationFlags[DeclarationFlags["Export"] = 16] = "Export"; | ||
})(exports.DeclarationFlags || (exports.DeclarationFlags = {})); | ||
var DeclarationFlags = exports.DeclarationFlags; | ||
(function (ParameterFlags) { | ||
@@ -34,3 +34,3 @@ ParameterFlags[ParameterFlags["None"] = 0] = "None"; | ||
property: function (name, type, flags) { | ||
if (flags === void 0) { flags = MemberFlags.None; } | ||
if (flags === void 0) { flags = DeclarationFlags.None; } | ||
return { | ||
@@ -42,3 +42,3 @@ kind: "property", | ||
method: function (name, parameters, returnType, flags) { | ||
if (flags === void 0) { flags = MemberFlags.None; } | ||
if (flags === void 0) { flags = DeclarationFlags.None; } | ||
return { | ||
@@ -63,3 +63,3 @@ kind: "method", | ||
constructor: function (parameters, flags) { | ||
if (flags === void 0) { flags = MemberFlags.None; } | ||
if (flags === void 0) { flags = DeclarationFlags.None; } | ||
return { | ||
@@ -99,2 +99,8 @@ kind: "constructor", | ||
}; | ||
}, | ||
exportEquals: function (target) { | ||
return { | ||
kind: 'export=', | ||
target: target | ||
}; | ||
} | ||
@@ -115,6 +121,29 @@ }; | ||
}; | ||
var reservedWords = 'instanceof typeof break do new var case else return void catch finally continue for switch while this with debugger function throw default if try delete in'.split(/ /g); | ||
function canEmitAsIdentifier(s) { | ||
return /^[$A-Z_][0-9A-Z_$]*$/i.test(s) && reservedWords.indexOf(s) < 0; | ||
exports.reservedWords = ['abstract', 'await', 'boolean', 'break', 'byte', 'case', | ||
'catch', 'char', 'class', 'const', 'continue', 'debugger', 'default', | ||
'delete', 'do', 'double', 'else', 'enum', 'export', 'extends', 'false', | ||
'final', 'finally', 'float', 'for', 'function', 'goto', 'if', 'implements', | ||
'import', 'in', 'instanceof', 'int', 'interface', 'let', 'long', 'native', | ||
'new', 'null', 'package', 'private', 'protected', 'public', 'return', 'short', | ||
'static', 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', | ||
'transient', 'true', 'try', 'typeof', 'var', 'void', 'volatile', 'while', 'with', 'yield']; | ||
/** IdentifierName can be written as unquoted property names, but may be reserved words. */ | ||
function isIdentifierName(s) { | ||
return /^[$A-Z_][0-9A-Z_$]*$/i.test(s); | ||
} | ||
exports.isIdentifierName = isIdentifierName; | ||
/** Identifiers are e.g. legal variable names. They may not be reserved words */ | ||
function isIdentifier(s) { | ||
return isIdentifierName(s) && exports.reservedWords.indexOf(s) < 0; | ||
} | ||
exports.isIdentifier = isIdentifier; | ||
function quoteIfNeeded(s) { | ||
if (isIdentifierName(s)) { | ||
return s; | ||
} | ||
else { | ||
// JSON.stringify handles escaping quotes for us. Handy! | ||
return JSON.stringify(s); | ||
} | ||
} | ||
function never(x, err) { | ||
@@ -146,6 +175,10 @@ throw new Error(err); | ||
} | ||
function startWithDeclare(s) { | ||
function startWithDeclareOrExport(s, flags) { | ||
if (getContextFlags() & 2 /* InAmbientNamespace */) { | ||
// Already in an all-export context | ||
start(s); | ||
} | ||
else if (flags & DeclarationFlags.Export) { | ||
start("export " + s); | ||
} | ||
else { | ||
@@ -200,3 +233,2 @@ start("declare " + s); | ||
print('}'); | ||
newline(); | ||
function printMember(member) { | ||
@@ -207,4 +239,4 @@ switch (member.kind) { | ||
tab(); | ||
print("" + member.name); | ||
if (member.flags & MemberFlags.Optional) | ||
print(quoteIfNeeded(member.name)); | ||
if (member.flags & DeclarationFlags.Optional) | ||
print('?'); | ||
@@ -230,4 +262,4 @@ print('('); | ||
tab(); | ||
print("" + member.name); | ||
if (member.flags & MemberFlags.Optional) | ||
print(quoteIfNeeded(member.name)); | ||
if (member.flags & DeclarationFlags.Optional) | ||
print('?'); | ||
@@ -290,7 +322,7 @@ print(': '); | ||
printDeclarationComments(f); | ||
if (!canEmitAsIdentifier(f.name)) { | ||
start("/* Unspeakable name '" + f.name + "'"); | ||
if (!isIdentifier(f.name)) { | ||
start("/* Illegal functoin name '" + f.name + "' can't be used here"); | ||
newline(); | ||
} | ||
startWithDeclare("function " + f.name + "("); | ||
startWithDeclareOrExport("function " + f.name + "(", f.flags); | ||
writeDelimited(f.parameters, ', ', writeParameter); | ||
@@ -301,3 +333,3 @@ print('): '); | ||
newline(); | ||
if (!canEmitAsIdentifier(f.name)) { | ||
if (!isIdentifier(f.name)) { | ||
start("*/"); | ||
@@ -324,3 +356,3 @@ newline(); | ||
printDeclarationComments(c); | ||
startWithDeclare("class " + c.name + " {"); | ||
startWithDeclareOrExport("class " + c.name + " {", c.flags); | ||
newline(); | ||
@@ -355,3 +387,3 @@ indentLevel++; | ||
printDeclarationComments(p); | ||
start(p.name + ": "); | ||
start(quoteIfNeeded(p.name) + ": "); | ||
writeReference(p.type); | ||
@@ -363,3 +395,3 @@ print(';'); | ||
printDeclarationComments(m); | ||
start(m.name + "("); | ||
start(quoteIfNeeded(m.name) + "("); | ||
writeDelimited(m.parameters, ', ', writeParameter); | ||
@@ -373,3 +405,3 @@ print('): '); | ||
printDeclarationComments(ns); | ||
startWithDeclare("namespace " + ns.name + " {"); | ||
startWithDeclareOrExport("namespace " + ns.name + " {", ns.flags); | ||
contextStack.push(2 /* InAmbientNamespace */); | ||
@@ -389,3 +421,3 @@ newline(); | ||
printDeclarationComments(c); | ||
startWithDeclare("const " + c.name + ": "); | ||
startWithDeclareOrExport("const " + c.name + ": ", c.flags); | ||
writeReference(c.type); | ||
@@ -395,5 +427,12 @@ print(';'); | ||
} | ||
function writeAlias(a) { | ||
throw new Error("NYI"); | ||
} | ||
function writeExportEquals(e) { | ||
start("export = " + e.target + ";"); | ||
newline(); | ||
} | ||
function writeDeclaration(d) { | ||
if (typeof d === 'string') { | ||
print(d); | ||
return print(d); | ||
} | ||
@@ -412,4 +451,8 @@ else { | ||
return writeConst(d); | ||
case "alias": | ||
return writeAlias(d); | ||
case "export=": | ||
return writeExportEquals(d); | ||
default: | ||
throw new Error("Unknown declaration kind " + d.kind); | ||
return never(d, "Unknown declaration kind " + d.kind); | ||
} | ||
@@ -416,0 +459,0 @@ } |
106
lib/index.ts
export interface DeclarationBase { | ||
jsDocComment?: string; | ||
comment?: string; | ||
flags?: DeclarationFlags; | ||
} | ||
@@ -10,3 +11,2 @@ | ||
type: Type; | ||
flags?: MemberFlags; | ||
} | ||
@@ -26,3 +26,2 @@ | ||
returnType: Type; | ||
flags?: MemberFlags; | ||
} | ||
@@ -40,3 +39,2 @@ | ||
parameters: Parameter[]; | ||
flags: MemberFlags; | ||
} | ||
@@ -60,3 +58,3 @@ | ||
name: string; | ||
members: TopLevelDeclaration[]; | ||
members: NamespaceMember[]; | ||
} | ||
@@ -77,3 +75,3 @@ | ||
kind: "namespace"; | ||
members: TopLevelDeclaration[]; | ||
members: NamespaceMember[]; | ||
} | ||
@@ -120,5 +118,7 @@ | ||
export type Type = TypeReference | UnionType | IntersectionType | PrimitiveType | ObjectType; | ||
export type TopLevelDeclaration = InterfaceDeclaration | TypeAliasDeclaration | ClassDeclaration | NamespaceDeclaration | ExportEqualsDeclaration | ConstDeclaration | FunctionDeclaration | NamespaceDeclaration; | ||
export enum MemberFlags { | ||
export type NamespaceMember = InterfaceDeclaration | TypeAliasDeclaration | ClassDeclaration | NamespaceDeclaration | ConstDeclaration | FunctionDeclaration; | ||
export type TopLevelDeclaration = NamespaceMember | ExportEqualsDeclaration; | ||
export enum DeclarationFlags { | ||
None = 0, | ||
@@ -156,3 +156,3 @@ Private = 1 << 0, | ||
property(name: string, type: Type, flags = MemberFlags.None): PropertyDeclaration { | ||
property(name: string, type: Type, flags = DeclarationFlags.None): PropertyDeclaration { | ||
return { | ||
@@ -164,3 +164,3 @@ kind: "property", | ||
method(name: string, parameters: Parameter[], returnType: Type, flags = MemberFlags.None): MethodDeclaration { | ||
method(name: string, parameters: Parameter[], returnType: Type, flags = DeclarationFlags.None): MethodDeclaration { | ||
return { | ||
@@ -186,3 +186,3 @@ kind: "method", | ||
constructor(parameters: Parameter[], flags = MemberFlags.None): ConstructorDeclaration { | ||
constructor(parameters: Parameter[], flags = DeclarationFlags.None): ConstructorDeclaration { | ||
return { | ||
@@ -227,2 +227,9 @@ kind: "constructor", | ||
}; | ||
}, | ||
exportEquals(target: string): ExportEqualsDeclaration { | ||
return { | ||
kind: 'export=', | ||
target | ||
}; | ||
} | ||
@@ -245,7 +252,30 @@ }; | ||
const reservedWords = 'instanceof typeof break do new var case else return void catch finally continue for switch while this with debugger function throw default if try delete in'.split(/ /g); | ||
function canEmitAsIdentifier(s: string) { | ||
return /^[$A-Z_][0-9A-Z_$]*$/i.test(s) && reservedWords.indexOf(s) < 0; | ||
export const reservedWords = ['abstract', 'await', 'boolean', 'break', 'byte', 'case', | ||
'catch', 'char', 'class', 'const', 'continue', 'debugger', 'default', | ||
'delete', 'do', 'double', 'else', 'enum', 'export', 'extends', 'false', | ||
'final', 'finally', 'float', 'for', 'function', 'goto', 'if', 'implements', | ||
'import', 'in', 'instanceof', 'int', 'interface', 'let', 'long', 'native', | ||
'new', 'null', 'package', 'private', 'protected', 'public', 'return', 'short', | ||
'static', 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', | ||
'transient', 'true', 'try', 'typeof', 'var', 'void', 'volatile', 'while', 'with', 'yield']; | ||
/** IdentifierName can be written as unquoted property names, but may be reserved words. */ | ||
export function isIdentifierName(s: string) { | ||
return /^[$A-Z_][0-9A-Z_$]*$/i.test(s); | ||
} | ||
/** Identifiers are e.g. legal variable names. They may not be reserved words */ | ||
export function isIdentifier(s: string) { | ||
return isIdentifierName(s) && reservedWords.indexOf(s) < 0; | ||
} | ||
function quoteIfNeeded(s: string) { | ||
if (isIdentifierName(s)) { | ||
return s; | ||
} else { | ||
// JSON.stringify handles escaping quotes for us. Handy! | ||
return JSON.stringify(s); | ||
} | ||
} | ||
export const enum ContextFlags { | ||
@@ -288,5 +318,8 @@ None = 0, | ||
function startWithDeclare(s: string) { | ||
function startWithDeclareOrExport(s: string, flags: DeclarationFlags | undefined) { | ||
if (getContextFlags() & ContextFlags.InAmbientNamespace) { | ||
// Already in an all-export context | ||
start(s); | ||
} else if (flags & DeclarationFlags.Export) { | ||
start(`export ${s}`); | ||
} else { | ||
@@ -343,3 +376,2 @@ start(`declare ${s}`); | ||
print('}'); | ||
newline(); | ||
@@ -351,4 +383,4 @@ function printMember(member: ObjectTypeMember) { | ||
tab(); | ||
print(`${member.name}`); | ||
if (member.flags & MemberFlags.Optional) print('?'); | ||
print(quoteIfNeeded(member.name)); | ||
if (member.flags & DeclarationFlags.Optional) print('?'); | ||
print('('); | ||
@@ -371,4 +403,4 @@ let first = true; | ||
tab(); | ||
print(`${member.name}`); | ||
if (member.flags & MemberFlags.Optional) print('?'); | ||
print(quoteIfNeeded(member.name)); | ||
if (member.flags & DeclarationFlags.Optional) print('?'); | ||
print(': '); | ||
@@ -433,8 +465,8 @@ writeReference(member.type); | ||
printDeclarationComments(f); | ||
if (!canEmitAsIdentifier(f.name)) { | ||
start(`/* Unspeakable name '${f.name}'`); | ||
if (!isIdentifier(f.name)) { | ||
start(`/* Illegal functoin name '${f.name}' can't be used here`); | ||
newline(); | ||
} | ||
startWithDeclare(`function ${f.name}(`); | ||
startWithDeclareOrExport(`function ${f.name}(`, f.flags); | ||
@@ -447,3 +479,3 @@ writeDelimited(f.parameters, ', ', writeParameter); | ||
if (!canEmitAsIdentifier(f.name)) { | ||
if (!isIdentifier(f.name)) { | ||
start(`*/`); | ||
@@ -472,3 +504,3 @@ newline(); | ||
printDeclarationComments(c); | ||
startWithDeclare(`class ${c.name} {`); | ||
startWithDeclareOrExport(`class ${c.name} {`, c.flags); | ||
newline(); | ||
@@ -505,3 +537,3 @@ indentLevel++; | ||
printDeclarationComments(p); | ||
start(`${p.name}: `); | ||
start(`${quoteIfNeeded(p.name)}: `); | ||
writeReference(p.type); | ||
@@ -514,3 +546,3 @@ print(';'); | ||
printDeclarationComments(m); | ||
start(`${m.name}(`); | ||
start(`${quoteIfNeeded(m.name)}(`); | ||
writeDelimited(m.parameters, ', ', writeParameter); | ||
@@ -525,3 +557,3 @@ print('): '); | ||
printDeclarationComments(ns); | ||
startWithDeclare(`namespace ${ns.name} {`); | ||
startWithDeclareOrExport(`namespace ${ns.name} {`, ns.flags); | ||
contextStack.push(ContextFlags.InAmbientNamespace); | ||
@@ -541,3 +573,3 @@ newline(); | ||
printDeclarationComments(c); | ||
startWithDeclare(`const ${c.name}: `); | ||
startWithDeclareOrExport(`const ${c.name}: `, c.flags); | ||
writeReference(c.type); | ||
@@ -548,5 +580,14 @@ print(';'); | ||
function writeAlias(a: TypeAliasDeclaration) { | ||
throw new Error("NYI"); | ||
} | ||
function writeExportEquals(e: ExportEqualsDeclaration) { | ||
start(`export = ${e.target};`); | ||
newline(); | ||
} | ||
function writeDeclaration(d: TopLevelDeclaration) { | ||
if (typeof d === 'string') { | ||
print(d); | ||
return print(d); | ||
} else { | ||
@@ -564,9 +605,12 @@ switch (d.kind) { | ||
return writeConst(d); | ||
case "alias": | ||
return writeAlias(d); | ||
case "export=": | ||
return writeExportEquals(d); | ||
default: | ||
throw new Error(`Unknown declaration kind ${d.kind}`); | ||
return never(d, `Unknown declaration kind ${(d as TopLevelDeclaration).kind}`); | ||
} | ||
} | ||
} | ||
} |
{ | ||
"name": "dts-dom", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"homepage": "https://github.com/RyanCavanaugh/dts-dom", | ||
@@ -17,2 +17,2 @@ "description": "DOM for TypeScript Declaration Files", | ||
} | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
[![npm version](https://badge.fury.io/js/dts-dom.svg)](https://badge.fury.io/js/dts-dom) | ||
`dts-dom` is a library for programatically generating TypeScript declaration files. | ||
@@ -40,2 +42,2 @@ It is based mostly on the same [CodeDOM provided for C# and other .NET languages](https://msdn.microsoft.com/en-us/library/y2k85ax6(v=vs.110).aspx). | ||
} | ||
``` | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43715
1006
43
1