[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

@ -22,10 +22,8 @@ function () {
key: "invite", key: "invite",
value: function invite() { value: function invite() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return function () { var privacy = options.privacy || "Private";
var privacy = options.privacy || "Private"; console.log(this);
console.log(this);
}.apply(this);
} }
}]); }]);

View File

@ -0,0 +1,2 @@
import { A } from 'lib'
enum Enum { A };

View File

@ -0,0 +1,7 @@
var Enum;
(function (Enum) {
Enum[Enum["A"] = 0] = "A";
})(Enum || (Enum = {}));
;

View File

@ -0,0 +1,2 @@
import { A } from 'lib'
enum Enum { id = A };

View File

@ -0,0 +1,8 @@
import { A } from 'lib';
var Enum;
(function (Enum) {
Enum[Enum["id"] = A] = "id";
})(Enum || (Enum = {}));
;

View File

@ -0,0 +1,2 @@
import { A } from 'lib'
const obj: { A: A } = { A: 'foo' };

View File

@ -0,0 +1,3 @@
const obj = {
A: 'foo'
};

View File

@ -134,6 +134,20 @@ export default function isReferenced(
// no: type X = { NODE: OtherType } // no: type X = { NODE: OtherType }
case "ObjectTypeProperty": case "ObjectTypeProperty":
return parent.key !== node; 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; return true;

View File

@ -122,7 +122,7 @@ describe("validators", function() {
expect(t.isReferenced(node, parent)).toBe(true); 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 node = t.identifier("a");
const parent = t.objectProperty(t.identifier("key"), node); const parent = t.objectProperty(t.identifier("key"), node);
const grandparent = t.objectExpression([parent]); const grandparent = t.objectExpression([parent]);
@ -130,13 +130,55 @@ describe("validators", function() {
expect(t.isReferenced(node, parent, grandparent)).toBe(true); 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 node = t.identifier("a");
const parent = t.objectProperty(t.identifier("key"), node); const parent = t.objectProperty(t.identifier("key"), node);
const grandparent = t.objectPattern([parent]); const grandparent = t.objectPattern([parent]);
expect(t.isReferenced(node, parent, grandparent)).toBe(false); 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() { describe("isBinding", function() {