@sap/eslint-plugin-cds 2.1.1 → 2.3.2

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.
package/CHANGELOG.md CHANGED
@@ -6,15 +6,75 @@ This project adheres to [Semantic Versioning](http://semver.org/).
6
6
 
7
7
  The format is based on [Keep a Changelog](http://keepachangelog.com/).
8
8
 
9
+ ## [2.3.2] - 2022-01-24
10
+
11
+ ### Changed
12
+
13
+ - Rule `require-2many-oncond` now also detect navigations of aaspects for flavor 'parsed'
14
+ - Removed duplicates from rule results of category 'Environment'
15
+
16
+ ## [2.3.1] - 2021-12-10
17
+
18
+ ### Changed
19
+
20
+ - Removed custom formatter as it is no longer used by `cds lint`.
21
+ - Deduplicate lint results from from rules of category environment.
22
+ - Removed 'unpeploy.json' from files as no lint rule requires it
23
+ - Fixed redundant triggers of model recompilations for non-model files and model files without changes
24
+
25
+ ## [2.3.0] - 2021-12-03
26
+
27
+ ### Added
28
+
29
+ - Added new rule 'valid-csv-header'
30
+
31
+ ### Changed
32
+
33
+ - Fixed suggestion messages in editor option (and disabled auto-fix)
34
+ - Added rule properties 'docs.recommended', 'severity'
35
+
36
+ ## [2.2.2] - 2021-11-08
37
+
38
+ ### Added
39
+
40
+ - Added new rule 'no-join-on-draft-enabled-entities'
41
+
42
+ ### Changed
43
+
44
+ - Compile 'model' files based on CSN flavor 'inferred'
45
+ - Compile 'outsider' files based on CSN flavor 'parsed'
46
+
47
+ ## [2.2.1] - 2021-11-01
48
+
49
+ ### Changed
50
+
51
+ - Optimized model loading and fixed bug in loading of 'outsider' files
52
+
53
+ ## [2.2.0] - 2021-10-29
54
+
55
+ ### Added
56
+
57
+ - Added typings to javascript for all exposed apis
58
+
59
+ ### Changed
60
+
61
+ - Aligned rule creation and tester api with ESLint
62
+
63
+ ## [2.1.2] - 2021-10-05
64
+
65
+ ### Changed
66
+
67
+ - Allow not only *.js but also other file types (i.e. *.ts, etc) to bypass plugin rules
68
+
9
69
  ## [2.1.1] - 2021-10-04
10
70
 
11
- ## Changed
71
+ ### Changed
12
72
 
13
73
  - Added preprocessor to avoid (other plugins) parsing errors on cds files
14
74
 
15
75
  ## [2.1.0] - 2021-09-23
16
76
 
17
- ## Changed
77
+ ### Changed
18
78
 
19
79
  - Source code is now Javascript only
20
80
  - Rule API simplified to only include report and cds
package/README.md CHANGED
@@ -3,4 +3,4 @@
3
3
 
4
4
  The [ESLint](https://eslint.org) plugin includes a set of recommended [SAP Cloud Application Programming Model (CAP)](https://cap.cloud.sap) model and environment rules. The aim of CDS linting is to catch issues with CDS models and with the environment early. To use this module we recommend to install [@sap/cds-dk](https://www.npmjs.com/package/@sap/cds-dk) globally.
5
5
 
6
- See the [CDS Linting documentation](https://cap.cloud.sap/docs/get-started/tools/#cds-lint) for more details, or jump directly to a complete [list of rules](https://cap.cloud.sap/docs/get-started/tools/#cds-lint-rules). CAP provides a set of recommended rules. On top, you can create your own, application-specific rules.
6
+ See the [CDS Linting documentation](https://cap.cloud.sap/docs/tools/#cds-lint) for more details, or jump directly to a complete [list of rules](https://cap.cloud.sap/docs/tools/#cds-lint-rules). CAP provides a set of recommended rules. On top, you can create your own, application-specific rules.
package/lib/api/index.js CHANGED
@@ -1,12 +1,27 @@
1
1
  /**
2
2
  * Our custom ESLint plugin API should:
3
- * - Expose 'createRule', and 'runRuleTester' to users to support the addition of *custom* cds rules
4
- * - Expose 'getConfigPath' and 'getFileExtensions' for usage in 'cds lint' (@sap/cds-dk)
3
+ * - Expose 'createRule', 'defineRule' (experimental) and 'runRuleTester' to
4
+ * support the addition of *custom* CDS Lint rules
5
+ * - Expose 'getConfigPath', 'getFileExtensions', and 'genDocs' for usage in CDS Lint (@sap/cds-dk)
6
+ * - Expose 'parserPath' for CDS Lint rule unit tests with ESLint's ruleTester
5
7
  */
6
8
 
7
- const { defineRule, createRule, runRuleTester } = require("../impl/ruleFactory");
8
- const { getConfigPath } = require("../impl/utils/model");
9
- const { getFileExtensions } = require("../impl/utils/rules");
10
- const { genDocs } = require("../impl/utils/rules");
9
+ const {
10
+ createRule,
11
+ defineRule,
12
+ runRuleTester,
13
+ } = require("../impl/ruleFactory");
14
+ const { getConfigPath } = require("../impl/utils/model");
15
+ const { getFileExtensions } = require("../impl/utils/helpers");
16
+ const { genDocs } = require("../impl/utils/rules");
17
+ const parserPath = require.resolve("../impl/parser");
11
18
 
12
- module.exports = { defineRule, createRule, runRuleTester, getConfigPath, getFileExtensions, genDocs }
19
+ module.exports = {
20
+ createRule,
21
+ defineRule,
22
+ runRuleTester,
23
+ getConfigPath,
24
+ getFileExtensions,
25
+ genDocs,
26
+ parserPath,
27
+ };
@@ -2,12 +2,9 @@
2
2
  * This file contains all constants for:
3
3
  * - categories: The category labels we use to for model and environment rules
4
4
  * - customRulesDir: The custom rules directory name in the user's project home
5
- * which contains the subdirs docs, rules and tests
6
- * - recommended: The set of this plugin's recommended rules and their severities
7
- * - globals: Any globals which should be exposed to ESLint by this plugin
8
- * - overrides:
9
- * - files: Any additional file extensions which ESLint should lint
10
- * - parser: The custom parser require to lint the additional files types
5
+ * which contains the subdirs 'docs', 'rules' and 'tests'
6
+ * - globals: The globals which should be exposed to ESLint by this plugin
7
+ * - files: Any additional file extensions which ESLint should lint
11
8
  */
12
9
 
13
10
  module.exports = {
@@ -15,18 +12,7 @@ module.exports = {
15
12
  env: "Environment",
16
13
  model: "Model Validation",
17
14
  },
18
-
19
15
  customRulesDir: ".eslint",
20
-
21
- recommended: {
22
- "@sap/cds/assoc2many-ambiguous-key": 1,
23
- "@sap/cds/cds-compile-error": 2,
24
- "@sap/cds/min-node-version": 2,
25
- "@sap/cds/no-db-keywords": 2,
26
- "@sap/cds/require-2many-oncond": 2,
27
- "@sap/cds/sql-cast-suggestion": 1
28
- },
29
-
30
16
  globals: {
31
17
  SELECT: true,
32
18
  INSERT: true,
@@ -39,7 +25,6 @@ module.exports = {
39
25
  CXL: true,
40
26
  cds: true,
41
27
  },
42
-
43
- files: ["*.cds", "*.csn"],
44
-
28
+ files: ["*.cds", "*.csn", "*.csv"],
29
+ modelFiles: ["*.cds", "*.csn"],
45
30
  };
package/lib/impl/index.js CHANGED
@@ -1,46 +1,63 @@
1
1
  /**
2
2
  * Custom ESLint plugin:
3
3
  * https://eslint.org/docs/developer-guide/working-with-plugins
4
- * This file exposes our plugins ESLint config, which (according to ESLint guidelines) must:
5
- * - Expose any 'configs' for prescribed rule configuration bundles (i.e. "recommended")
6
- * See shareable configs: https://eslint.org/docs/developer-guide/shareable-configs
7
- * - Expose any 'globals' for use in ESLint
4
+ * This file exposes our plugins ESLint configuration, which must:
5
+ * - Expose any 'configs' for prescribed rule configuration bundles
6
+ * (i.e. "recommended"). See shareable configs:
7
+ * https://eslint.org/docs/developer-guide/shareable-configs
8
+ * - Expose any 'globals' for use in ESLint
8
9
  * - Expose any 'processors' for use in ESLint
9
10
  * - Expose any 'rules' for use in ESLint
10
- * We also use this file to initiate our cached 'cds', which:
11
- * - Store our models and envs (internally)
12
- * - Caches cds services api uses in custom lint rules (externally)
11
+ * We also initiate and cache the objects 'rulesInfo' and 'cds' for later use.
13
12
  */
14
13
 
15
- const { rules } = require("./rules");
14
+ const path = require("path");
16
15
  const processor = require("./processor");
17
16
  const plugin = require("../../package.json").name;
18
- const { files, globals, recommended } = require("./constants");
19
- const { Cache, getProxy, getLocation, getRange } = require("../impl/utils/model");
17
+
18
+ const { getRules } = require("../impl/utils/rules");
19
+ const {
20
+ Cache,
21
+ getCDSProxy,
22
+ getLocation,
23
+ getRange,
24
+ } = require("../impl/utils/model");
25
+ const { files, globals } = require("./constants");
20
26
 
21
27
  const cds = require("@sap/cds");
22
28
  cds.getLocation = getLocation;
23
29
  cds.getRange = getRange;
24
- Cache.set('cds', getProxy(cds));
30
+ Cache.set("cds", getCDSProxy(cds));
31
+
32
+ let rulesInfo;
33
+ if (!Cache.has("rulesInfo")) {
34
+ rulesInfo = getRules(path.join(__dirname, "rules"));
35
+ Cache.set("rulesInfo", rulesInfo);
36
+ } else {
37
+ rulesInfo = Cache.get("rulesInfo");
38
+ }
39
+ const recommended = Object.assign({},
40
+ ...Object.entries(rulesInfo.rules)
41
+ .filter(([k,v]) => (v.meta.docs.recommended))
42
+ .map(([k,v]) => ({ [`@sap/cds/${k}`]:v.meta.severity }))
43
+ );
25
44
 
26
45
  module.exports = {
27
46
  configs: {
28
47
  recommended: {
48
+ globals,
29
49
  plugins: [plugin],
30
50
  overrides: [
31
51
  {
32
52
  files: files,
33
- parser: "./parser",
34
- processor: "@sap/cds/cds"
53
+ processor: "@sap/cds/cds",
35
54
  },
36
55
  ],
37
56
  rules: recommended,
38
- }
57
+ },
39
58
  },
40
- globals,
41
59
  processors: {
42
- cds: processor
60
+ cds: processor,
43
61
  },
44
- parser: "./parser",
45
- rules
46
- }
62
+ rules: rulesInfo.rules,
63
+ };
@@ -2,12 +2,20 @@
2
2
  * Custom ESLint parser:
3
3
  * https://eslint.org/docs/developer-guide/working-with-custom-parsers
4
4
  * This file must:
5
- * - Expose 'parseForESLint' method on the parser which should return the AST, optional properties services, a scopeManager, and visitorKeys
5
+ * - Expose 'parseForESLint' method on the parser which should return the AST,
6
+ * optional properties services, a scopeManager, and visitorKeys
6
7
  * - Expose default method 'parse' which should return the AST
7
- * Both methods should take in the source code and an optional configuration (parserOptions)
8
+ * Both methods should take in the source code and an optional configuration
9
+ * (parserOptions). Note, that because we use a 'empty' preprocessor, the
10
+ * parser is only used by ESLint's ruleTester.
8
11
  */
9
12
 
10
- const { getAST, getProxy, getLocation, getRange } = require("./utils/model");
13
+ const {
14
+ getAST,
15
+ getCDSProxy,
16
+ getLocation,
17
+ getRange,
18
+ } = require("../impl/utils/model");
11
19
  const cds = require("@sap/cds");
12
20
 
13
21
  module.exports = {
@@ -21,7 +29,7 @@ module.exports = {
21
29
  return {
22
30
  ast: getAST(code),
23
31
  services: {
24
- cdsProxy: getProxy(cds),
32
+ cdsProxy: getCDSProxy(cds),
25
33
  },
26
34
  scopeManager: null,
27
35
  tokensAndComments: [],
@@ -1,12 +1,23 @@
1
- const path = require("path");
1
+ /**
2
+ * ESLint custom processor:
3
+ * https://eslint.org/docs/developer-guide/working-with-plugins#processors-in-plugins
4
+ * This processor is used to avoid parsing errors when this plugin is extended
5
+ * in ESLint alongside other plugins, such as prettier which then also try to
6
+ * read the new file types exposed via globs.
7
+ * Note, that because we cache the file contents and return empty files, the
8
+ * plugin's parser is not actually used and we must retrieve the file contents
9
+ * later on (ruleFactory).
10
+ */
11
+
2
12
  const { Cache } = require("./utils/model");
13
+ const { isValidFile } = require("./utils/helpers");
3
14
 
4
15
  module.exports = {
5
16
  preprocess: function (text, filename) {
6
- const fileExtension = `*${path.extname(filename)}`;
7
- if (["*.cds", "*.csn"].includes(fileExtension)) {
8
- Cache.set(`file:${filename}`, text)
17
+ if (isValidFile(filename)) {
18
+ Cache.set(`file:${filename}`, text);
9
19
  }
10
- return [{ text: '', filename }];
11
- }
20
+ return [{ text: "", filename }];
21
+ },
22
+ supportsAutofix: true,
12
23
  };