ESLint plugin to prevent hardcoded query parameter names in URL construction
npm install eslint-plugin-no-hardcoded-query-paramsESLint plugin to prevent hardcoded query parameter names in URL construction.
``bash`
npm install --save-dev eslint-plugin-no-hardcoded-query-params
`javascript
import noHardcodedQueryParams from 'eslint-plugin-no-hardcoded-query-params';
export default [
{
plugins: {
'no-hardcoded-query-params': noHardcodedQueryParams,
},
rules: {
'no-hardcoded-query-params/no-hardcoded-query-params': 'error',
},
},
];
`
`javascript`
export default [
{
plugins: {
'no-hardcoded-query-params': noHardcodedQueryParams,
},
rules: {
'no-hardcoded-query-params/no-hardcoded-query-params': [
'error',
{
allowedPatterns: ['^utm_', '^test-'],
},
],
},
},
];
`javascript`
export default [
{
plugins: {
'no-hardcoded-query-params': noHardcodedQueryParams,
},
rules: {
'no-hardcoded-query-params/no-hardcoded-query-params': 'error',
},
},
{
files: ['/.test.ts', '/.spec.ts'],
rules: {
'no-hardcoded-query-params/no-hardcoded-query-params': 'off',
},
},
];
This rule prevents hardcoded query parameter names in URL construction, encouraging the use of centralized enums or constants.
`javascript${baseUrl}?user-id=${userId}
const url = process.env.SELF_URL + '?referral-code=' + code;
const link = ;`
searchParams.set('page-number', '1');
`javascript${baseUrl}?${QueryParams.USER_ID}=${userId}
const url = process.env.SELF_URL + '?' + QueryParams.REFERRAL_CODE + '=' + code;
const link = ;`
searchParams.set(QueryParams.PAGE_NUMBER, '1');
Array of regex patterns for query parameter names that are allowed to be hardcoded.
Example:
`javascript`
{
allowedPatterns: ['^utm_', '^fb_', '^test-']
}
This would allow parameters like utm_source, utm_campaign, fb_clid, test-param`, etc.