Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Convert ElementConfig to JSX.LibraryManagedAttributes, fixes #155 #240

Merged
merged 1 commit into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,17 +523,17 @@ const transform = {
) {
const inline = utilityTypes[typeName.name];
path.replaceWith(inline(...typeParameters.params));
return;
} else if (typeof utilityTypes[typeName.name] === "string") {
const replacementName = utilityTypes[typeName.name];
path.replaceWith(
t.tsTypeReference(t.identifier(replacementName), typeParameters)
);
state.usedUtilityTypes.add(replacementName);
return;
} else {
state.usedUtilityTypes.add(typeName.name);
}

return;
}

if (typeName.name in UnqualifiedReactTypeNameMap) {
Expand All @@ -548,9 +548,41 @@ const transform = {
typeParameters.params.length > 0 ? typeParameters : null
)
);
} else {
path.replaceWith(t.tsTypeReference(typeName, typeParameters));
return;
}

if (t.isTSQualifiedName(id)) {
const { left, right } = id;

// React.ElementConfig<T> -> JSX.LibraryManagedAttributes<T, React.ComponentProps<T>>
if (
t.isIdentifier(left, { name: "React" }) &&
t.isIdentifier(right, { name: "ElementConfig" })
) {
path.replaceWith(
t.tsTypeReference(
t.tsQualifiedName(
t.identifier("JSX"),
t.identifier("LibraryManagedAttributes")
),
t.tsTypeParameterInstantiation([
typeParameters.params[0],
t.tsTypeReference(
t.tsQualifiedName(
t.identifier("React"),
t.identifier("ComponentProps")
),
t.tsTypeParameterInstantiation([typeParameters.params[0]])
),
])
)
);
return;
}
}

// fallthrough case
path.replaceWith(t.tsTypeReference(typeName, typeParameters));
},
},
QualifiedTypeIdentifier: {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/convert/react/element-config-typeof/flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @flow
import * as React from "react";
type Props = React.ElementConfig<typeof Foo>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"prettier": true,
"prettierOptions": {
"semi": true,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "all"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/convert/react/element-config-typeof/ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as React from "react";
type Props = JSX.LibraryManagedAttributes<
typeof Foo,
React.ComponentProps<typeof Foo>
>;
3 changes: 3 additions & 0 deletions test/fixtures/convert/react/element-config/flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @flow
import * as React from "react";
type Props = React.ElementConfig<T>;
2 changes: 2 additions & 0 deletions test/fixtures/convert/react/element-config/ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import * as React from "react";
type Props = JSX.LibraryManagedAttributes<T, React.ComponentProps<T>>;