Skip to content

Commit 5a8bcd7

Browse files
daogradychgeorenejeglinsky
authored
Add description for js rules (#1946)
We introduced a new set of lint rules targeting JS implementation files, instead of just .cds files. This PR introduces automatic documentation extraction for these new rules, to be found in list form under `/tools/cds-lint/rules/` ![Screenshot 2025-07-02 at 12 39 12](https://github.com/user-attachments/assets/987bbfdc-063b-4130-ba66-239900cb008e) We do not yet introduce extended documentation per rule for these new rules, as I am not clear yet whether they are automatically generated or maintained manually. Co-authored-by: Christian Georgi <chgeo@users.noreply.github.com> Co-authored-by: René Jeglinsky <rene.jeglinsky@sap.com>
1 parent dca6701 commit 5a8bcd7

2 files changed

Lines changed: 46 additions & 17 deletions

File tree

tools/cds-lint/rules.data.ts

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,56 @@ import * as path from 'path'
33

44
export default {
55
async load() {
6-
const data: any = { "Model Validation": [], "Environment": [] };
6+
const data: any = { "Model Validation": [], "Environment": [], "Javascript": [] };
77
let plugin: any;
88
try {
99
// @ts-ignore
1010
plugin = (await import('@sap/eslint-plugin-cds')).default;
11-
} catch (e) {
11+
} catch {
1212
return data
1313
}
14-
const allRules = Object.keys(plugin.configs.all.rules).sort();
15-
16-
allRules.forEach((rule: string) => {
17-
rule = rule.replace('@sap/cds/', '');
18-
const description = md2Html(plugin.rules[rule]?.meta.docs.description);
19-
const ruleDocs = path.join(__dirname, `rules/${rule}.md`)
20-
const hasRuleDocs = fs.existsSync(ruleDocs)
21-
const url = hasRuleDocs ? `./${rule}` : null
22-
const isRecommended = plugin.rules[rule]?.meta.docs.recommended ? '✅' : '';
23-
const hasFix = plugin.rules[rule]?.meta.fixable ? '🔧' : '';
24-
const hasSuggestions = plugin.rules[rule]?.meta.hasSuggestions ? '💡' : '';
25-
const model = plugin.rules[rule]?.meta?.model === 'parsed' ? '👀' : '';
26-
const category = plugin.rules[rule]?.meta?.model === 'none' ? "Environment" : "Model Validation";
27-
data[category].push({ rule, description, url, isRecommended, hasFix, hasSuggestions, model })
28-
})
14+
const allCdsRules = Object.keys(plugin.configs.all.rules)
15+
.map((ruleName: string) => ruleName.replace('@sap/cds/', ''))
16+
.sort();
17+
18+
for (const cdsRuleName of allCdsRules) {
19+
const rule = plugin.rules[cdsRuleName]
20+
const model = rule.meta?.model
21+
const category = model === 'none' ? 'Environment' : 'Model Validation';
22+
23+
data[category].push({...ruleInfo(cdsRuleName, rule),
24+
model: model === 'parsed' ? '👀' : ''
25+
})
26+
}
27+
28+
const allJsRuleName = Object.keys(plugin.configs.js.all.rules)
29+
.map((ruleName: string) => ruleName.replace('@sap/cds/', ''))
30+
.sort();
31+
32+
for (const jsRuleName of allJsRuleName) {
33+
data.Javascript.push(ruleInfo(jsRuleName, plugin.rules[jsRuleName]))
34+
}
35+
2936
return data;
3037
}
3138

3239
}
3340

41+
function ruleInfo (name, rule) {
42+
const meta = rule.meta ?? {}
43+
const ruleDocs = path.join(__dirname, `rules/${name}.md`)
44+
const hasRuleDocs = fs.existsSync(ruleDocs)
45+
return {
46+
rule: name,
47+
description: md2Html(meta.docs.description),
48+
url: hasRuleDocs ? `./${name}` : null,
49+
isRecommended: meta.docs?.recommended ? '✅' : '',
50+
hasFix: meta.fixable ? '🔧' : '',
51+
hasSuggestions: meta.hasSuggestions ? '💡' : '',
52+
model: meta.model === 'parsed' ? '👀' : ''
53+
}
54+
}
55+
3456
function md2Html(string:string='') {
3557
return string
3658
// @ts-ignore

tools/cds-lint/rules/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,10 @@ These are only run via the command line and are not available in the editor as t
4242
pinpointed to any particular file.
4343

4444
<RulesRefTable category="Environment"/>
45+
46+
47+
## JavaScript
48+
49+
JavaScript rules are designed to catch bad practices and common mistakes when implementing custom JavaScript handlers in CDS projects. These rules help ensure code quality, maintainability, and adherence to best practices when extending CDS with JavaScript logic.
50+
51+
<RulesRefTable category="Javascript"/>

0 commit comments

Comments
 (0)