-
-
Notifications
You must be signed in to change notification settings - Fork 282
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
fix: avoid duplicate variable name in swr
function
#1130
fix: avoid duplicate variable name in swr
function
#1130
Conversation
444597a
to
5693ebe
Compare
packages/swr/src/index.ts
Outdated
@@ -235,6 +236,12 @@ const generateSwrImplementation = ({ | |||
doc?: string; | |||
}) => { | |||
const swrProps = toObjectString(props, 'implementation'); | |||
|
|||
const hasParamNameQuery = props.some( |
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.
OK then rename hasParamNamedQuery
to hasParamReservedWord
so people know why you are looking specifically for the word query
they will know its a reserved word.
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.
Please allow me to add to the explanation.
The purpose of using hasParamNameQuery
in the variable name is not whether it is a reserved word or not, but simply to check whether query
is used as a parameter.
Let's consider a case where another reserved word has increased.
For example, suppose you want to treat swrFn
as a reserved word and add _
to it.
In that case, I think the following will be added:
const hasParamNameSwrFn = props.some(
(param: GetterProp) => param.name === 'swrFn',
);
const queryResultVarName = hasParamNameSwrFn ? '_swrFn' : 'swrFn';
Because, variables used within functions with the same name are marked with _
depending on the existence of individual specified parameters, not whether the parameters contain reserved words. However, please note that this is just an example and there are other options if you actually want to respond.
From this, I think that the original hasParamNameQuery
represents the original intention of the variable name rather than hasParamReservedWord
as commented.
And I thought that the reason was expressed in the code, so it was sufficient, but what do you think?
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 disagree the word query
is to generic in terms of React Query, Vue Query, SWR? So this code tells me no reason wht you are looking for hasParamNamedQuery
its too specific with no reasoning why you are doing is.
"looking for a reserved word, preventing its use by appending an underscore so it does not collide with other usages" From looking at your code I can't tell any of that and no where does it mention query
is a reserved word you are looking for.
So someone looking at this code in 1 year will have the spend the time to think "why are they doing this" where as naming things properly like "checkReservedWord" or "cleanReservedWords" tells the developer exactly what you are doing without thinking about it.
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.
At the very least put a comment above the line.
// #800 Convert param named query to _query to prevent collision
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.
@melloware
Got it. I respect your opinion and change the function name from "hasParamNamedQuery" to "hasParamReservedWord".
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.
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.
See comment
5693ebe
to
146f9bb
Compare
swr
functionswr
function
swr
functionswr
function
Status
READY
Description
fix #800
When using
query
for an API parameter defined inOpenAPI
, the name of the argument in the automatically generated function and the name of the argument within the function collide.In that case, we changed the variable name
query
to_query
.Variables other than
query
are not considered because they are rarely used as parameters.Similar problems occur with
client
other thanswr
, so we will deal with them in order once this PR is merged.Related PRs
none
Todos
Steps to Test or Reproduce
query
in the parameter name as below:swr
forclient
inorval.config.js
as bellow:orval
executequery
to_query