Files
Umbraco-CMS/src/Umbraco.Web.UI.Client/devops/eslint/rules/exported-string-constant-naming.cjs

40 lines
1.2 KiB
JavaScript
Raw Normal View History

module.exports = {
meta: {
type: 'problem',
docs: {
2023-11-19 12:03:19 +01:00
description:
2023-11-19 12:51:53 +01:00
'Ensure all exported string constants should be in uppercase with words separated by underscores and prefixed with UMB_',
},
},
create: function (context) {
2023-11-19 12:03:19 +01:00
const excludedFileNames = context.options[0]?.excludedFileNames || [];
return {
ExportNamedDeclaration(node) {
2023-11-19 12:03:19 +01:00
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') {
2023-11-19 11:51:34 +01:00
const isValidName = /^[A-Z]+(_[A-Z]+)*$/.test(id.name);
if (!isValidName || !id.name.startsWith('UMB_')) {
context.report({
node: id,
2023-11-19 11:51:34 +01:00
message:
2023-11-19 12:51:53 +01:00
'Exported string constant should be in uppercase with words separated by underscores and prefixed with UMB_',
});
}
}
}
},
};
},
};