New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option for adding variables with PropTypes. #1
Conversation
I will try resolve conflicts. |
I'm curious why you're using/importing |
This is a temporary solution. |
6af7a3c
to
814f08b
Compare
@ruslankhh Just trying to understand your process. So are you generating TS declarations and then compiling the |
Nope)
|
Unrelated to this PR, I'd highly suggest not using |
README.md
Outdated
@@ -208,3 +208,43 @@ class Example extends React.Component { | |||
} | |||
} | |||
``` | |||
|
|||
- `addVariables` (boolean) - Adding variables for props. Defaults to `false`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of addVariables
, let's call it exportVariableDeclarations
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but maybe call declarePropTypeVariables
? It's not only export:
https://github.com/milesj/babel-plugin-typescript-to-proptypes/pull/1/files#diff-ada6231da2b06b9c22c8023a81419998R15
src/addToClass.ts
Outdated
@@ -49,9 +53,10 @@ export default function addToClass(node: t.ClassDeclaration, state: ConvertState | |||
if (propTypes) { | |||
propTypes.value = mergePropTypes(propTypes.value, propTypesList, state); | |||
} else { | |||
const isVariable = addVariables && typeNames.length === 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think instead of assuming that typeName === a variable, you should check for imports and validate that it's actually a variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this?
export default class ClassStandard extends React.Component<{ name: string }> {
render() {
return null;
}
}
Like that?
const isVariable = (
state.options.declarePropTypeVariables &&
typeNames.length === 1 &&
state.componentTypes[typeNames[0]]
);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or do you mean to check if such a variable exists?
But it's redundant. We create it in the same location where we save this type in state:
babel-plugin-typescript-to-proptypes/src/index.ts
Lines 210 to 218 in afb36e2
state.componentTypes[node.id.name] = extractTypeProperties( | |
node, | |
state.componentTypes, | |
); | |
if (state.options.declarePropTypeVariables) { | |
addVariable(path, node.id.name, state); | |
} | |
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or do you think I'm adding a variable just for export?
That's not so. A variable is created for all types:
babel-plugin-typescript-to-proptypes/tests/__snapshots__/index.test.ts.snap
Lines 186 to 199 in afb36e2
interface Props { | |
name: string; | |
} | |
const Props = { | |
name: _pt.string.isRequired | |
}; | |
export default class ClassCustomReactImportNamePure extends R.PureComponent<Props> { | |
static propTypes = Props; | |
render() { | |
return null; | |
} | |
}" |
d4d2c78
to
1f5edf5
Compare
state.componentTypes[node.id.name] = extractTypeProperties( | ||
node, | ||
state.componentTypes, | ||
); | ||
|
||
if (state.options.declarePropTypeVariables) { | ||
addVariable(path, node.id.name, state); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should only add variables when the interface is for props/state. We shouldn't do it for every interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How we can do it? Match with the name suffix Props|State
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll need to use the extracted generic names from classes/functions, and cross reference that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we don't have classes/functions in d.ts
?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or we should write everytime classes/function in d.ts
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I won't allow this feature if it generates for every interface/type alias that exists. That would blow up the filesize unnecessarily.
Maybe if the setting was a regex pattern:
declarePropTypesVariables: /(Props|State)$/,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will think about it.
Our conversation got me thinking. |
I think a fork might be best, as it's a very specific use case. Glad you got it working. |
Hi!
We need this case for add our d.ts interfaces to JS as PropTypes. This is a temporary solution when moving our internal library from JS to TS. In the circumstances of using our library in both JS and TS projects.
addVariables
(boolean) - Adding variables for props. Defaults tofalse
.