@sap/eslint-plugin-cds 2.3.2 → 2.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/CHANGELOG.md +25 -50
  2. package/lib/api/index.js +11 -13
  3. package/lib/api/lint.d.ts +48 -0
  4. package/lib/constants.js +54 -0
  5. package/lib/index.js +44 -0
  6. package/lib/{impl/parser.js → parser.js} +2 -13
  7. package/lib/processor.js +47 -0
  8. package/lib/{impl/rules → rules}/assoc2many-ambiguous-key.js +50 -53
  9. package/lib/rules/latest-cds-version.js +42 -0
  10. package/lib/rules/min-node-version.js +47 -0
  11. package/lib/rules/no-db-keywords.js +46 -0
  12. package/lib/rules/no-dollar-prefixed-names.js +49 -0
  13. package/lib/{impl/rules → rules}/no-join-on-draft-enabled-entities.js +14 -11
  14. package/lib/rules/require-2many-oncond.js +27 -0
  15. package/lib/rules/sql-cast-suggestion.js +52 -0
  16. package/lib/rules/start-elements-lowercase.js +61 -0
  17. package/lib/rules/start-entities-uppercase.js +55 -0
  18. package/lib/{impl/rules → rules}/valid-csv-header.js +17 -9
  19. package/lib/{impl/utils → utils}/fuzzySearch.js +0 -0
  20. package/lib/utils/helpers.js +47 -0
  21. package/lib/{impl/utils → utils}/jsonc.js +0 -0
  22. package/lib/utils/model.js +394 -0
  23. package/lib/utils/ruleHelpers.js +56 -0
  24. package/lib/utils/ruleTester.js +79 -0
  25. package/lib/utils/rules.js +979 -0
  26. package/lib/{impl/utils → utils}/validate.js +2 -18
  27. package/package.json +2 -2
  28. package/lib/impl/constants.js +0 -30
  29. package/lib/impl/index.js +0 -63
  30. package/lib/impl/processor.js +0 -23
  31. package/lib/impl/ruleFactory.js +0 -360
  32. package/lib/impl/rules/cds-compile-error.js +0 -34
  33. package/lib/impl/rules/latest-cds-version.js +0 -51
  34. package/lib/impl/rules/min-node-version.js +0 -44
  35. package/lib/impl/rules/no-db-keywords.js +0 -38
  36. package/lib/impl/rules/require-2many-oncond.js +0 -31
  37. package/lib/impl/rules/rule.hbs +0 -20
  38. package/lib/impl/rules/sql-cast-suggestion.js +0 -52
  39. package/lib/impl/rules/start-elements-lowercase.js +0 -75
  40. package/lib/impl/rules/start-entities-uppercase.js +0 -65
  41. package/lib/impl/types.d.ts +0 -48
  42. package/lib/impl/utils/helpers.js +0 -68
  43. package/lib/impl/utils/model.js +0 -548
  44. package/lib/impl/utils/rules.js +0 -697
@@ -1,31 +0,0 @@
1
- module.exports = require("../../api").createRule({
2
- meta: {
3
- docs: {
4
- description: `Foreign key information of a \`TO MANY\` relationship must be defined within the target and specified in an \`ON\` condition.`,
5
- category: "Model Validation",
6
- recommended: true,
7
- version: "2.1.0",
8
- },
9
- severity: "error",
10
- type: "problem",
11
- },
12
- create: function (context) {
13
- const m = context.cds.model; if (!m) return
14
- m.forall((d) => {
15
- if (d.name) {
16
- if (!d.elements) return;
17
- for (const elementName in d.elements) {
18
- const element = d.elements[elementName];
19
- if (element.is2many && !element.on && typeof element.target === 'string') {
20
- const loc = context.cds.getLocation(elementName, element);
21
- context.report({
22
- message: `You must provide an \`ON\` condition for \`TO MANY\` relationship '${element.name}'.`,
23
- loc,
24
- file: d.$location.file,
25
- });
26
- }
27
- }
28
- }
29
- });
30
- },
31
- });
@@ -1,20 +0,0 @@
1
- // @ts-check
2
- module.exports = require("../../api").createRule({
3
- meta: {
4
- docs: {
5
- description: "{{description}}",
6
- version: "{{version}}"
7
- },
8
- type:"{{type}}",
9
- },
10
- create: function(context) {
11
- const m = cotext.cds.model;
12
- m.forall((d)) => {
13
- // Add cds logic here, for example
14
- return [{
15
- message: "{{messages}}",
16
- loc: {{loc}}
17
- }];
18
- }
19
- }
20
- })
@@ -1,52 +0,0 @@
1
- module.exports = require("../../api").createRule({
2
- meta: {
3
- docs: {
4
- description: "Should make suggestions for possible missing SQL casts.",
5
- category: "Model Validation",
6
- recommended: true,
7
- version: "1.0.8",
8
- },
9
- severity: "warn",
10
- type: "suggestion",
11
- hasSuggestions: true,
12
- messages: {
13
- missingSQLCast:
14
- "Potential issue - Missing SQL cast for column expression?",
15
- },
16
- },
17
- create: function (context) {
18
- const m = context.cds.model;
19
- if (m) {
20
- const view = (d) => d.query;
21
- m.foreach(view, (v) => {
22
- if (v.query.SET)
23
- for (const { SELECT } of v.query.SET.args) {
24
- // Only in UNION cases?
25
- for (const each of SELECT.columns || []) {
26
- const { xpr, cast, $location: location } = each;
27
- if (cast && xpr) {
28
- if (xpr[0].xpr && xpr[0].xpr && xpr[0].cast) {
29
- continue;
30
- } else {
31
- if (context.sourcecode.lines[location.line - 1]) {
32
- const endCol =
33
- context.sourcecode.lines[location.line - 1].length;
34
- const loc = {
35
- start: { line: location.line, column: location.col - 1 },
36
- end: { line: location.line, column: endCol },
37
- };
38
- context.report({
39
- messageId: "missingSQLCast",
40
- loc,
41
- file: location.file,
42
- });
43
- }
44
- }
45
- }
46
- }
47
- }
48
- });
49
- }
50
- return context.report;
51
- },
52
- });
@@ -1,75 +0,0 @@
1
- module.exports = require("../../api").createRule({
2
- meta: {
3
- docs: {
4
- description: "Regular element names should start with lowercase letters.",
5
- category: "Model Validation",
6
- version: "1.0.4",
7
- },
8
- type: "suggestion",
9
- hasSuggestions: true,
10
- messages: {
11
- startLowercase:
12
- "Element name '{{entityName}}.{{elementName}}' should start with a lowercase letter.",
13
- fixLowercase:
14
- "Start element name with a lowercase letter."
15
- },
16
- fixable: "code",
17
- },
18
- create: function (context) {
19
- const m = context.cds.model;
20
- if (m && m.definitions) {
21
- m.forall((d) => {
22
- const entityName = d.name;
23
- for (const elementName in d.elements) {
24
- const element = d.elements[elementName];
25
- if (
26
- elementName &&
27
- !(
28
- entityName.startsWith("localized") || entityName.endsWith("texts")
29
- )
30
- ) {
31
- if (
32
- elementName.charAt(0) !== elementName.charAt(0).toLowerCase() &&
33
- !["ID"].includes(elementName)
34
- ) {
35
- if (element.$location && element.$location.file) {
36
- const file = element.$location.file;
37
- const loc = context.cds.getLocation(elementName, element);
38
- const fix = (fixer) => {
39
- const elementNameSanitized =
40
- elementName.charAt(0).toLowerCase() + elementName.slice(1);
41
- const rangeEnd = context.sourcecode.getIndexFromLoc({
42
- line: loc.end.line,
43
- column: loc.end.column,
44
- });
45
- const rangeBeg = rangeEnd
46
- ? rangeEnd - elementNameSanitized.length
47
- : 0;
48
- return fixer.replaceTextRange(
49
- [rangeBeg, rangeEnd],
50
- elementNameSanitized
51
- );
52
- };
53
- context.report({
54
- messageId: "startLowercase",
55
- loc,
56
- file,
57
- data: {
58
- entityName,
59
- elementName,
60
- },
61
- suggest: [
62
- {
63
- messageId: "fixLowercase",
64
- fix,
65
- },
66
- ],
67
- });
68
- }
69
- }
70
- }
71
- }
72
- });
73
- }
74
- },
75
- });
@@ -1,65 +0,0 @@
1
- module.exports = require("../../api").createRule({
2
- meta: {
3
- docs: {
4
- description: "Regular entity names should start with uppercase letters.",
5
- category: "Model Validation",
6
- version: "1.0.4",
7
- },
8
- type: "suggestion",
9
- hasSuggestions: true,
10
- messages: {
11
- startUppercase:
12
- "Entity name '{{entityName}}' should start with an uppercase letter.",
13
- fixUppercase: "Start entity name with an uppercase letter.",
14
- },
15
- fixable: "code",
16
- },
17
- create: function (context) {
18
- const m = context.cds.model;
19
- if (m) {
20
- m.foreach("entity", (e) => {
21
- let entityName = e.name;
22
- const names = entityName.split(".");
23
- entityName = names[names.length - 1];
24
- if (
25
- entityName &&
26
- !(entityName.startsWith("localized") || entityName.endsWith("texts"))
27
- ) {
28
- if (entityName.charAt(0) !== entityName.charAt(0).toUpperCase()) {
29
- if (e.$location && e.$location.file) {
30
- const file = e.$location.file;
31
- const loc = context.cds.getLocation(entityName, e);
32
- const fix = (fixer) => {
33
- const entityNameSanitized =
34
- entityName.charAt(0).toUpperCase() + entityName.slice(1);
35
- const rangeEnd = context.sourcecode.getIndexFromLoc({
36
- line: loc.end.line,
37
- column: loc.end.column,
38
- });
39
- const rangeBeg = rangeEnd
40
- ? rangeEnd - entityNameSanitized.length
41
- : 0;
42
- return fixer.replaceTextRange(
43
- [rangeBeg, rangeEnd],
44
- entityNameSanitized
45
- );
46
- };
47
- context.report({
48
- messageId: "startUppercase",
49
- loc,
50
- file,
51
- data: { entityName },
52
- suggest: [
53
- {
54
- messageId: "fixUppercase",
55
- fix,
56
- },
57
- ],
58
- });
59
- }
60
- }
61
- }
62
- });
63
- }
64
- },
65
- });
@@ -1,48 +0,0 @@
1
- import { Rule, RuleTester, SourceCode } from "eslint";
2
-
3
- export interface CDSRuleMetaData extends Rule.RuleMetaData {
4
- docs: {
5
- /** provides the short description of the rule in the [rules index](https://eslint.org/docs/rules/) */
6
- description: Rule.RuleMetaData['docs']['description'];
7
- /** specifies version of @sap/eslint-plugin-cds at which the rule was first implemented */
8
- version: string;
9
- };
10
- messages?: Rule.RuleMetaData['messages'];
11
- fixable?: Rule.RuleMetaData['fixable'];
12
- schema?: Rule.RuleMetaData['schema'];
13
- deprecated?: Rule.RuleMetaData['deprecated'];
14
- type?: Rule.RuleMetaData['type'];
15
- }
16
-
17
- export type CDSReport = Rule.ReportDescriptor & { file?: string };
18
-
19
- export interface CDSRuleContext extends Rule.RuleContext {
20
- cds: any;
21
- configPath: string;
22
- code: string;
23
- filePath: string;
24
- options: [];
25
- ruleID: string;
26
- sourcecode: SourceCode
27
- }
28
- export interface CDSRuleSpec {
29
- meta: CDSRuleMetaData,
30
- create: (arg0: CDSRuleContext) => Rule.ReportDescriptor;
31
- }
32
-
33
- export interface CDSTestCaseError extends RuleTester.TestCaseError {
34
- message: string | RegExp;
35
- }
36
-
37
- export interface CDSRuleTestOpts {
38
- /** specifies __dirname */
39
- root: string;
40
- /** requires your rule .js here */
41
- rule?: string;
42
- /** filename ('schema.cds' for model, 'package.json' for env) */
43
- filename: string;
44
- /** resolves cds parser path */
45
- parser?: string;
46
- /** List of warnings/errors from ESLint's [ruleTester](https://eslint.org/docs/developer-guide/nodejs-api#ruletester) */
47
- errors: CDSTestCaseError[]
48
- }
@@ -1,68 +0,0 @@
1
- const { files, envFiles, modelFiles } = require("../constants");
2
-
3
- module.exports = {
4
-
5
- /**
6
- * Checks whether the given file path contains a file extension allowed by
7
- * the plugin
8
- * @param {string} filePath
9
- * @returns boolean
10
- */
11
- isValidFile: function (filePath, key = "") {
12
- function genRegex(key) {
13
- return new RegExp(
14
- `${key
15
- .map((file) => {
16
- return file.replace("*", "");
17
- })
18
- .join("$|")}$`
19
- );
20
- }
21
- if (key === "model") {
22
- return genRegex(modelFiles).test(filePath);
23
- } else if (key === "env") {
24
- return genRegex(envFiles).test(filePath);
25
- } else {
26
- return genRegex(files).test(filePath);
27
- }
28
- },
29
-
30
- /**
31
- * Checks whether the plugin is run via the VS Code ESLint extension (editor)
32
- * @returns boolean
33
- */
34
- isEditor() {
35
- return process.argv.join(" ").includes("dbaeumer.vscode-eslint");
36
- },
37
-
38
- /**
39
- * Returns an array of allowed file extensions
40
- * the plugin can parse of the form "*.ext"
41
- * @returns {ConfigOverrideFiles} Array of file extensions
42
- */
43
- getFileExtensions: function () {
44
- return files;
45
- },
46
-
47
- /**
48
- * Prints a formatted message string according to the styles provided
49
- * @param msg message to print
50
- * @param styles array of styles for apply
51
- * @returns
52
- */
53
- styleText: function (msg, styles) {
54
- const types = {
55
- reset: "\x1b[0m", // Default
56
- bold: "\x1b[1m", // Bold/Bright
57
- link: "\x1b[4m", // underline
58
- red: "\x1b[31m", // Foreground Red
59
- green: "\x1b[32m", // Foreground Green
60
- yellow: "\x1b[33m", // Foreground Yellow
61
- };
62
- let msgStyle = "";
63
- styles.forEach((style) => {
64
- msgStyle += types[style];
65
- });
66
- return `${msgStyle}${msg}${types.reset}`;
67
- },
68
- };