From d65935323fef31229afc9d3ebfb2aacae79e6b0f Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Sun, 19 Nov 2023 11:36:50 +0100 Subject: [PATCH] add eslint rule to ensure exported const are prefixed with UMB_ --- src/Umbraco.Web.UI.Client/.eslintrc.json | 1 + .../eslint/rules/prefix-exported-const.cjs | 27 +++++++++++++++++++ .../eslint-local-rules.cjs | 1 + 3 files changed, 29 insertions(+) create mode 100644 src/Umbraco.Web.UI.Client/devops/eslint/rules/prefix-exported-const.cjs diff --git a/src/Umbraco.Web.UI.Client/.eslintrc.json b/src/Umbraco.Web.UI.Client/.eslintrc.json index 6e166bb9c9..e9af81e6a4 100644 --- a/src/Umbraco.Web.UI.Client/.eslintrc.json +++ b/src/Umbraco.Web.UI.Client/.eslintrc.json @@ -51,6 +51,7 @@ "exceptions": ["@umbraco-cms", "@open-wc/testing", "@storybook", "msw", "."] } ], + "local-rules/prefix-exported-constants": "error", "@typescript-eslint/no-non-null-assertion": "off", "@typescript-eslint/no-explicit-any": "warn", "@typescript-eslint/no-unused-vars": "warn" diff --git a/src/Umbraco.Web.UI.Client/devops/eslint/rules/prefix-exported-const.cjs b/src/Umbraco.Web.UI.Client/devops/eslint/rules/prefix-exported-const.cjs new file mode 100644 index 0000000000..7bc19fa3f9 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/devops/eslint/rules/prefix-exported-const.cjs @@ -0,0 +1,27 @@ +module.exports = { + meta: { + type: 'problem', + docs: { + description: 'Ensure all exported consts are prefixed with UMB_', + }, + }, + create: function (context) { + return { + ExportNamedDeclaration(node) { + if (node.declaration && node.declaration.type === 'VariableDeclaration') { + const declaration = node.declaration.declarations[0]; + const { id, init } = declaration; + + if (id && id.type === 'Identifier' && init && init.type === 'Literal' && typeof init.value === 'string') { + if (!id.name.startsWith('UMB_')) { + context.report({ + node: id, + message: 'Exported constant should be prefixed with UMB_', + }); + } + } + } + }, + }; + }, +}; diff --git a/src/Umbraco.Web.UI.Client/eslint-local-rules.cjs b/src/Umbraco.Web.UI.Client/eslint-local-rules.cjs index ba1259b8cd..f092b410a9 100644 --- a/src/Umbraco.Web.UI.Client/eslint-local-rules.cjs +++ b/src/Umbraco.Web.UI.Client/eslint-local-rules.cjs @@ -18,4 +18,5 @@ module.exports = { 'prefer-import-aliases': preferImportAliasesRule, 'prefer-static-styles-last': preferStaticStylesLastRule, 'umb-class-prefix': umbClassPrefixRule, + 'prefix-exported-constants': require('./devops/eslint/rules/prefix-exported-const.cjs'), };