Files
Umbraco-CMS/src/Umbraco.Web.UI.Client/devops/eslint/rules/exported-string-constant-naming.cjs
Mads Rasmussen 3df85ff695 update rule name
2023-11-20 09:39:47 +01:00

40 lines
1.2 KiB
JavaScript

module.exports = {
meta: {
type: 'problem',
docs: {
description:
'Ensure all exported string constants should be in uppercase with words separated by underscores and prefixed with UMB_',
},
},
create: function (context) {
const excludedFileNames = context.options[0]?.excludedFileNames || [];
return {
ExportNamedDeclaration(node) {
const fileName = context.getFilename();
if (excludedFileNames.some((excludedFileName) => fileName.includes(excludedFileName))) {
// Skip the rule check for files in the excluded list
return;
}
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') {
const isValidName = /^[A-Z]+(_[A-Z]+)*$/.test(id.name);
if (!isValidName || !id.name.startsWith('UMB_')) {
context.report({
node: id,
message:
'Exported string constant should be in uppercase with words separated by underscores and prefixed with UMB_',
});
}
}
}
},
};
},
};