@sap/eslint-plugin-cds
Version:
ESLint plugin including recommended SAP Cloud Application Programming model and environment rules
57 lines (52 loc) • 1.56 kB
JavaScript
const allowList = new Set([
// exception: not part of the facade and should be available to users this way
'@sap/cds/eslint.config.mjs'
])
/**
* @param {string} importPath
* @param {RuleContext} context
* @param {Node} node
*/
function checkImport (importPath, context, node) {
if (!importPath.startsWith('@sap/cds/')) return
if (allowList.has(importPath)) return
context.report({
node,
messageId: 'noDeepSapCdsImports',
data: { import: importPath }
})
}
module.exports = {
meta: {
type: 'problem',
docs: {
recommended: true,
category: 'JavaScript Validation',
description: 'Warn about deep imports from @sap/cds.'
},
schema: [],
messages: {
noDeepSapCdsImports: `"{{import}}" is a deep import. The API of @sap/cds is available only from its facade via 'require("@sap/cds")' or 'import ... from "@sap/cds"'.`,
},
hasSuggestions: false
},
create: context => ({
CallExpression(node) {
// look for: require('@sap/cds/...')
if (node.callee.type !== 'Identifier') return
if (node.callee.name !== 'require') return
if (node.arguments.length !== 1) return
if (node.arguments[0].type !== 'Literal') return
checkImport(node.arguments[0].value, context, node)
},
ImportDeclaration(node) {
// import ... from '@sap/cds/...'
checkImport(node.source.value, context, node)
},
ImportExpression(node) {
// await import('@sap/cds/...')
checkImport(node.source.value, context, node)
}
})
}