Socket
Socket
Sign inDemoInstall

@polymer/gen-typescript-declarations

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@polymer/gen-typescript-declarations - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

8

CHANGELOG.md

@@ -7,4 +7,10 @@ # Changelog

<!-- ## [Unreleased] -->
## [Unreleased]
## [0.3.0] - 2017-12-12
- `void` is not nullable.
- Support constructor functions (e.g. `function(new:HTMLElement, string)`).
- Support record types (e.g. `@param {{foo: bar}}`).
- Include method `@return` descriptions.
## [0.2.0] - 2017-12-08

@@ -11,0 +17,0 @@ - Many fixes. See https://github.com/Polymer/gen-typescript-declarations/issues/23.

@@ -118,2 +118,5 @@ "use strict";

}
else if (isRecordType(node)) {
t = convertRecord(node, templateTypes);
}
else if (isAllLiteral(node)) {

@@ -131,2 +134,5 @@ t = ts.anyType;

}
else if (isVoidLiteral(node)) {
t = new ts.NameType('void');
}
else if (isName(node)) {

@@ -154,2 +160,3 @@ t = new ts.NameType(node.name);

case 'boolean':
case 'void':
return false;

@@ -170,8 +177,46 @@ }

function convertFunction(node, templateTypes) {
return new ts.FunctionType(node.params.map((param, idx) => new ts.ParamType(
const params = node.params.map((param, idx) => new ts.ParamType(
// TypeScript wants named parameters, but we don't have names.
'p' + idx, convert(param, templateTypes))),
// Cast because typings are wrong: `FunctionType.result` is not an array.
node.result ? convert(node.result, templateTypes) : ts.anyType);
'p' + idx, convert(param, templateTypes)));
if (node.new) {
return new ts.ConstructorType(params,
// It doesn't make sense for a constructor to return something other
// than a named type. Also, in this context the name type is not
// nullable by default, so it's simpler to just directly convert here.
isName(node.this) ? new ts.NameType(node.this.name) : ts.anyType);
}
else {
return new ts.FunctionType(params,
// Cast because type is wrong: `FunctionType.result` is not an array.
node.result ? convert(node.result, templateTypes) : ts.anyType);
}
}
function convertRecord(node, templateTypes) {
const fields = [];
for (const field of node.fields) {
if (field.type !== 'FieldType') {
return ts.anyType;
}
let fieldType = field.value ? convert(field.value, templateTypes) : ts.anyType;
// In Closure you can't declare a record field optional, instead you
// declare `foo: bar|undefined`. In TypeScript we can represent this as
// `foo?: bar`. This also matches the semantics better, since Closure would
// allow the field to be omitted when it is `|undefined`, but TypeScript
// would require it to be explicitly set to `undefined`.
let optional = false;
if (fieldType.kind === 'union') {
fieldType.members = fieldType.members.filter((member) => {
if (member.kind === 'name' && member.name === 'undefined') {
optional = true;
return false;
}
return true;
});
// No need for a union if we collapsed it to one member.
fieldType.simplify();
}
fields.push(new ts.ParamType(field.key, fieldType, optional));
}
return new ts.RecordType(fields);
}
function isParameterizedArray(node) {

@@ -191,2 +236,5 @@ return node.type === 'TypeApplication' &&

}
function isRecordType(node) {
return node.type === 'RecordType';
}
function isNullable(node) {

@@ -210,2 +258,5 @@ return node.type === 'NullableType';

}
function isVoidLiteral(node) {
return node.type === 'VoidLiteral';
}
function isName(node) {

@@ -212,0 +263,0 @@ return node.type === 'NameExpression';

3

lib/gen-ts.js

@@ -288,3 +288,4 @@ "use strict";

templateTypes: feature.templateTypes,
returns: closure_types_1.closureTypeToTypeScript(feature.return && feature.return.type, feature.templateTypes)
returns: closure_types_1.closureTypeToTypeScript(feature.return && feature.return.type, feature.templateTypes),
returnsDescription: feature.return && feature.return.desc
});

@@ -291,0 +292,0 @@ for (const param of feature.params || []) {

@@ -153,3 +153,3 @@ /**

}
export declare type Type = NameType | UnionType | ArrayType | FunctionType;
export declare type Type = NameType | UnionType | ArrayType | FunctionType | ConstructorType | RecordType;
export declare class NameType {

@@ -191,2 +191,10 @@ readonly kind: string;

}
export declare class ConstructorType {
readonly kind: string;
params: ParamType[];
returns: NameType;
constructor(params: ParamType[], returns: NameType);
traverse(): Iterable<Node>;
serialize(): string;
}
export declare class ParamType {

@@ -196,7 +204,16 @@ readonly kind: string;

type: Type;
optional: boolean;
traverse(): Iterable<Node>;
constructor(name: string, type: Type);
constructor(name: string, type: Type, optional?: boolean);
serialize(): string;
}
export declare class RecordType {
readonly kind: string;
fields: ParamType[];
constructor(fields: ParamType[]);
traverse(): Iterable<Node>;
serialize(): string;
}
export declare const anyType: NameType;
export declare const nullType: NameType;
export declare const undefinedType: NameType;

@@ -479,3 +479,3 @@ "use strict";

serialize() {
const params = this.params.map((param) => `${param.name}: ${param.type.serialize()}`);
const params = this.params.map((param) => param.serialize());
return `(${params.join(', ')}) => ${this.returns.serialize()}`;

@@ -485,8 +485,29 @@ }

exports.FunctionType = FunctionType;
// {new(foo): bar}
class ConstructorType {
constructor(params, returns) {
this.kind = 'constructor';
this.params = params;
this.returns = returns;
}
*traverse() {
for (const p of this.params) {
yield* p.traverse();
}
yield* this.returns.traverse();
yield this;
}
serialize() {
const params = this.params.map((param) => param.serialize());
return `{new(${params.join(', ')}): ${this.returns.serialize()}}`;
}
}
exports.ConstructorType = ConstructorType;
// foo: bar
class ParamType {
constructor(name, type) {
constructor(name, type, optional = false) {
this.kind = 'param';
this.name = name;
this.type = type;
this.optional = optional;
}

@@ -497,4 +518,24 @@ *traverse() {

}
serialize() {
return `${this.name}${this.optional ? '?' : ''}: ${this.type.serialize()}`;
}
}
exports.ParamType = ParamType;
class RecordType {
constructor(fields) {
this.kind = 'record';
this.fields = fields;
}
*traverse() {
for (const m of this.fields) {
yield* m.traverse();
}
yield this;
}
serialize() {
const fields = this.fields.map((field) => field.serialize());
return `{${fields.join(', ')}}`;
}
}
exports.RecordType = RecordType;
exports.anyType = new NameType('any');

@@ -501,0 +542,0 @@ exports.nullType = new NameType('null');

{
"name": "@polymer/gen-typescript-declarations",
"version": "0.2.0",
"version": "0.3.0",
"description": "Generate TypeScript type declarations for Polymer components.",

@@ -5,0 +5,0 @@ "main": "lib/gen-ts.js",

@@ -13,3 +13,2 @@ /**

// TODO Object<foo>, Object<foo, bar>
// TODO Record types
//

@@ -129,2 +128,4 @@ // Useful resources for working on this package:

t = new ts.ArrayType(ts.anyType);
} else if (isRecordType(node)) { // {foo:bar}
t = convertRecord(node, templateTypes);
} else if (isAllLiteral(node)) { // *

@@ -138,2 +139,4 @@ t = ts.anyType;

t = ts.undefinedType;
} else if (isVoidLiteral(node)) { // void
t = new ts.NameType('void');
} else if (isName(node)) { // string, Object, MyClass, etc.

@@ -163,2 +166,3 @@ t = new ts.NameType(node.name);

case 'boolean':
case 'void':
return false

@@ -187,13 +191,57 @@ }

node: doctrine.type.FunctionType,
templateTypes: string[]): ts.FunctionType {
return new ts.FunctionType(
node.params.map(
(param, idx) => new ts.ParamType(
// TypeScript wants named parameters, but we don't have names.
'p' + idx,
convert(param, templateTypes))),
// Cast because typings are wrong: `FunctionType.result` is not an array.
node.result ? convert(node.result as any, templateTypes) : ts.anyType);
templateTypes: string[]): ts.FunctionType|ts.ConstructorType {
const params = node.params.map(
(param, idx) => new ts.ParamType(
// TypeScript wants named parameters, but we don't have names.
'p' + idx,
convert(param, templateTypes)));
if (node.new) {
return new ts.ConstructorType(
params,
// It doesn't make sense for a constructor to return something other
// than a named type. Also, in this context the name type is not
// nullable by default, so it's simpler to just directly convert here.
isName(node.this) ? new ts.NameType(node.this.name) : ts.anyType);
} else {
return new ts.FunctionType(
params,
// Cast because type is wrong: `FunctionType.result` is not an array.
node.result ? convert(node.result as any, templateTypes) : ts.anyType);
}
}
function convertRecord(node: doctrine.type.RecordType, templateTypes: string[]):
ts.RecordType|ts.NameType {
const fields = [];
for (const field of node.fields) {
if (field.type !== 'FieldType') {
return ts.anyType;
}
let fieldType =
field.value ? convert(field.value, templateTypes) : ts.anyType;
// In Closure you can't declare a record field optional, instead you
// declare `foo: bar|undefined`. In TypeScript we can represent this as
// `foo?: bar`. This also matches the semantics better, since Closure would
// allow the field to be omitted when it is `|undefined`, but TypeScript
// would require it to be explicitly set to `undefined`.
let optional = false;
if (fieldType.kind === 'union') {
fieldType.members = fieldType.members.filter((member) => {
if (member.kind === 'name' && member.name === 'undefined') {
optional = true;
return false;
}
return true;
});
// No need for a union if we collapsed it to one member.
fieldType.simplify();
}
fields.push(new ts.ParamType(field.key, fieldType, optional));
}
return new ts.RecordType(fields);
}
function isParameterizedArray(node: doctrine.Type):

@@ -219,2 +267,6 @@ node is doctrine.type.TypeApplication {

function isRecordType(node: doctrine.Type): node is doctrine.type.RecordType {
return node.type === 'RecordType';
}
function isNullable(node: doctrine.Type): node is doctrine.type.NullableType {

@@ -247,4 +299,8 @@ return node.type === 'NullableType';

function isVoidLiteral(node: doctrine.Type): node is doctrine.type.VoidLiteral {
return node.type === 'VoidLiteral';
}
function isName(node: doctrine.Type): node is doctrine.type.NameExpression {
return node.type === 'NameExpression';
}

@@ -329,3 +329,4 @@ /**

returns: closureTypeToTypeScript(
feature.return && feature.return.type, feature.templateTypes)
feature.return && feature.return.type, feature.templateTypes),
returnsDescription: feature.return && feature.return.desc
});

@@ -332,0 +333,0 @@

@@ -453,3 +453,4 @@ /**

// A TypeScript type expression.
export type Type = NameType|UnionType|ArrayType|FunctionType;
export type Type =
NameType|UnionType|ArrayType|FunctionType|ConstructorType|RecordType;

@@ -601,4 +602,3 @@ // string, MyClass, null, undefined, any

serialize(): string {
const params =
this.params.map((param) => `${param.name}: ${param.type.serialize()}`);
const params = this.params.map((param) => param.serialize());
return `(${params.join(', ')}) => ${this.returns.serialize()}`;

@@ -608,2 +608,27 @@ }

// {new(foo): bar}
export class ConstructorType {
readonly kind = 'constructor';
params: ParamType[];
returns: NameType;
constructor(params: ParamType[], returns: NameType) {
this.params = params;
this.returns = returns;
}
* traverse(): Iterable<Node> {
for (const p of this.params) {
yield* p.traverse();
}
yield* this.returns.traverse();
yield this;
}
serialize(): string {
const params = this.params.map((param) => param.serialize());
return `{new(${params.join(', ')}): ${this.returns.serialize()}}`;
}
}
// foo: bar

@@ -614,2 +639,3 @@ export class ParamType {

type: Type;
optional: boolean;

@@ -621,8 +647,34 @@ * traverse(): Iterable<Node> {

constructor(name: string, type: Type) {
constructor(name: string, type: Type, optional: boolean = false) {
this.name = name;
this.type = type;
this.optional = optional;
}
serialize() {
return `${this.name}${this.optional ? '?' : ''}: ${this.type.serialize()}`;
}
}
export class RecordType {
readonly kind = 'record';
fields: ParamType[];
constructor(fields: ParamType[]) {
this.fields = fields;
}
* traverse(): Iterable<Node> {
for (const m of this.fields) {
yield* m.traverse();
}
yield this;
}
serialize(): string {
const fields = this.fields.map((field) => field.serialize());
return `{${fields.join(', ')}}`;
}
}
export const anyType = new NameType('any');

@@ -629,0 +681,0 @@ export const nullType = new NameType('null');

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc