Added sample cds rule
This commit is contained in:
15
.eslint/index.js
Normal file
15
.eslint/index.js
Normal 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
4
.eslint/package.json
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"name": "eslint-plugin-cloud-cap-samples",
|
||||||
|
"version": "1.0.0"
|
||||||
|
}
|
||||||
38
.eslint/rules/no-open-services.js
Normal file
38
.eslint/rules/no-open-services.js
Normal 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
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
53
.eslintrc
53
.eslintrc
@@ -1,25 +1,32 @@
|
|||||||
{
|
{
|
||||||
"extends": "eslint:recommended",
|
"extends": [
|
||||||
"env": {
|
"eslint:recommended",
|
||||||
"browser": true,
|
"plugin:@sap/cds/recommended",
|
||||||
"es2022": true,
|
"plugin:cloud-cap-samples/recommended"
|
||||||
"node": true,
|
],
|
||||||
"jest": true,
|
"plugins": [
|
||||||
"mocha": true
|
"cloud-cap-samples"
|
||||||
},
|
],
|
||||||
"globals": {
|
"env": {
|
||||||
"SELECT": true,
|
"browser": true,
|
||||||
"INSERT": true,
|
"es2022": true,
|
||||||
"UPDATE": true,
|
"node": true,
|
||||||
"DELETE": true,
|
"jest": true,
|
||||||
"CREATE": true,
|
"mocha": true
|
||||||
"DROP": true,
|
},
|
||||||
"cds": true
|
"globals": {
|
||||||
},
|
"SELECT": true,
|
||||||
"rules": {
|
"INSERT": true,
|
||||||
"no-console": "off",
|
"UPDATE": true,
|
||||||
"require-atomic-updates": "off",
|
"DELETE": true,
|
||||||
"require-await":"warn",
|
"CREATE": true,
|
||||||
"no-unused-vars": ["warn", { "argsIgnorePattern": "_" }]
|
"DROP": true,
|
||||||
}
|
"cds": true
|
||||||
|
},
|
||||||
|
"rules": {
|
||||||
|
"no-console": "off",
|
||||||
|
"require-atomic-updates": "off",
|
||||||
|
"require-await":"warn",
|
||||||
|
"no-unused-vars": ["warn", { "argsIgnorePattern": "_" }]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
10
.vscode/settings.json
vendored
10
.vscode/settings.json
vendored
@@ -14,5 +14,13 @@
|
|||||||
"**/odata-v4/okra/**"
|
"**/odata-v4/okra/**"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"mochaExplorer.parallel": true
|
"mochaExplorer.parallel": true,
|
||||||
|
"eslint.validate": [
|
||||||
|
"cds",
|
||||||
|
"csn",
|
||||||
|
"csv",
|
||||||
|
"csv (semicolon)",
|
||||||
|
"tsv",
|
||||||
|
"tab"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,9 @@
|
|||||||
"repository": "https://github.com/sap-samples/cloud-cap-samples.git",
|
"repository": "https://github.com/sap-samples/cloud-cap-samples.git",
|
||||||
"author": "daniel.hutzel@sap.com",
|
"author": "daniel.hutzel@sap.com",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sap/cds": ">=5.5.3"
|
"@sap/cds": ">=5.5.3",
|
||||||
|
"@sap/eslint-plugin-cds": "file:../../SAPDevelop/CAP/Tools/dev/cds-lint",
|
||||||
|
"eslint-plugin-cloud-cap-samples": "file:.eslint"
|
||||||
},
|
},
|
||||||
"workspaces": [
|
"workspaces": [
|
||||||
"./*/"
|
"./*/"
|
||||||
@@ -16,7 +18,8 @@
|
|||||||
"chai-as-promised": "^7.1.1",
|
"chai-as-promised": "^7.1.1",
|
||||||
"chai-subset": "^1.6.0",
|
"chai-subset": "^1.6.0",
|
||||||
"semver": "^7",
|
"semver": "^7",
|
||||||
"sqlite3": "^5"
|
"sqlite3": "^5",
|
||||||
|
"eslint": "^7"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"cleanup": "rm -rf node_modules && rm -rf */node_modules && rm -rf */*/node_modules",
|
"cleanup": "rm -rf node_modules && rm -rf */node_modules && rm -rf */*/node_modules",
|
||||||
|
|||||||
Reference in New Issue
Block a user