[plugin-transform-typescript] Strip type imports used in Enums and object types (#9605)

* fix: strip type imports used in Enums and object types

* chore: update failing snapshot

* docs: correct TSPropertySignature comment

* fix: enum value should be considered a reference

* chore: add tests for TSPropertySignature and TSEnumMember
This commit is contained in:
Evan Henley
2019-02-28 15:03:12 -06:00
committed by Nicolò Ribaudo
parent 98ab1b6428
commit d72f3aa758
9 changed files with 84 additions and 6 deletions

View File

@@ -134,6 +134,20 @@ export default function isReferenced(
// no: type X = { NODE: OtherType }
case "ObjectTypeProperty":
return parent.key !== node;
// yes: enum X { Foo = NODE }
// no: enum X { NODE }
case "TSEnumMember":
return parent.id !== node;
// yes: { [NODE]: value }
// no: { NODE: value }
case "TSPropertySignature":
if (parent.key === node) {
return !!parent.computed;
}
return true;
}
return true;

View File

@@ -122,7 +122,7 @@ describe("validators", function() {
expect(t.isReferenced(node, parent)).toBe(true);
});
it("returns true if node id a value of ObjectProperty of an expression", function() {
it("returns true if node is a value of ObjectProperty of an expression", function() {
const node = t.identifier("a");
const parent = t.objectProperty(t.identifier("key"), node);
const grandparent = t.objectExpression([parent]);
@@ -130,13 +130,55 @@ describe("validators", function() {
expect(t.isReferenced(node, parent, grandparent)).toBe(true);
});
it("returns false if node id a value of ObjectProperty of a pattern", function() {
it("returns false if node is a value of ObjectProperty of a pattern", function() {
const node = t.identifier("a");
const parent = t.objectProperty(t.identifier("key"), node);
const grandparent = t.objectPattern([parent]);
expect(t.isReferenced(node, parent, grandparent)).toBe(false);
});
describe("TSPropertySignature", function() {
it("returns false if node is a key", function() {
// { A: string }
const node = t.identifier("A");
const parent = t.tsPropertySignature(
node,
t.tsTypeAnnotation(t.tsStringKeyword()),
);
expect(t.isReferenced(node, parent)).toBe(false);
});
it("returns true if node is a value", function() {
// { someKey: A }
const node = t.identifier("A");
const parent = t.tsPropertySignature(
t.identifier("someKey"),
t.tsTypeAnnotation(t.tsTypeReference(node)),
);
expect(t.isReferenced(node, parent)).toBe(true);
});
});
describe("TSEnumMember", function() {
it("returns false if node is an id", function() {
// enum X = { A };
const node = t.identifier("A");
const parent = t.tsEnumMember(node);
expect(t.isReferenced(node, parent)).toBe(false);
});
it("returns true if node is a value", function() {
// enum X = { Foo = A }
const node = t.identifier("A");
const parent = t.tsEnumMember(t.identifier("Foo"), node);
expect(t.isReferenced(node, parent)).toBe(true);
});
});
});
describe("isBinding", function() {