UNPKG

@sap/eslint-plugin-cds

Version:

ESLint plugin including recommended SAP Cloud Application Programming model and environment rules

74 lines (64 loc) 1.97 kB
'use strict' const cds = require('@sap/cds') module.exports = { meta: { schema: [{/* to avoid deprecation warning for ESLint 9 */}], docs: { description: '`@restrict.where` must have valid values.', category: 'Model Validation', recommended: true, url: 'https://cap.cloud.sap/docs/tools/cds-lint/rules/auth-valid-restrict-where', }, severity: 'error', messages: { compilationFailed: 'Invalid `where` expression, CDS compilation failed.', invalidType: 'Invalid `where` type. Must be a string or expression.', }, type: 'problem', model: 'inferred' }, create (context) { const model = context.getModel() if (!model) return return function checkRestrictWhere() { model.foreach('entity', checkEntityRestrict) } function checkEntityRestrict (def) { if (!Array.isArray(def['@restrict'])) return for (const entry of def['@restrict']) { if (entry?.where !== undefined) { // TODO: Check return value (where xpr) checkAndCompileWhereExpression(entry.where) } } function checkAndCompileWhereExpression(where) { if (where === null || Array.isArray(where) || (typeof where !== 'object' && typeof where !== 'string')) { context.report({ messageId: 'invalidType', node: context.getNode(def), file: def.$location.file, }) return null } try { const compileOptions = {} return typeof where === 'string' ? cds.parse.expr(where, compileOptions) : where } catch(e) { if (e.code !== 'ERR_CDS_COMPILATION_FAILURE') throw e context.report({ messageId: 'compilationFailed', node: context.getNode(def), file: def.$location.file, }) return null } } } } }