@sap/eslint-plugin-cds
Version:
ESLint plugin including recommended SAP Cloud Application Programming model and environment rules
74 lines (61 loc) • 1.57 kB
JavaScript
/*
Use cases not yet covered:
//---------
INLINE EXTENSION
class FooService extends require('@sap/cds').ApplicationService { ... }
//---------
REFERENCED FUNCTION
function bad() { ... }
class ... {
this.on('', bad)
}
//---------
METHOD
class ... {
bad () {}
this.on('', this.bad)
}
//---------
IMPORTED FUNCTION
const { bad } = require('./bad')
class ... {
this.on('', bad)
}
//---------
NON-CLASS-BASED CDS SERVICE
cds.services['myService'].on('READ', 'Books', () => {})
*/
'use strict'
const { RULE_CATEGORIES } = require('../../constants')
const { CdsHandlerRule } = require('./CdsHandlerRule')
class NoSharedVariable extends CdsHandlerRule {
AssignmentExpression(node) {
if (!this.isInsideCapHandlerRegistration) return
const declaringScope = this.findDefinitionScope(node.left.name)
if (declaringScope?.isLocal === false) {
this.context.report({
node,
messageId: 'noSharedHandlerVariable',
data: {
definitionScope: declaringScope.scope.name
}
})
}
}
}
module.exports = {
meta: {
type: 'problem',
docs: {
recommended: true,
category: RULE_CATEGORIES.javascript,
description: 'Enforce that variables can not be used to share state between handlers.'
},
schema: [],
messages: {
noSharedHandlerVariable: 'Assignment to a non-local variable inside a CDS event handler (was declared in scope "{{definitionScope}}").'
},
hasSuggestions: true
},
create: context => new NoSharedVariable(context).asESLintVisitor()
}