From 0b47f9ce4eafb0cddbad1bfd9d9ed6ae939d132f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Thu, 29 Dec 2022 14:24:19 +0100 Subject: [PATCH] refactor: Lint for no interpolation in regular string (#5060) (no-changelog) * :sparkles: Create rule `no-interpolation-in-regular-string` * :shirt: Enable rule * :zap: Run rule (no issues) and add exception * :zap: Simplify regex To account for expressions and to make it less expensive --- packages/@n8n_io/eslint-config/base.js | 2 ++ packages/@n8n_io/eslint-config/local-rules.js | 30 +++++++++++++++++++ .../CodeNodeEditor/completions/js.snippets.ts | 1 + 3 files changed, 33 insertions(+) diff --git a/packages/@n8n_io/eslint-config/base.js b/packages/@n8n_io/eslint-config/base.js index 25543c4ff7..e1fec4db29 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 // ****************************************************************** diff --git a/packages/@n8n_io/eslint-config/local-rules.js b/packages/@n8n_io/eslint-config/local-rules.js index c7a6968fe3..9b4677c1c0 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 (/\$\{/.test(node.value)) { + context.report({ + messageId: 'useBackticks', + node, + fix: (fixer) => fixer.replaceText(node, `\`${node.value}\``), + }); + } + }, + }; + }, + }, }; const isJsonParseCall = (node) => 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 b242ad64d6..ff9a42a826 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' }),