Added sample cds rule

This commit is contained in:
Mara Jochum
2022-09-15 16:54:56 +02:00
parent 630838a7b4
commit 4fa96203cb
6 changed files with 101 additions and 26 deletions

15
.eslint/index.js Normal file
View File

@@ -0,0 +1,15 @@
const cds = require("@sap/eslint-plugin-cds");
module.exports = {
configs: {
recommended: {
plugins: ["cloud-cap-samples"],
rules: {
"cloud-cap-samples/no-open-services": ["error", "show"]
}
}
},
rules: {
"no-open-services": cds.createRule(require("./rules/no-open-services")),
}
};

4
.eslint/package.json Normal file
View File

@@ -0,0 +1,4 @@
{
"name": "eslint-plugin-cloud-cap-samples",
"version": "1.0.0"
}

View File

@@ -0,0 +1,38 @@
module.exports = {
meta: {
docs: {
description: "Service without `@requires/restrict` should not expose fields `createdBy` and `modifiedBy`.",
version: "1.0.0"
},
fixable: "code",
model: "inferred"
},
create: function (context) {
return { entity: checkForExposedFields };
function checkForExposedFields(e) {
const services = context.getModel().services;
const unauthorizedServices = services
.map((s) => {
if (!s["@requires"] && !s["@restrict"]) {
return s.name;
}
})
.filter((item) => !!item);
const found = Object.keys(e.elements).find((r) => ["createdBy", "modifiedBy"].indexOf(r) >= 0);
const isUnauthorizedService = unauthorizedServices.some((r) => {
if (e.name.includes(r)) {
return true;
}
return false;
});
if (found && isUnauthorizedService) {
context.report({
message: `Danger - exposed field '${found}' with '${e.name}' Either remove these or add add \`@restrict/requires\`.`,
node: context.getNode(e),
file: e.$location.file
});
}
}
}
};