@sap/eslint-plugin-cds
Version:
ESLint plugin including recommended SAP Cloud Application Programming model and environment rules
68 lines (58 loc) • 1.97 kB
JavaScript
const { RULE_CATEGORIES } = require('../constants')
const allowedUpperCaseElements = ['ID']
module.exports = {
meta: {
schema: [{/* to avoid deprecation warning for ESLint 9 */}],
docs: {
category: RULE_CATEGORIES.model,
description: 'Regular element names should start with lowercase letters.',
url: 'https://cap.cloud.sap/docs/tools/cds-lint/rules/start-elements-lowercase',
},
type: 'suggestion',
messages: {
startLowercase: "Element name '{{defName}}:{{elementName}}' should start with a lowercase letter.",
},
fixable: 'code',
model: 'parsed',
},
create: function (context) {
const model = context.getModel()
if (!model?.definitions)
return
return function checkAllElementsStartWithLowercase() {
for (const defName in model.definitions)
checkDefinition(defName, model.definitions[defName])
}
function checkDefinition(defName, def) {
if (defName.startsWith('localized') || defName.endsWith('texts'))
return
checkElements(def)
function checkElements(art) {
if (art.elements) {
for (const elementName in art.elements) {
const element = art.elements[elementName]
checkStartLowercase(element, elementName)
checkElements(element)
}
}
}
function checkStartLowercase (element, elementName) {
if (!element.$location?.file)
return // without location, we can't report anything properly
if (elementName.charAt(0) !== elementName.charAt(0).toLowerCase()
&& !allowedUpperCaseElements.includes(elementName)) {
context.report({
messageId: 'startLowercase',
loc: context.getLocation(elementName, element),
file: element.$location.file,
data: {
defName,
elementName
}
})
}
}
}
}
}