Skip to content

Commit

Permalink
refactor: Lint for no interpolation in regular string (#5060) (no-cha…
Browse files Browse the repository at this point in the history
…ngelog)

* ✨ Create rule `no-interpolation-in-regular-string`

* 👕 Enable rule

* ⚡ Run rule (no issues) and add exception

* ⚡ Simplify regex

To account for expressions and to make it less expensive
  • Loading branch information
ivov authored Dec 29, 2022
1 parent ba0fd8a commit 0b47f9c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/@n8n_io/eslint-config/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ const config = (module.exports = {

'n8n-local-rules/no-unneeded-backticks': 'error',

'n8n-local-rules/no-interpolation-in-regular-string': 'error',

// ******************************************************************
// overrides to base ruleset
// ******************************************************************
Expand Down
30 changes: 30 additions & 0 deletions packages/@n8n_io/eslint-config/local-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,36 @@ module.exports = {
};
},
},

'no-interpolation-in-regular-string': {
meta: {
type: 'problem',
docs: {
description:
'String interpolation `${...}` requires backticks, not single or double quotes.',
recommended: 'error',
},
messages: {
useBackticks: 'Use backticks to interpolate',
},
fixable: 'code',
},
create(context) {
return {
Literal(node) {
if (typeof node.value !== 'string') return;

if (/\$\{/.test(node.value)) {
context.report({
messageId: 'useBackticks',
node,
fix: (fixer) => fixer.replaceText(node, `\`${node.value}\``),
});
}
},
};
},
},
};

const isJsonParseCall = (node) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { completeFromList, snippetCompletion } from '@codemirror/autocomplete';
*/
export const jsSnippets = completeFromList([
...snippets.filter((snippet) => snippet.label !== 'class'),
// eslint-disable-next-line n8n-local-rules/no-interpolation-in-regular-string
snippetCompletion('console.log(${arg})', { label: 'console.log()' }),
snippetCompletion('DateTime', { label: 'DateTime' }),
snippetCompletion('Interval', { label: 'Interval' }),
Expand Down

0 comments on commit 0b47f9c

Please sign in to comment.