[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:
parent
98ab1b6428
commit
d72f3aa758
@ -22,10 +22,8 @@ function () {
|
||||
key: "invite",
|
||||
value: function invite() {
|
||||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
||||
return function () {
|
||||
var privacy = options.privacy || "Private";
|
||||
console.log(this);
|
||||
}.apply(this);
|
||||
}
|
||||
}]);
|
||||
|
||||
|
||||
2
packages/babel-plugin-transform-typescript/test/fixtures/imports/enum-id/input.mjs
vendored
Normal file
2
packages/babel-plugin-transform-typescript/test/fixtures/imports/enum-id/input.mjs
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import { A } from 'lib'
|
||||
enum Enum { A };
|
||||
7
packages/babel-plugin-transform-typescript/test/fixtures/imports/enum-id/output.mjs
vendored
Normal file
7
packages/babel-plugin-transform-typescript/test/fixtures/imports/enum-id/output.mjs
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
var Enum;
|
||||
|
||||
(function (Enum) {
|
||||
Enum[Enum["A"] = 0] = "A";
|
||||
})(Enum || (Enum = {}));
|
||||
|
||||
;
|
||||
2
packages/babel-plugin-transform-typescript/test/fixtures/imports/enum-value/input.mjs
vendored
Normal file
2
packages/babel-plugin-transform-typescript/test/fixtures/imports/enum-value/input.mjs
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import { A } from 'lib'
|
||||
enum Enum { id = A };
|
||||
8
packages/babel-plugin-transform-typescript/test/fixtures/imports/enum-value/output.mjs
vendored
Normal file
8
packages/babel-plugin-transform-typescript/test/fixtures/imports/enum-value/output.mjs
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
import { A } from 'lib';
|
||||
var Enum;
|
||||
|
||||
(function (Enum) {
|
||||
Enum[Enum["id"] = A] = "id";
|
||||
})(Enum || (Enum = {}));
|
||||
|
||||
;
|
||||
@ -0,0 +1,2 @@
|
||||
import { A } from 'lib'
|
||||
const obj: { A: A } = { A: 'foo' };
|
||||
@ -0,0 +1,3 @@
|
||||
const obj = {
|
||||
A: 'foo'
|
||||
};
|
||||
@ -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;
|
||||
|
||||
@ -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() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user