@sap/eslint-plugin-cds
Version:
ESLint plugin including recommended SAP Cloud Application Programming model and environment rules
68 lines (60 loc) • 1.94 kB
JavaScript
const { RULE_CATEGORIES } = require('../constants')
module.exports = {
meta: {
schema: [{/* to avoid deprecation warning for ESLint 9 */}],
docs: {
category: RULE_CATEGORIES.model,
description: 'Should make suggestions for possible missing SQL casts.',
recommended: true,
url: 'https://cap.cloud.sap/docs/tools/cds-lint/rules/sql-cast-suggestion',
},
type: 'suggestion',
messages: {
missingSqlCast: 'Potential issue - Missing SQL cast for column expression?'
},
model: 'parsed',
},
create: function (context) {
const model = context.getModel()
if (!model?.definitions)
return
return function checkAllElementsStartWithLowercase() {
for (const defName in model.definitions) {
const def = model.definitions[defName]
checkSqlCastsInView(defName, def)
}
}
function checkSqlCastsInView(defName, def) {
// TODO: restructure and make more robust (#507)
if (!def?.query?.SET?.args?.length)
return
for (const arg of def.query.SET.args) {
if (arg?.SELECT?.columns?.length) {
// Only in UNION cases?
for (const col of arg.SELECT.columns) {
if (col)
checkColumn(col)
}
}
}
function checkColumn(col) {
const { xpr, cast } = col
if (cast && xpr) {
if (!(xpr[0]?.xpr && xpr[0]?.cast)) {
// we don't pass a name for the column's location, as it would be used to calculate
// endColumn, which is not correct for this expression
const loc = col.$location ?
context.getLocation('', col, model) :
context.getLocation(defName, def, model)
context.report({
messageId: 'missingSqlCast',
loc,
file: def.$location.file,
})
}
}
}
}
}
}