Skip to content
This repository has been archived by the owner on Feb 20, 2020. It is now read-only.

Commit

Permalink
fix(object-member): support numeric name (#104)
Browse files Browse the repository at this point in the history
Before

```ts
dts.emit(dts.parse_type("{ 0: 1 }")) //=> { "0": 1 }
```

After

```ts
dts.emit(dts.parse_type("{ 0: 1 }")) //=> { 0: 1 }
```
  • Loading branch information
ikatyang committed Aug 14, 2018
1 parent e700198 commit f8cf7c9
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ before_install:

script:
- yarn lint
- yarn test --verbose
- yarn test --verbose --runInBand

after_script:
- codecov
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/__snapshots__/parse.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,8 @@ import xyz = require(\\"xyz\\");
type X = A[B[C]];
type TypeName<T> = T extends string ? \\"string\\" : T extends number ? \\"number\\" : T extends boolean ? \\"boolean\\" : T extends undefined ? \\"undefined\\" : T extends Function ? \\"function\\" : \\"object\\";
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : T;
type X = {
0: 123;
};
"
`;
4 changes: 4 additions & 0 deletions src/__tests__/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ type TypeName<T> =
"object";
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : T;
type X = {
0: 123
}
`;

it('should return correctly', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ exports[`should return correctly with name, members 1`] = `
c = \\"hello\\"
}"
`;

exports[`should return correctly with numerical member name 1`] = `
"declare enum E {
0 = 0
}"
`;
15 changes: 15 additions & 0 deletions src/declarations/__tests__/enum-declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ it('should return correctly with name, export, members', () => {
).toMatchSnapshot();
});

it('should return correctly with numerical member name', () => {
expect(
emit(
create_enum_declaration({
name: 'E',
members: [
create_variable_declaration({
name: 0,
}),
],
}),
),
).toMatchSnapshot();
});

it('should throw error with members (VariableDeclaration with type (not undefined or LiteralType))', () => {
expect(() =>
emit(
Expand Down
9 changes: 7 additions & 2 deletions src/declarations/enum-declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
IElement,
IElementOptions,
} from '../element';
import { add_declare_modifier_if_need } from '../utils';
import { add_declare_modifier_if_need, is_valid_identifier } from '../utils';
import { IVariableDeclaration } from './variable-declaration';

export interface IEnumDeclarationOptions extends IElementOptions {
Expand Down Expand Up @@ -69,7 +69,12 @@ export const transform_enum_declaration = (
counter = value + 1;
}
return ts.createEnumMember(
/* name */ variable_declaration.name,
/* name */ typeof variable_declaration.name === 'string' &&
is_valid_identifier(variable_declaration.name)
? variable_declaration.name
: (ts.createLiteral(variable_declaration.name) as
| ts.StringLiteral
| ts.NumericLiteral),
/* initializer */ ts.createLiteral(value),
);
}),
Expand Down
9 changes: 7 additions & 2 deletions src/declarations/variable-declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { transform } from '../transform';
import { add_declare_modifier_if_need } from '../utils';

export interface IVariableDeclarationOptions extends IElementOptions {
name: string;
name:
| string
// for object member
| number;
type?: IType;
export?: boolean;
const?: boolean;
Expand Down Expand Up @@ -52,7 +55,9 @@ export const transform_variable_declaration = (
path,
),
/* declarationList */ ts.createVariableDeclarationList(
/* declarations */ [ts.createVariableDeclaration(element.name, type)],
/* declarations */ [
ts.createVariableDeclaration(element.name.toString(), type),
],
/* flags */ element.const === true
? ts.NodeFlags.Const
: element.let === true ? ts.NodeFlags.Let : undefined,
Expand Down
7 changes: 5 additions & 2 deletions src/members/class-member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ export const transform_class_member = (
return ts.createProperty(
/* decorators */ undefined,
/* modifiers */ modifiers,
/* name */ is_valid_identifier(element.owned.name)
/* name */ typeof element.owned.name === 'string' &&
is_valid_identifier(element.owned.name)
? element.owned.name
: ts.createLiteral(element.owned.name),
: (ts.createLiteral(element.owned.name) as
| ts.StringLiteral
| ts.NumericLiteral),
/* questionToken */ question_token,
/* type */ transform(
element.owned.type || any_type,
Expand Down
7 changes: 5 additions & 2 deletions src/members/object-member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ export const transform_object_member = (
case ElementKind.VariableDeclaration:
return ts.createPropertySignature(
/* modifiers */ modifiers,
/* name */ is_valid_identifier(element.owned.name)
/* name */ typeof element.owned.name === 'string' &&
is_valid_identifier(element.owned.name)
? element.owned.name
: ts.createLiteral(element.owned.name),
: (ts.createLiteral(element.owned.name) as
| ts.StringLiteral
| ts.NumericLiteral),
/* questionToken */ question_token,
/* type */ transform(
element.owned.type || any_type,
Expand Down
5 changes: 4 additions & 1 deletion src/parsers/property-signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ export const parse_property_signature = (
): IObjectMember =>
create_object_member({
owned: create_variable_declaration({
name: (node.name as ts.Identifier).text,
name:
node.name.kind === ts.SyntaxKind.NumericLiteral
? +(node.name as ts.NumericLiteral).text
: (node.name as ts.Identifier).text,
type: if_defined(node.type, parse_native),
}),
readonly: has_kind(node.modifiers, ts.SyntaxKind.ReadonlyKeyword),
Expand Down
6 changes: 6 additions & 0 deletions src/types/__tests__/__snapshots__/object-type.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ exports[`should return correctly 1`] = `
[key: string]: any;
}"
`;

exports[`should return correctly with numerical name 1`] = `
"{
0: any;
}"
`;
17 changes: 17 additions & 0 deletions src/types/__tests__/object-type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { string_type } from '../../constants';
import { create_variable_declaration } from '../../declarations';
import { create_function_declaration } from '../../declarations/function-declaration';
import { create_parameter_declaration } from '../../declarations/parameter-declaration';
import { emit } from '../../emit';
Expand Down Expand Up @@ -28,6 +29,22 @@ it('should return correctly', () => {
).toMatchSnapshot();
});

it('should return correctly with numerical name', () => {
expect(
emit(
create_object_type({
members: [
create_object_member({
owned: create_variable_declaration({
name: 0,
}),
}),
],
}),
),
).toMatchSnapshot();
});

describe('is_object_type', () => {
it('should return correctly', () => {
const element = create_object_type({
Expand Down

0 comments on commit f8cf7c9

Please sign in to comment.