From bacb6ab4c0315a596508e1387ef63b9a191f4ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Thu, 29 Dec 2022 13:03:57 +0100 Subject: [PATCH 1/4] :sparkles: Create rule `no-interpolation-in-regular-string` --- packages/@n8n_io/eslint-config/local-rules.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/packages/@n8n_io/eslint-config/local-rules.js b/packages/@n8n_io/eslint-config/local-rules.js index c7a6968fe3430..6fd1d478531d1 100644 --- a/packages/@n8n_io/eslint-config/local-rules.js +++ b/packages/@n8n_io/eslint-config/local-rules.js @@ -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 (/\$\{[a-zA-Z0-9_$]*\}/.test(node.value)) { + context.report({ + messageId: 'useBackticks', + node, + fix: (fixer) => fixer.replaceText(node, `\`${node.value}\``), + }); + } + }, + }; + }, + }, }; const isJsonParseCall = (node) => From 81a765670b52b233ab435e47d60bbc131de02907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Thu, 29 Dec 2022 13:04:05 +0100 Subject: [PATCH 2/4] :shirt: Enable rule --- packages/@n8n_io/eslint-config/base.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/@n8n_io/eslint-config/base.js b/packages/@n8n_io/eslint-config/base.js index 25543c4ff7f41..e1fec4db291d7 100644 --- a/packages/@n8n_io/eslint-config/base.js +++ b/packages/@n8n_io/eslint-config/base.js @@ -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 // ****************************************************************** From d87c8586578a1db0854596d8da26b01090bf7506 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Thu, 29 Dec 2022 13:04:27 +0100 Subject: [PATCH 3/4] :zap: Run rule (no issues) and add exception --- .../src/components/CodeNodeEditor/completions/js.snippets.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/editor-ui/src/components/CodeNodeEditor/completions/js.snippets.ts b/packages/editor-ui/src/components/CodeNodeEditor/completions/js.snippets.ts index b242ad64d6b01..ff9a42a826ebb 100644 --- a/packages/editor-ui/src/components/CodeNodeEditor/completions/js.snippets.ts +++ b/packages/editor-ui/src/components/CodeNodeEditor/completions/js.snippets.ts @@ -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' }), From e84eca0ac06653464d1af00e4f30cafe7e8c5a60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Thu, 29 Dec 2022 13:12:47 +0100 Subject: [PATCH 4/4] :zap: Simplify regex To account for expressions and to make it less expensive --- packages/@n8n_io/eslint-config/local-rules.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@n8n_io/eslint-config/local-rules.js b/packages/@n8n_io/eslint-config/local-rules.js index 6fd1d478531d1..9b4677c1c0a33 100644 --- a/packages/@n8n_io/eslint-config/local-rules.js +++ b/packages/@n8n_io/eslint-config/local-rules.js @@ -158,7 +158,7 @@ module.exports = { Literal(node) { if (typeof node.value !== 'string') return; - if (/\$\{[a-zA-Z0-9_$]*\}/.test(node.value)) { + if (/\$\{/.test(node.value)) { context.report({ messageId: 'useBackticks', node,