Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-react-query-string-key-gen

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-react-query-string-key-gen - npm Package Compare versions

Comparing version 0.2.0 to 0.2.1

37

dist/index.js

@@ -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.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc