Skip to content

Commit

Permalink
chore: avoid duplicate variable name in swr function
Browse files Browse the repository at this point in the history
Fix #800
  • Loading branch information
soartec-lab committed Jan 1, 2024
1 parent 24b0130 commit 146f9bb
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/swr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
GeneratorOptions,
GeneratorVerbOptions,
GetterParams,
GetterProp,
GetterProps,
GetterPropType,
GetterResponse,
Expand Down Expand Up @@ -235,6 +236,12 @@ const generateSwrImplementation = ({
doc?: string;
}) => {
const swrProps = toObjectString(props, 'implementation');

const hasParamReservedWord = props.some(
(prop: GetterProp) => prop.name === 'query',
);
const queryResultVarName = hasParamReservedWord ? '_query' : 'query';

const httpFunctionProps = swrProperties;

const swrKeyImplementation = `const isEnabled = swrOptions?.enabled !== false${
Expand Down Expand Up @@ -291,7 +298,7 @@ ${doc}export const ${camel(
: ''
});
const query = useSwr<Awaited<ReturnType<typeof swrFn>>, TError>(swrKey, swrFn, ${
const ${queryResultVarName} = useSwr<Awaited<ReturnType<typeof swrFn>>, TError>(swrKey, swrFn, ${
swrOptions.options
? `{
${stringify(swrOptions.options)?.slice(1, -1)}
Expand All @@ -302,7 +309,7 @@ ${doc}export const ${camel(
return {
swrKey,
...query
...${queryResultVarName}
}
}\n`;
};
Expand Down

0 comments on commit 146f9bb

Please sign in to comment.