babel-plugin-react-query-string-key-gen
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -20,5 +20,16 @@ var __defProp = Object.defineProperty; | ||
const {node} = path; | ||
if (!isUseQueryCall(t, node.callee) || hasStringOnlyQueryKeyParam(t, node)) { | ||
if (!isUseQueryCall(t, node.callee)) { | ||
return; | ||
} | ||
const stringKeyLiteral = extractParamsQueryFnName(t, node); | ||
if (hasStringOnlyQueryKeyParam(t, node)) { | ||
if (stringKeyLiteral) { | ||
if (node.arguments[0].value === "") { | ||
node.arguments[0].value = stringKeyLiteral.value; | ||
} else { | ||
node.arguments.unshift(stringKeyLiteral); | ||
} | ||
} | ||
return; | ||
} | ||
if (hasParamsArrayKey(t, node)) { | ||
@@ -28,11 +39,9 @@ if (hasStringKeyParam(t, node)) { | ||
} | ||
const [arrayKey, queryFn] = node.arguments; | ||
let stringKeyLiteral = null; | ||
if (t.isIdentifier(queryFn)) { | ||
stringKeyLiteral = t.stringLiteral(queryFn.name); | ||
} else if (t.isFunction(queryFn)) { | ||
stringKeyLiteral = extractQueryFnNameFromBody(t, queryFn); | ||
} | ||
const [arrayKey] = node.arguments; | ||
if (stringKeyLiteral) { | ||
arrayKey.elements.unshift(stringKeyLiteral); | ||
if (t.isStringLiteral(arrayKey.elements[0])) { | ||
arrayKey.elements[0].value = stringKeyLiteral.value; | ||
} else { | ||
arrayKey.elements.unshift(stringKeyLiteral); | ||
} | ||
} | ||
@@ -65,2 +74,12 @@ } else if (hasQueryObject(t, node)) { | ||
} | ||
function extractParamsQueryFnName(t, node) { | ||
const [, queryFn] = node.arguments; | ||
let stringKeyLiteral = null; | ||
if (t.isIdentifier(queryFn)) { | ||
stringKeyLiteral = t.stringLiteral(queryFn.name); | ||
} else if (t.isFunction(queryFn)) { | ||
stringKeyLiteral = extractQueryFnNameFromBody(t, queryFn); | ||
} | ||
return stringKeyLiteral; | ||
} | ||
function extractQueryFnNameFromBody(t, queryFn) { | ||
@@ -67,0 +86,0 @@ if (hasCalleeName(t, queryFn.body)) { |
{ | ||
"name": "babel-plugin-react-query-string-key-gen", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "author": "Dominic Lee", |
@@ -5,4 +5,4 @@ # babel-plugin-react-query-string-key-gen | ||
Naming things well is difficult in programming. A poorly named thing could cause further confusion for the next person reading your code. | ||
However, there is a fairly strong convention to include a string key as an identifier in the react-query query key. | ||
React Query uses the convention of a string key as a unique, readable identifier for queries. However, naming things well is difficult. | ||
Poor naming causes further confusion for the next person reading your code. | ||
@@ -12,3 +12,3 @@ ### Solution | ||
We could generate this key at build-time, inferring from the name of it's associated query function. Why name things twice when you can just | ||
reuse what's already there? This babel plugin will statically analyze your code and extract the query function from your `useQuery` hooks. | ||
reuse what's already there? This babel plugin will statically analyze your code and extract the query function name from your `useQuery` hooks. | ||
@@ -37,3 +37,3 @@ ### Installation | ||
This plugin makes a few assumptions about your query key order: | ||
This plugin makes a few assumptions about your query key order and query function: | ||
@@ -40,0 +40,0 @@ 1. It expects your unique string key to always be the first element in your array key. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
8867
138
0