From e6f5640e76e87d08a9463bc551e6a9160318a088 Mon Sep 17 00:00:00 2001 From: Franz Becker Date: Tue, 31 Aug 2021 13:36:28 +0100 Subject: [PATCH 1/2] support transformation of symbol --- src/transform.ts | 5 +++++ test/fixtures/convert/basic-types/symbol01/flow.js | 1 + test/fixtures/convert/basic-types/symbol01/ts.js | 1 + test/fixtures/convert/intersection-union/union02/flow.js | 1 + test/fixtures/convert/intersection-union/union02/ts.js | 1 + 5 files changed, 9 insertions(+) create mode 100644 test/fixtures/convert/basic-types/symbol01/flow.js create mode 100644 test/fixtures/convert/basic-types/symbol01/ts.js create mode 100644 test/fixtures/convert/intersection-union/union02/flow.js create mode 100644 test/fixtures/convert/intersection-union/union02/ts.js 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/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/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; From a8ffde3a808b1a7ab0036bdae0d5c8b37fa2d771 Mon Sep 17 00:00:00 2001 From: Franz Becker Date: Fri, 17 Sep 2021 17:36:40 +0100 Subject: [PATCH 2/2] add support for well known symbols --- src/transforms/object-type.ts | 15 ++++++++++++--- .../interface-well-known-symbol01/flow.js | 3 +++ .../interface-well-known-symbol01/ts.js | 3 +++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/convert/interfaces/interface-well-known-symbol01/flow.js create mode 100644 test/fixtures/convert/interfaces/interface-well-known-symbol01/ts.js 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/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