@sap/eslint-plugin-cds 2.1.0 → 2.3.0

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
@@ -7,6 +7,56 @@ This project adheres to [Semantic Versioning](http://semver.org/).
7
7
  The format is based on [Keep a Changelog](http://keepachangelog.com/).
8
8
 
9
9
 
10
+ ## [2.3.0] - 201-12-03
11
+
12
+ ## Added
13
+
14
+ - Added new rule 'valid-csv-header'
15
+
16
+ ## Changed
17
+
18
+ - Fixed suggestion messages in editor option (and disabled auto-fix)
19
+ - Added rule properties 'docs.recommended', 'severity'
20
+
21
+ ## [2.2.2] - 2021-11-08
22
+
23
+ ## Added
24
+
25
+ - Added new rule 'no-join-on-draft-enabled-entities'
26
+
27
+ ## Changed
28
+
29
+ - Compile 'model' files based on CSN flavor 'inferred'
30
+ - Compile 'outsider' files based on CSN flavor 'parsed'
31
+
32
+ ## [2.2.1] - 2021-11-01
33
+
34
+ ## Changed
35
+
36
+ - Optimized model loading and fixed bug in loading of 'outsider' files
37
+
38
+ ## [2.2.0] - 2021-10-29
39
+
40
+ ## Added
41
+
42
+ - Added typings to javascript for all exposed apis
43
+
44
+ ## Changed
45
+
46
+ - Aligned rule creation and tester api with ESLint
47
+
48
+ ## [2.1.2] - 2021-10-05
49
+
50
+ ## Changed
51
+
52
+ - Allow not only *.js but also other file types (i.e. *.ts, etc) to bypass plugin rules
53
+
54
+ ## [2.1.1] - 2021-10-04
55
+
56
+ ## Changed
57
+
58
+ - Added preprocessor to avoid (other plugins) parsing errors on cds files
59
+
10
60
  ## [2.1.0] - 2021-09-23
11
61
 
12
62
  ## Changed
@@ -211,4 +261,4 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
211
261
 
212
262
  ## [1.0.0] - 2020-12-07
213
263
 
214
- - Initial release 1.0.0
264
+ - Initial release 1.0.0
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.
@@ -6,200 +6,177 @@
6
6
  * - Separately from model errors
7
7
  * - Report them within a project scope but independent of a specific file within that scope
8
8
  */
9
- const fs = require("fs");
10
- const path = require("path");
11
- const { CLIEngine } = require("eslint")
12
- // Note: CliEngine depracted, but new Node API requires
13
- // async Linter framework
14
- const formatter = CLIEngine.getFormatter("stylish");
15
- const { Cache } = require("../impl/utils/model");
16
- const { styleText, isEditor, isTest } = require("../impl/utils/helpers");
9
+ const fs = require("fs");
10
+ const path = require("path");
11
+ const formatter = require("eslint/lib/cli-engine/formatters/stylish");
17
12
 
18
- const CONSTANTS = require("../impl/constants");
13
+ const { Cache } = require("../impl/utils/model");
14
+ const { styleText, isEditor } = require("../impl/utils/helpers");
19
15
 
20
- // eslint-disable-next-line no-control-regex
21
- const REGEX_STRIP_ANSI = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
22
- const REGEX_NEWLINE = /\r?\n/g;
16
+ const CONSTANTS = require("../impl/constants");
17
+ /* eslint-disable-next-line no-control-regex */
18
+ const REGEX_STRIP_ANSI = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
19
+ const REGEX_NEWLINE = /\r?\n/g;
23
20
 
24
- let projects = Cache.getModels();
21
+ let projects = Cache.get('configpaths') || [];
25
22
 
26
- // Types: results: ESLint.LintResult[], data: any
27
- module.exports = function (results, data) {
28
- let output = "";
29
- // Get plugin ruleIDs
30
- const rules = getPluginRules(data);
31
- // Get ruleIDs from custom rules
32
- const customRules = [];
33
- projects.forEach((project) => {
34
- const customRulesPath = path.resolve(project, CONSTANTS.customRulesDir, 'rules');
35
- if (fs.existsSync(customRulesPath)) {
36
- fs.readdirSync(customRulesPath).forEach((customRule) => {
37
- const ruleID = path.basename(customRule, '.js');
38
- if (!customRules.includes(ruleID)) {
39
- customRules.push(ruleID)
40
- }
41
- })
42
- }
43
- })
44
- if (!Cache.has("err")) {
45
- // Filter out error messages not from plugin or custom rules
46
- if (!isEditor() && !isTest()) {
47
- results.forEach((result) => {
48
- result.messages = result.messages.filter (msg => (
49
- rules.model.includes(msg.ruleId) ||
50
- rules.environment.includes(msg.ruleId) ||
51
- customRules.includes(msg.ruleId)
52
- ))
53
- // result.messages.forEach((msg) => {
54
- // if (!rules.model.includes(msg.ruleId) &&
55
- // !rules.environment.includes(msg.ruleId) &&
56
- // !customRules.includes(msg.ruleId)) {
57
- // if (!resultsToRemove.includes(i)) {
58
- // resultsToRemove.push(i);
59
- // }
60
- // }
61
- // })
62
- })
63
- // results.forEach((result, index) => {
64
- // if (resultsToRemove.includes(index)) {
65
- // results.splice(index, 1);
66
- // }
67
- // })
68
- }
69
- // Get standard ESLint 'stylish' output
70
- const outputStylish = formatter(results);
23
+ module.exports = function (results, data) {
24
+ let output = "";
25
+ // Get plugin ruleIDs
26
+ const rules = getPluginRules(data);
27
+ // Get ruleIDs from custom rules
28
+ const customRules = [];
29
+ projects.forEach((project) => {
30
+ const customRulesPath = path.resolve(project, CONSTANTS.customRulesDir, "rules");
31
+ if (fs.existsSync(customRulesPath)) {
32
+ fs.readdirSync(customRulesPath).forEach((customRule) => {
33
+ const ruleID = path.basename(customRule, ".js");
34
+ if (!customRules.includes(ruleID)) {
35
+ customRules.push(ruleID);
36
+ }
37
+ });
38
+ }
39
+ });
40
+ if (!Cache.has("err")) {
41
+ // Filter out error messages not from plugin or custom rules
42
+ if (!isEditor()) {
43
+ results.forEach((result) => {
44
+ result.messages = result.messages.filter(
45
+ (msg) =>
46
+ rules.model.includes(msg.ruleId) ||
47
+ rules.environment.includes(msg.ruleId) ||
48
+ customRules.includes(msg.ruleId)
49
+ );
50
+ });
51
+ }
52
+ // Get standard ESLint 'stylish' output
53
+ const outputStylish = formatter(results);
71
54
 
72
- // Split according to category
73
- const msgs = splitOutput(outputStylish, rules);
55
+ // Split according to category
56
+ const msgs = splitOutput(outputStylish, rules);
74
57
 
75
- // Format new CDSLint output
76
- output = formatOutput(output, msgs);
77
- }
78
- return output;
79
- };
58
+ // Format new CDSLint output
59
+ output = formatOutput(output, msgs);
60
+ }
61
+ return output;
62
+ };
80
63
 
81
- /**
82
- * Splits rules by config and category
83
- * @param data Rules meta data
84
- * @returns rules object based on category
85
- */
86
- function getPluginRules (data) {
87
- const rules = { environment: [], model: [], recommended: [] };
88
- Object.keys(data.rulesMeta).forEach((rule) => {
89
- if (data.rulesMeta[rule]) {
90
- const category = data.rulesMeta[rule].docs.category;
91
- if (category === CONSTANTS.categories.model) {
92
- rules.model.push(rule);
93
- rules.recommended.push(rule);
94
- } else if (category === CONSTANTS.categories.env) {
95
- rules.environment.push(rule);
96
- rules.recommended.push(rule);
97
- }
98
- }
99
- });
100
- return rules;
101
- }
64
+ /**
65
+ * Splits rules by config and category
66
+ * @param data Rules meta data
67
+ * @returns rules object based on category
68
+ */
69
+ function getPluginRules(data) {
70
+ const rules = { environment: [], model: [], recommended: [] };
71
+ Object.keys(data.rulesMeta).forEach((rule) => {
72
+ if (data.rulesMeta[rule]) {
73
+ const category = data.rulesMeta[rule].docs.category;
74
+ if (category === CONSTANTS.categories.model) {
75
+ rules.model.push(rule);
76
+ rules.recommended.push(rule);
77
+ } else if (category === CONSTANTS.categories.env) {
78
+ rules.environment.push(rule);
79
+ rules.recommended.push(rule);
80
+ }
81
+ }
82
+ });
83
+ return rules;
84
+ }
102
85
 
103
- /**
104
- * Obtains ESLint's standard (*stylish*) output, then splits and reccollects error messages
105
- * based on rule category and content type (msg vs. error count/report)
106
- * @param outputStylish ESLint's standard output
107
- * @param rules Plugin rules based on category
108
- * @returns Collected msgs
109
- */
110
- function splitOutput (outputStylish, rules) {
111
- const msgs = { environment: {}, model: {}, report: [] };
112
- let projects = Cache.getModels();
113
- let file = "";
114
- outputStylish.split(REGEX_NEWLINE).forEach((line) => {
115
- const lineStripped = line.replace(REGEX_STRIP_ANSI, "");
116
- // Collect model file (default)
117
- const lineStrippedSplit = lineStripped.split(" ");
118
- const rule = lineStrippedSplit[lineStrippedSplit.length - 1];
119
- if (
120
- !lineStripped.startsWith(" ") &&
121
- !rules.recommended.includes(rule) &&
122
- !lineStripped.startsWith("✖")
123
- ) {
124
- file = line;
125
- if (
126
- process.argv[1].includes("jest") ||
127
- process.argv[1].includes("mocha")
128
- ) {
129
- projects = [path.dirname(lineStripped)];
130
- }
131
- } else if (rules.environment.includes(rule)) {
132
- // Collect error/warning messages from @sap/cds/ rules
133
- const delimiter = line.split(/ +/, 2)[1];
134
- const lineWithoutLocation = line.split(delimiter)[1];
135
- let project = "";
136
- for (let i = 0; i < projects.length; i++) {
137
- project = projects[i];
138
- if (file.includes(project)) {
139
- project = styleText(project, ["link"]);
140
- break;
141
- }
142
- }
143
- if (project) {
144
- if (!Object.keys(msgs.environment).includes(project)) {
145
- msgs.environment[project] = [lineWithoutLocation];
146
- } else if (
147
- !msgs.environment[project].includes(lineWithoutLocation)
148
- ) {
149
- msgs.environment[project].push(lineWithoutLocation);
150
- }
151
- }
152
- } else if (
153
- lineStripped &&
154
- !rules.environment.includes(rule) &&
155
- !lineStripped.includes("potentially fixable") &&
156
- !lineStripped.startsWith("✖")
157
- ) {
158
- if (lineStripped) {
159
- if (!msgs.model[file]) {
160
- msgs.model[file] = [line];
161
- } else {
162
- msgs.model[file].push(line);
163
- }
164
- }
165
- } else if (
166
- lineStripped.startsWith("✖") ||
167
- lineStripped.includes("potentially fixable")
168
- ) {
169
- msgs.report.push(line);
170
- }
171
- });
172
- return msgs;
173
- }
86
+ /**
87
+ * Obtains ESLint's standard (*stylish*) output, then splits and reccollects error messages
88
+ * based on rule category and content type (msg vs. error count/report)
89
+ * @param outputStylish ESLint's standard output
90
+ * @param rules Plugin rules based on category
91
+ * @returns Collected msgs
92
+ */
93
+ function splitOutput(outputStylish, rules) {
94
+ const msgs = { environment: {}, model: {}, report: [] };
95
+ let file = "";
96
+ outputStylish.split(REGEX_NEWLINE).forEach((line) => {
97
+ const lineStripped = line.replace(REGEX_STRIP_ANSI, "");
98
+ // Collect model file (default)
99
+ const lineStrippedSplit = lineStripped.split(" ");
100
+ const rule = lineStrippedSplit[lineStrippedSplit.length - 1];
101
+ if (
102
+ !lineStripped.startsWith(" ") &&
103
+ !rules.recommended.includes(rule) &&
104
+ !lineStripped.startsWith("✖")
105
+ ) {
106
+ file = line;
107
+ if (process.argv[1].includes("jest") || process.argv[1].includes("mocha")) {
108
+ projects = [path.dirname(lineStripped)];
109
+ }
110
+ } else if (rules.environment.includes(rule)) {
111
+ // Collect error/warning messages from @sap/cds/ rules
112
+ const delimiter = line.split(/ +/, 2)[1];
113
+ const lineWithoutLocation = line.split(delimiter)[1];
114
+ let project = "";
115
+ for (let i = 0; i < projects.length; i++) {
116
+ project = projects[i];
117
+ if (file.includes(project)) {
118
+ project = styleText(project, ["link"]);
119
+ break;
120
+ }
121
+ }
122
+ if (project) {
123
+ if (!Object.keys(msgs.environment).includes(project)) {
124
+ msgs.environment[project] = [lineWithoutLocation];
125
+ } else if (!msgs.environment[project].includes(lineWithoutLocation)) {
126
+ msgs.environment[project].push(lineWithoutLocation);
127
+ }
128
+ }
129
+ } else if (
130
+ lineStripped &&
131
+ !rules.environment.includes(rule) &&
132
+ !lineStripped.includes("potentially fixable") &&
133
+ !lineStripped.startsWith("✖")
134
+ ) {
135
+ if (lineStripped) {
136
+ if (!msgs.model[file]) {
137
+ msgs.model[file] = [line];
138
+ } else {
139
+ msgs.model[file].push(line);
140
+ }
141
+ }
142
+ } else if (lineStripped.startsWith("✖") || lineStripped.includes("potentially fixable")) {
143
+ msgs.report.push(line);
144
+ }
145
+ });
146
+ return msgs;
147
+ }
174
148
 
175
- /**
176
- * Takes the collected error messages and collects them
177
- * in a final string for ESLint to output
178
- * @param output final string to output
179
- * @param msgs error messages based on category
180
- * @returns
181
- */
182
- function formatOutput (output, msgs) {
183
- // First, output model msgs per file (from ESLint 'stylish' output)
184
- for (const fileModel in msgs.model) {
185
- if (msgs.model[fileModel].length > 0) {
186
- output += `${fileModel}\n`;
187
- output += `${msgs.model[fileModel].join("\n")}\n\n`;
188
- }
189
- }
190
- // Next, output env msgs (file-independent but associated to project)
191
- for (const fileModel in msgs.environment) {
192
- if (msgs.environment[fileModel].length > 0) {
193
- output += `${fileModel}\n`;
194
- output += `${msgs.environment[fileModel].join("\n")}\n\n`;
195
- }
196
- }
197
- // Finally output error/warning report count
198
- if (output && msgs.report) {
199
- output += `${msgs.report.join("\n")}`;
200
- }
201
- if (output) {
202
- output = `\n${output}`;
203
- }
204
- return output;
205
- }
149
+ /**
150
+ * Takes the collected error messages and collects them
151
+ * in a final string for ESLint to output
152
+ * @param output final string to output
153
+ * @param msgs error messages based on category
154
+ * @returns
155
+ */
156
+ function formatOutput(output, msgs) {
157
+ // First, output model msgs per file (from ESLint 'stylish' output)
158
+ for (const fileModel in msgs.model) {
159
+ if (msgs.model[fileModel].length > 0) {
160
+ output += `${fileModel}\n`;
161
+ output += `${msgs.model[fileModel].join("\n")}\n\n`;
162
+ }
163
+ }
164
+ // Next, output env msgs (file-independent but associated to project)
165
+ for (const fileModel in msgs.environment) {
166
+ if (msgs.environment[fileModel].length > 0) {
167
+ output += `${fileModel}\n`;
168
+ output += `${msgs.environment[fileModel].join("\n")}\n\n`;
169
+ }
170
+ }
171
+ // Finally output error/warning report count
172
+ if (output && msgs.report) {
173
+ output += `${msgs.report.join("\n")}`;
174
+ }
175
+ if (output) {
176
+ output = `\n${output}`;
177
+ if (Cache.has('linted')) {
178
+ output += `\n\nTotal files linted: ${Cache.get('linted').files.length}\n`;
179
+ }
180
+ }
181
+ return output;
182
+ }
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,11 +25,6 @@ module.exports = {
39
25
  CXL: true,
40
26
  cds: true,
41
27
  },
42
-
43
- overrides: [
44
- {
45
- files: ["*.cds", "*.csn"],
46
- parser: "./parser",
47
- },
48
- ],
28
+ files: ["*.cds", "*.csn", "*.csv", "undeploy.json"],
29
+ modelFiles: ["*.cds", "*.csn"],
49
30
  };
package/lib/impl/index.js CHANGED
@@ -1,23 +1,63 @@
1
1
  /**
2
2
  * Custom ESLint plugin:
3
3
  * https://eslint.org/docs/developer-guide/working-with-plugins
4
- * This file must:
5
- * - Expose any 'rules' 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
6
8
  * - Expose any 'globals' for use in ESLint
7
- * - Expose any 'configs' for prescribed rule configuration bundles (i.e. "recommended")
8
- * See shareable configs: https://eslint.org/docs/developer-guide/shareable-configs
9
+ * - Expose any 'processors' for use in ESLint
10
+ * - Expose any 'rules' for use in ESLint
11
+ * We also initiate and cache the objects 'rulesInfo' and 'cds' for later use.
9
12
  */
10
13
 
11
- const { rules } = require("./rules");
14
+ const path = require("path");
15
+ const processor = require("./processor");
12
16
  const plugin = require("../../package.json").name;
13
- const { globals, overrides, recommended } = require("./constants");
14
17
 
15
- const configs = {
16
- recommended: {
17
- plugins: [plugin],
18
- overrides,
19
- rules: recommended,
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");
26
+
27
+ const cds = require("@sap/cds");
28
+ cds.getLocation = getLocation;
29
+ cds.getRange = getRange;
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
+ );
44
+
45
+ module.exports = {
46
+ configs: {
47
+ recommended: {
48
+ globals,
49
+ plugins: [plugin],
50
+ overrides: [
51
+ {
52
+ files: files,
53
+ processor: "@sap/cds/cds",
54
+ },
55
+ ],
56
+ rules: recommended,
57
+ },
58
+ },
59
+ processors: {
60
+ cds: processor,
20
61
  },
62
+ rules: rulesInfo.rules,
21
63
  };
22
-
23
- module.exports = { rules, globals, configs };
@@ -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: [],
@@ -0,0 +1,23 @@
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
+
12
+ const { Cache } = require("./utils/model");
13
+ const { isValidFile } = require("./utils/helpers");
14
+
15
+ module.exports = {
16
+ preprocess: function (text, filename) {
17
+ if (isValidFile(filename)) {
18
+ Cache.set(`file:${filename}`, text);
19
+ }
20
+ return [{ text: "", filename }];
21
+ },
22
+ supportsAutofix: true,
23
+ };