@sap/eslint-plugin-cds 2.3.0 → 2.3.4

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 (45) hide show
  1. package/CHANGELOG.md +44 -61
  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 +387 -0
  23. package/lib/utils/ruleHelpers.js +56 -0
  24. package/lib/utils/ruleTester.js +79 -0
  25. package/lib/utils/rules.js +973 -0
  26. package/lib/{impl/utils → utils}/validate.js +2 -18
  27. package/package.json +2 -2
  28. package/lib/api/formatter.js +0 -182
  29. package/lib/impl/constants.js +0 -30
  30. package/lib/impl/index.js +0 -63
  31. package/lib/impl/processor.js +0 -23
  32. package/lib/impl/ruleFactory.js +0 -341
  33. package/lib/impl/rules/cds-compile-error.js +0 -34
  34. package/lib/impl/rules/latest-cds-version.js +0 -51
  35. package/lib/impl/rules/min-node-version.js +0 -44
  36. package/lib/impl/rules/no-db-keywords.js +0 -38
  37. package/lib/impl/rules/require-2many-oncond.js +0 -31
  38. package/lib/impl/rules/rule.hbs +0 -20
  39. package/lib/impl/rules/sql-cast-suggestion.js +0 -52
  40. package/lib/impl/rules/start-elements-lowercase.js +0 -75
  41. package/lib/impl/rules/start-entities-uppercase.js +0 -65
  42. package/lib/impl/types.d.ts +0 -48
  43. package/lib/impl/utils/helpers.js +0 -68
  44. package/lib/impl/utils/model.js +0 -554
  45. package/lib/impl/utils/rules.js +0 -678
@@ -1,34 +0,0 @@
1
- module.exports = require("../../api").createRule({
2
- meta: {
3
- docs: {
4
- description: `Checks whether the CDS model file can be compiled by the @sap/cds wihtout errors.`,
5
- category: "Model Validation",
6
- version: "1.0.0",
7
- },
8
- severity: "error",
9
- type: "problem",
10
- },
11
- create: function (context) {
12
- const m = context.cds.model;
13
- if (m && m.err) {
14
- // If any csn compile errors occur
15
- m.err.messages.forEach((err) => {
16
- const msg = err.message;
17
- let file = "";
18
- const loc = {
19
- start: { line: 0, column: 0 },
20
- end: { line: 1, column: 0 },
21
- };
22
- // Get its location if it exists
23
- if (err.$location) {
24
- loc.start.column = err.$location.col;
25
- loc.start.line = err.$location.line;
26
- loc.end.column = err.$location.endCol;
27
- loc.end.line = err.$location.endLine;
28
- file = err.$location.file;
29
- }
30
- context.report({ message: `${msg}`, loc, file });
31
- });
32
- }
33
- }
34
- });
@@ -1,51 +0,0 @@
1
- const os = require("os");
2
- const cp = require("child_process");
3
- const semver = require("semver");
4
-
5
- const IS_WIN = os.platform() === "win32";
6
-
7
- module.exports = require("../../api").createRule({
8
- meta: {
9
- docs: {
10
- description: "Checks whether the latest `@sap/cds` version is being used.",
11
- category: "Environment",
12
- version: "1.0.4",
13
- },
14
- type: "suggestion",
15
- hasSuggestions: true,
16
- messages: {
17
- latestCDSVersion: `A newer CDS version is available!`,
18
- },
19
- },
20
- create: function (context) {
21
- let result;
22
- let cdsVersions;
23
- const e = context.cds.environment;
24
- if (!e) {
25
- try {
26
- result = cp
27
- .execSync(`npm outdated @sap/cds --json`, {
28
- cwd: process.cwd(),
29
- shell: IS_WIN,
30
- stdio: "pipe",
31
- })
32
- .toString();
33
- cdsVersions = JSON.parse(result)["@sap/cds"];
34
- } catch (err) {
35
- // Do not throw
36
- }
37
- } else {
38
- cdsVersions = context.cds.environment["@sap/cds"];
39
- }
40
- // If current cds version is not the latest
41
- if (
42
- Object.keys(cdsVersions).length !== 0 &&
43
- !semver.satisfies(cdsVersions.latest, cdsVersions.current)
44
- ) {
45
- // Add to ESLint report
46
- context.report({
47
- messageId: "latestCDSVersion",
48
- });
49
- }
50
- },
51
- });
@@ -1,44 +0,0 @@
1
- const path = require("path");
2
- const semver = require("semver");
3
-
4
- module.exports = require("../../api").createRule({
5
- meta: {
6
- docs: {
7
- description: `Checks whether the minimum Node.js version required by \`@sap/cds\` is achieved.`,
8
- category: "Environment",
9
- recommended: true,
10
- version: "1.0.0",
11
- },
12
- severity: "error",
13
- type: "problem",
14
- },
15
- create: function (context) {
16
- const e = context.cds.environment;
17
- let nodeVersion, nodeVersionCDS;
18
- if (!e) {
19
- // Get current and required node versions
20
- try {
21
- const CDSPath = require.resolve("@sap/cds/package.json", {
22
- paths: [path.dirname(context.filePath)],
23
- });
24
- const jsonCDS = require(CDSPath);
25
- nodeVersion = process.version;
26
- nodeVersionCDS = jsonCDS.engines.node;
27
- } catch (err) {
28
- // Do not throw
29
- }
30
- } else {
31
- nodeVersion = context.cds.environment.nodeVersion;
32
- nodeVersionCDS = context.cds.environment.nodeVersionCDS;
33
- }
34
- if (
35
- nodeVersion &&
36
- nodeVersionCDS &&
37
- !semver.satisfies(nodeVersion, nodeVersionCDS, { loose: true })
38
- ) {
39
- context.report({
40
- message: `CDS minimum node version of ${nodeVersionCDS} required, found ${nodeVersion}!`,
41
- });
42
- }
43
- },
44
- });
@@ -1,38 +0,0 @@
1
- module.exports = require("../../api").defineRule({
2
- meta: {
3
- docs: {
4
- description: `Avoid using reserved SQL keywords.`,
5
- category: "Model Validation",
6
- recommended: true,
7
- version: "2.1.0",
8
- },
9
- severity: "error"
10
- },
11
- create(context) {
12
- const { db = { kind: "sql" } } = context.cds.env.requires;
13
- function _check(d) {
14
- if (d.name in RESERVED) {
15
- // Do not blame in case of external services
16
- let srv = d._service || (d.parent && d.parent._service);
17
- if (srv && srv["@cds.external"]) return;
18
-
19
- // Do blame
20
- return `'${d.name}' is a reserved keyword in ${db.kind.toUpperCase()}`;
21
- }
22
- }
23
- return { entity: _check, element: _check };
24
- },
25
- });
26
-
27
- // REVISIT: Replace by compiler-provided check
28
- const RESERVED = {
29
- ORDER: 1,
30
- Order: 1,
31
- order: 1,
32
- GROUP: 1,
33
- Group: 1,
34
- group: 1,
35
- LIMIT: 1,
36
- Limit: 1,
37
- limit: 1,
38
- };
@@ -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) {
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
- };