diff --git a/package.json b/package.json index 363a33f4..e2786ea9 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { - "name": "@khanacademy/flow-to-ts", - "version": "0.5.2", + "name": "@monzo/flow-to-ts", + "version": "0.5.3", "publishConfig": { "access": "public" }, "repository": { "type": "git", - "url": "https://github.com/khan/flow-to-ts" + "url": "https://github.com/monzo/flow-to-ts" }, "main": "dist/convert.js", "bin": { diff --git a/src/transform.ts b/src/transform.ts index c6b8712f..d9a72eaf 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -277,6 +277,11 @@ export const transform = { path.replaceWith(t.tsArrayType(elementType)); }, }, + SymbolTypeAnnotation: { + exit(path) { + path.replaceWith(t.tsSymbolKeyword()); + }, + }, TupleTypeAnnotation: { exit(path) { const { types } = path.node; diff --git a/src/transforms/object-type.ts b/src/transforms/object-type.ts index 84798ca9..84548aba 100644 --- a/src/transforms/object-type.ts +++ b/src/transforms/object-type.ts @@ -138,8 +138,8 @@ export const ObjectTypeCallProperty = { export const ObjectTypeProperty = { exit(path, state) { + let { key } = path.node; const { - key, value, optional, variance, @@ -151,9 +151,8 @@ export const ObjectTypeProperty = { } = path.node; // TODO: static, kind const typeAnnotation = t.tsTypeAnnotation(value); const initializer = undefined; // TODO: figure out when this used - const computed = false; // TODO: maybe set this to true for indexers + let computed = false; const readonly = variance && variance.kind === "plus"; - if (variance && variance.kind === "minus") { // TODO: include file and location of infraction console.warn("typescript doesn't support writeonly properties"); @@ -162,6 +161,16 @@ export const ObjectTypeProperty = { console.warn("we don't handle get() or set() yet, :P"); } + if (t.isIdentifier(key)) { + if (key.name.startsWith("@@")) { + key = t.memberExpression( + t.identifier("Symbol"), + t.identifier(key.name.replace("@@", "")) + ); + computed = true; + } + } + if (method) { // TODO: assert value is a FunctionTypeAnnotation const methodSignature = { diff --git a/test/fixtures/convert/basic-types/symbol01/flow.js b/test/fixtures/convert/basic-types/symbol01/flow.js new file mode 100644 index 00000000..5bd5f8b9 --- /dev/null +++ b/test/fixtures/convert/basic-types/symbol01/flow.js @@ -0,0 +1 @@ +let a: symbol; diff --git a/test/fixtures/convert/basic-types/symbol01/ts.js b/test/fixtures/convert/basic-types/symbol01/ts.js new file mode 100644 index 00000000..5bd5f8b9 --- /dev/null +++ b/test/fixtures/convert/basic-types/symbol01/ts.js @@ -0,0 +1 @@ +let a: symbol; diff --git a/test/fixtures/convert/interfaces/interface-well-known-symbol01/flow.js b/test/fixtures/convert/interfaces/interface-well-known-symbol01/flow.js new file mode 100644 index 00000000..fb9f1323 --- /dev/null +++ b/test/fixtures/convert/interfaces/interface-well-known-symbol01/flow.js @@ -0,0 +1,3 @@ +interface Iterable { + @@iterator(): Iterator; +} diff --git a/test/fixtures/convert/interfaces/interface-well-known-symbol01/ts.js b/test/fixtures/convert/interfaces/interface-well-known-symbol01/ts.js new file mode 100644 index 00000000..3432ec74 --- /dev/null +++ b/test/fixtures/convert/interfaces/interface-well-known-symbol01/ts.js @@ -0,0 +1,3 @@ +interface Iterable { + [Symbol.iterator](): Iterator; +} \ No newline at end of file diff --git a/test/fixtures/convert/intersection-union/union02/flow.js b/test/fixtures/convert/intersection-union/union02/flow.js new file mode 100644 index 00000000..50ad4997 --- /dev/null +++ b/test/fixtures/convert/intersection-union/union02/flow.js @@ -0,0 +1 @@ +let obj: string | symbol; diff --git a/test/fixtures/convert/intersection-union/union02/ts.js b/test/fixtures/convert/intersection-union/union02/ts.js new file mode 100644 index 00000000..50ad4997 --- /dev/null +++ b/test/fixtures/convert/intersection-union/union02/ts.js @@ -0,0 +1 @@ +let obj: string | symbol;