@polymer/gen-typescript-declarations
Advanced tools
Comparing version 0.3.5 to 0.3.6
@@ -9,3 +9,6 @@ # Changelog | ||
## [0.3.5] - 2017-01-01 | ||
## [0.3.6] - 2017-01-09 | ||
- Support parameterized types other than `Array` and `Object`, such as `Foo<T>`. | ||
## [0.3.5] - 2017-01-02 | ||
- Properties are now emitted as `readonly` when applicable. | ||
@@ -12,0 +15,0 @@ - Bump Analyzer for latest scanning features (getters/setters, static methods, methods/properties on class prototypes). |
@@ -135,2 +135,5 @@ "use strict"; | ||
} | ||
else if (isParameterizedType(node)) { | ||
t = convertParameterizedType(node, templateTypes); | ||
} | ||
else if (isUnion(node)) { | ||
@@ -187,2 +190,9 @@ t = convertUnion(node, templateTypes); | ||
]); | ||
/* | ||
* As above but only applicable when parameterized (`Foo<T>`) | ||
*/ | ||
const parameterizedRenameMap = new Map([ | ||
['HTMLCollection', 'HTMLCollectionOf'], | ||
['NodeList', 'NodeListOf'], | ||
]); | ||
/** | ||
@@ -217,2 +227,13 @@ * Return whether the given AST node is an expression that is nullable by | ||
} | ||
function convertParameterizedType(node, templateTypes) { | ||
if (!isName(node.expression)) { | ||
console.error('Could not find name of parameterized type'); | ||
return ts.anyType; | ||
} | ||
const types = node.applications.map((application) => convert(application, templateTypes)); | ||
const name = renameMap.get(node.expression.name) || | ||
parameterizedRenameMap.get(node.expression.name) || | ||
node.expression.name; | ||
return new ts.ParameterizedType(name, types); | ||
} | ||
function convertUnion(node, templateTypes) { | ||
@@ -271,2 +292,5 @@ return new ts.UnionType(node.elements.map((element) => convert(element, templateTypes))); | ||
} | ||
function isParameterizedType(node) { | ||
return node.type === 'TypeApplication'; | ||
} | ||
function isBareArray(node) { | ||
@@ -273,0 +297,0 @@ return node.type === 'NameExpression' && node.name === 'Array'; |
@@ -127,3 +127,3 @@ /** | ||
} | ||
export declare type Type = NameType | UnionType | ArrayType | FunctionType | ConstructorType | RecordType | IntersectionType | IndexableObjectType | ParamType; | ||
export declare type Type = NameType | UnionType | ArrayType | FunctionType | ConstructorType | RecordType | IntersectionType | IndexableObjectType | ParamType | ParameterizedType; | ||
export declare class NameType { | ||
@@ -157,2 +157,10 @@ readonly kind: string; | ||
} | ||
export declare class ParameterizedType { | ||
readonly kind: string; | ||
itemTypes: Type[]; | ||
name: string; | ||
constructor(name: string, itemTypes: Type[]); | ||
traverse(): Iterable<Node>; | ||
serialize(): string; | ||
} | ||
export declare class FunctionType { | ||
@@ -159,0 +167,0 @@ readonly kind: string; |
@@ -415,2 +415,21 @@ "use strict"; | ||
exports.ArrayType = ArrayType; | ||
// Foo<Bar> | ||
class ParameterizedType { | ||
constructor(name, itemTypes) { | ||
this.kind = 'parameterized'; | ||
this.name = name; | ||
this.itemTypes = itemTypes; | ||
} | ||
*traverse() { | ||
for (const itemType of this.itemTypes) { | ||
yield* itemType.traverse(); | ||
} | ||
yield this; | ||
} | ||
serialize() { | ||
const types = this.itemTypes.map((t) => t.serialize()); | ||
return `${this.name}<${types.join(', ')}>`; | ||
} | ||
} | ||
exports.ParameterizedType = ParameterizedType; | ||
// (foo: bar) => baz | ||
@@ -417,0 +436,0 @@ class FunctionType { |
{ | ||
"name": "@polymer/gen-typescript-declarations", | ||
"version": "0.3.5", | ||
"version": "0.3.6", | ||
"description": "Generate TypeScript type declarations for Polymer components.", | ||
@@ -5,0 +5,0 @@ "main": "lib/gen-ts.js", |
@@ -149,2 +149,4 @@ /** | ||
t = convertIndexableObject(node, templateTypes); | ||
} else if (isParameterizedType(node)) { // Type<T> | ||
t = convertParameterizedType(node, templateTypes); | ||
} else if (isUnion(node)) { // foo|bar | ||
@@ -195,2 +197,10 @@ t = convertUnion(node, templateTypes); | ||
/* | ||
* As above but only applicable when parameterized (`Foo<T>`) | ||
*/ | ||
const parameterizedRenameMap = new Map<string, string>([ | ||
['HTMLCollection', 'HTMLCollectionOf'], | ||
['NodeList', 'NodeListOf'], | ||
]); | ||
/** | ||
@@ -234,2 +244,17 @@ * Return whether the given AST node is an expression that is nullable by | ||
function convertParameterizedType( | ||
node: doctrine.type.TypeApplication, | ||
templateTypes: string[]): ts.ParameterizedType|ts.NameType { | ||
if (!isName(node.expression)) { | ||
console.error('Could not find name of parameterized type'); | ||
return ts.anyType; | ||
} | ||
const types = node.applications.map((application) => | ||
convert(application, templateTypes)); | ||
const name = renameMap.get(node.expression.name) || | ||
parameterizedRenameMap.get(node.expression.name) || | ||
node.expression.name; | ||
return new ts.ParameterizedType(name, types); | ||
} | ||
function convertUnion( | ||
@@ -306,2 +331,7 @@ node: doctrine.type.UnionType, templateTypes: string[]): ts.Type { | ||
function isParameterizedType(node: doctrine.Type): | ||
node is doctrine.type.TypeApplication { | ||
return node.type === 'TypeApplication'; | ||
} | ||
function isBareArray(node: doctrine.Type): | ||
@@ -308,0 +338,0 @@ node is doctrine.type.TypeApplication { |
@@ -389,3 +389,3 @@ /** | ||
export type Type = NameType|UnionType|ArrayType|FunctionType|ConstructorType| | ||
RecordType|IntersectionType|IndexableObjectType|ParamType; | ||
RecordType|IntersectionType|IndexableObjectType|ParamType|ParameterizedType; | ||
@@ -517,2 +517,26 @@ // string, MyClass, null, undefined, any | ||
// Foo<Bar> | ||
export class ParameterizedType { | ||
readonly kind = 'parameterized'; | ||
itemTypes: Type[]; | ||
name: string; | ||
constructor(name: string, itemTypes: Type[]) { | ||
this.name = name; | ||
this.itemTypes = itemTypes; | ||
} | ||
* traverse(): Iterable<Node> { | ||
for (const itemType of this.itemTypes) { | ||
yield* itemType.traverse(); | ||
} | ||
yield this; | ||
} | ||
serialize(): string { | ||
const types = this.itemTypes.map((t) => t.serialize()); | ||
return `${this.name}<${types.join(', ')}>`; | ||
} | ||
} | ||
// (foo: bar) => baz | ||
@@ -519,0 +543,0 @@ export class FunctionType { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
172882
3512