@sap/eslint-plugin-cds 2.2.2 → 2.3.3
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 +32 -106
- package/lib/api/index.js +11 -13
- package/lib/api/lint.d.ts +48 -0
- package/lib/constants.js +54 -0
- package/lib/index.js +44 -0
- package/lib/{impl/parser.js → parser.js} +2 -13
- package/lib/processor.js +47 -0
- package/lib/{impl/rules → rules}/assoc2many-ambiguous-key.js +51 -52
- package/lib/rules/latest-cds-version.js +42 -0
- package/lib/rules/min-node-version.js +47 -0
- package/lib/rules/no-db-keywords.js +46 -0
- package/lib/rules/no-dollar-prefixed-names.js +47 -0
- package/lib/{impl/rules → rules}/no-join-on-draft-enabled-entities.js +16 -11
- package/lib/rules/require-2many-oncond.js +27 -0
- package/lib/rules/sql-cast-suggestion.js +52 -0
- package/lib/rules/start-elements-lowercase.js +61 -0
- package/lib/rules/start-entities-uppercase.js +55 -0
- package/lib/rules/valid-csv-header.js +100 -0
- package/lib/utils/fuzzySearch.js +87 -0
- package/lib/utils/helpers.js +55 -0
- package/lib/{impl/utils → utils}/jsonc.js +0 -0
- package/lib/{impl/utils → utils}/model.js +122 -216
- package/lib/utils/ruleHelpers.js +56 -0
- package/lib/utils/ruleTester.js +79 -0
- package/lib/utils/rules.js +1033 -0
- package/lib/{impl/utils → utils}/validate.js +8 -21
- package/package.json +3 -3
- package/lib/api/formatter.js +0 -182
- package/lib/impl/constants.js +0 -40
- package/lib/impl/index.js +0 -58
- package/lib/impl/processor.js +0 -23
- package/lib/impl/ruleFactory.js +0 -311
- package/lib/impl/rules/cds-compile-error.js +0 -35
- package/lib/impl/rules/latest-cds-version.js +0 -46
- package/lib/impl/rules/min-node-version.js +0 -42
- package/lib/impl/rules/no-db-keywords.js +0 -35
- package/lib/impl/rules/require-2many-oncond.js +0 -29
- package/lib/impl/rules/rule.hbs +0 -20
- package/lib/impl/rules/sql-cast-suggestion.js +0 -50
- package/lib/impl/rules/start-elements-lowercase.js +0 -74
- package/lib/impl/rules/start-entities-uppercase.js +0 -65
- package/lib/impl/types.d.ts +0 -48
- package/lib/impl/utils/helpers.js +0 -68
- package/lib/impl/utils/rules.js +0 -550
package/CHANGELOG.md
CHANGED
|
@@ -6,110 +6,77 @@ 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.
|
|
10
|
-
|
|
11
|
-
## Changed
|
|
12
|
-
|
|
13
|
-
- Allow not only *.js but also other file types (i.e. *.ts, etc) to bypass plugin rules
|
|
14
|
-
|
|
15
|
-
## [2.1.1] - 2021-10-04
|
|
16
|
-
|
|
17
|
-
## Changed
|
|
18
|
-
|
|
19
|
-
- Added preprocessor to avoid (other plugins) parsing errors on cds files
|
|
20
|
-
|
|
21
|
-
## [2.2.0] - 2021-10-29
|
|
22
|
-
|
|
23
|
-
## Added
|
|
24
|
-
|
|
25
|
-
- Added typings to javascript for all exposed apis
|
|
26
|
-
|
|
27
|
-
## Changed
|
|
28
|
-
|
|
29
|
-
- Aligned rule creation and tester api with ESLint
|
|
30
|
-
|
|
31
|
-
## [2.1.2] - 2021-10-05
|
|
32
|
-
|
|
33
|
-
## Changed
|
|
9
|
+
## [2.3.3] - 2022-03-24
|
|
34
10
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
## [2.1.1] - 2021-10-04
|
|
38
|
-
|
|
39
|
-
## Changed
|
|
40
|
-
|
|
41
|
-
- Added preprocessor to avoid (other plugins) parsing errors on cds files
|
|
42
|
-
|
|
43
|
-
## [2.2.1] - 2021-10-29
|
|
11
|
+
### Added
|
|
44
12
|
|
|
45
|
-
|
|
13
|
+
- Added new rule `no-dollar-prefixed-names`
|
|
14
|
+
- Lint reports with rules marked with '!' notify of rule compile errors
|
|
15
|
+
- Lint reports of any thrown errors can be exposed by `--debug` (includes stack)
|
|
46
16
|
|
|
47
|
-
|
|
17
|
+
## [2.3.2] - 2022-01-24
|
|
48
18
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
## Added
|
|
19
|
+
### Changed
|
|
52
20
|
|
|
53
|
-
-
|
|
21
|
+
- Rule `require-2many-oncond` now also detect navigations of aaspects for flavor 'parsed'
|
|
22
|
+
- Removed duplicates from rule results of category 'Environment'
|
|
54
23
|
|
|
55
|
-
##
|
|
24
|
+
## [2.3.1] - 2021-12-10
|
|
56
25
|
|
|
57
|
-
|
|
26
|
+
### Changed
|
|
58
27
|
|
|
59
|
-
|
|
28
|
+
- Removed custom formatter as it is no longer used by `cds lint`.
|
|
29
|
+
- Deduplicate lint results from from rules of category environment.
|
|
30
|
+
- Removed 'unpeploy.json' from files as no lint rule requires it
|
|
31
|
+
- Fixed redundant triggers of model recompilations for non-model files and model files without changes
|
|
60
32
|
|
|
61
|
-
##
|
|
33
|
+
## [2.3.0] - 2021-12-03
|
|
62
34
|
|
|
63
|
-
|
|
35
|
+
### Added
|
|
64
36
|
|
|
65
|
-
|
|
37
|
+
- Added new rule 'valid-csv-header'
|
|
66
38
|
|
|
67
|
-
|
|
39
|
+
### Changed
|
|
68
40
|
|
|
69
|
-
-
|
|
41
|
+
- Fixed suggestion messages in editor option (and disabled auto-fix)
|
|
42
|
+
- Added rule properties 'docs.recommended', 'severity'
|
|
70
43
|
|
|
71
44
|
## [2.2.2] - 2021-11-08
|
|
72
|
-
|
|
73
|
-
## Added
|
|
45
|
+
### Added
|
|
74
46
|
|
|
75
47
|
- Added new rule 'no-join-on-draft-enabled-entities'
|
|
76
48
|
|
|
77
|
-
|
|
49
|
+
### Changed
|
|
78
50
|
|
|
79
51
|
- Compile 'model' files based on CSN flavor 'inferred'
|
|
80
52
|
- Compile 'outsider' files based on CSN flavor 'parsed'
|
|
81
53
|
|
|
82
54
|
## [2.2.1] - 2021-11-01
|
|
83
|
-
|
|
84
|
-
## Changed
|
|
55
|
+
### Changed
|
|
85
56
|
|
|
86
57
|
- Optimized model loading and fixed bug in loading of 'outsider' files
|
|
87
58
|
|
|
88
59
|
## [2.2.0] - 2021-10-29
|
|
89
|
-
|
|
90
|
-
## Added
|
|
60
|
+
### Added
|
|
91
61
|
|
|
92
62
|
- Added typings to javascript for all exposed apis
|
|
93
63
|
|
|
94
|
-
|
|
64
|
+
### Changed
|
|
95
65
|
|
|
96
66
|
- Aligned rule creation and tester api with ESLint
|
|
97
67
|
|
|
98
68
|
## [2.1.2] - 2021-10-05
|
|
99
|
-
|
|
100
|
-
## Changed
|
|
69
|
+
### Changed
|
|
101
70
|
|
|
102
71
|
- Allow not only *.js but also other file types (i.e. *.ts, etc) to bypass plugin rules
|
|
103
72
|
|
|
104
73
|
## [2.1.1] - 2021-10-04
|
|
105
|
-
|
|
106
|
-
## Changed
|
|
74
|
+
### Changed
|
|
107
75
|
|
|
108
76
|
- Added preprocessor to avoid (other plugins) parsing errors on cds files
|
|
109
77
|
|
|
110
78
|
## [2.1.0] - 2021-09-23
|
|
111
|
-
|
|
112
|
-
## Changed
|
|
79
|
+
### Changed
|
|
113
80
|
|
|
114
81
|
- Source code is now Javascript only
|
|
115
82
|
- Rule API simplified to only include report and cds
|
|
@@ -117,8 +84,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
|
|
|
117
84
|
- Filter out lint messages when run from command line with custom formatter
|
|
118
85
|
|
|
119
86
|
## [2.0.5] - 2021-07-18
|
|
120
|
-
|
|
121
|
-
|
|
122
87
|
### Added
|
|
123
88
|
|
|
124
89
|
- When used from within VS Code ESLint exnteion, do not show environment rules
|
|
@@ -127,8 +92,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
|
|
|
127
92
|
- Rule 'min-node-version' reverted to use cds.resolve not cds.home
|
|
128
93
|
|
|
129
94
|
## [2.0.4] - 2021-07-12
|
|
130
|
-
|
|
131
|
-
|
|
132
95
|
### Added
|
|
133
96
|
|
|
134
97
|
- Plugin also considers 'outsider' files which are not part of a model
|
|
@@ -137,21 +100,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
|
|
|
137
100
|
- Custom formatter now prints projectPath for triggered env rule checks
|
|
138
101
|
|
|
139
102
|
## [2.0.3] - 20210-07-09
|
|
140
|
-
|
|
141
103
|
### Changed
|
|
142
104
|
|
|
143
105
|
- Removed `npm-shrinkwrap.json` file from package
|
|
144
106
|
|
|
145
107
|
## [2.0.2] - 2021-07-07
|
|
146
|
-
|
|
147
|
-
|
|
148
108
|
### Changed
|
|
149
109
|
|
|
150
110
|
- Fixed bug that always triggers csn-compile-err on type
|
|
151
111
|
|
|
152
112
|
## [2.0.1] - 2021-07-06
|
|
153
|
-
|
|
154
|
-
|
|
155
113
|
### Added
|
|
156
114
|
|
|
157
115
|
- Re-added model rule 'csn-compile-err' to pass csn compile errors to eslint for readability
|
|
@@ -161,8 +119,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
|
|
|
161
119
|
- Formatter ignores all lint reports for other file extensions except for those in plugin overrides files
|
|
162
120
|
|
|
163
121
|
## [2.0.0] - 2021-07-02
|
|
164
|
-
|
|
165
|
-
|
|
166
122
|
### Added
|
|
167
123
|
|
|
168
124
|
- New API: split exports into 'impl' (for eslint) and 'api' (for user)
|
|
@@ -173,15 +129,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
|
|
|
173
129
|
- API: getRuleTester now erquires relative path from ruleTester location to project root
|
|
174
130
|
|
|
175
131
|
## [1.1.7] - 2021-06-22
|
|
176
|
-
|
|
177
|
-
|
|
178
132
|
### Changed
|
|
179
133
|
|
|
180
134
|
- Load/Update model must be in sync with every 'on-type' event
|
|
181
135
|
|
|
182
136
|
## [1.1.6] - 2021-06-21
|
|
183
|
-
|
|
184
|
-
|
|
185
137
|
### Changed
|
|
186
138
|
|
|
187
139
|
- Formatter no longer has explicit dependencies, only reslies on 'stylish' output
|
|
@@ -190,26 +142,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
|
|
|
190
142
|
- On glob file expressions, gets multiple models in series and reports on all warnings/errors
|
|
191
143
|
|
|
192
144
|
## [1.1.5] - 2021-05-26
|
|
193
|
-
|
|
145
|
+
### Changed
|
|
194
146
|
|
|
195
147
|
- Naming convention rules changed to severity 'warning'
|
|
196
148
|
- Rules of type 'suggestion' must return 'fix' for appliable multipass fixes
|
|
197
149
|
- Split model generation into load and update to be able to work on all files
|
|
198
150
|
|
|
199
151
|
## [1.1.4] - 2021-05-20
|
|
200
|
-
|
|
201
|
-
|
|
202
152
|
### Changed
|
|
203
153
|
|
|
204
154
|
- Formatter does not show any (env/other) lint messages on model error
|
|
205
155
|
- Rule lower-camelcase-elements reverted to also check type keys
|
|
206
156
|
- Rule lower-camelcase-elements is not triggered by element 'ID' (see Bookshop in CAP samples)
|
|
207
157
|
|
|
208
|
-
|
|
209
158
|
## [1.1.3] - 2021-05-12
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
159
|
### Changed
|
|
214
160
|
|
|
215
161
|
- Changed rule type for naming convention rules to "suggestion"
|
|
@@ -217,16 +163,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
|
|
|
217
163
|
- Added extra layer to also support ruleTester "environment" checks
|
|
218
164
|
|
|
219
165
|
## [1.1.2] - 2021-05-05
|
|
220
|
-
|
|
221
|
-
|
|
222
166
|
### Changed
|
|
223
167
|
|
|
224
168
|
- Updated all rules to ingest args: (cds, context)
|
|
225
169
|
- Use context's sourcecode to get correct range indices for fixers
|
|
226
170
|
|
|
227
171
|
## [1.1.1] - 2021-05-04
|
|
228
|
-
|
|
229
|
-
|
|
230
172
|
### Changed
|
|
231
173
|
|
|
232
174
|
- Removed bulky headers from custom formatter
|
|
@@ -236,8 +178,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
|
|
|
236
178
|
- Fixed formatter to also print any other ESLint messages received
|
|
237
179
|
|
|
238
180
|
## [1.1.0] - 2021-04-29
|
|
239
|
-
|
|
240
|
-
|
|
241
181
|
### Added
|
|
242
182
|
|
|
243
183
|
- Custom cds formatter for separate reporting of env and model checks
|
|
@@ -246,25 +186,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
|
|
|
246
186
|
|
|
247
187
|
- Fixed version of model rule 'sql-cast-suggestion'
|
|
248
188
|
|
|
249
|
-
|
|
250
189
|
## [1.0.8] - 2021-04-12
|
|
251
|
-
|
|
252
|
-
|
|
253
190
|
### Added
|
|
254
191
|
|
|
255
192
|
- Proxy for cds object replaces fragile object clone from before
|
|
256
193
|
- Added proper typings and ignore where options should remain invisible to the (cds) api
|
|
257
194
|
- Added docstrings and header to each file to explain ESLint context
|
|
258
195
|
- Added model rule 'sql-cast-suggestion'
|
|
259
|
-
|
|
260
196
|
## [1.0.7] - 2021-04-01
|
|
261
|
-
|
|
262
|
-
### Fixed
|
|
197
|
+
### Changed
|
|
263
198
|
|
|
264
199
|
- Do not crash if `parserServices.cds` is not available
|
|
265
200
|
|
|
266
201
|
## [1.0.6] - 2021-04-01
|
|
267
|
-
|
|
268
202
|
### Added
|
|
269
203
|
|
|
270
204
|
- peer dependency to `eslint`
|
|
@@ -272,7 +206,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
|
|
|
272
206
|
- simplified api, cds instead of parserServices
|
|
273
207
|
|
|
274
208
|
## [1.0.4] - 2021-03-24
|
|
275
|
-
|
|
276
209
|
### Changed
|
|
277
210
|
|
|
278
211
|
- Added sync model load from cds to generate fully resolved models
|
|
@@ -281,25 +214,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
|
|
|
281
214
|
- Removed model rule 'no-entity-moo' and use as sample custom rule in docs
|
|
282
215
|
- Refactored and added more parserServices
|
|
283
216
|
|
|
284
|
-
|
|
285
217
|
## [1.0.3] - 2021-01-22
|
|
286
|
-
|
|
287
218
|
### Changed
|
|
288
219
|
|
|
289
220
|
- Fixed rule min-node-version to check if cds dependency is installed
|
|
290
221
|
- Updated README glob statement to double asterisk for check nested dirs
|
|
291
222
|
|
|
292
|
-
|
|
293
223
|
## [1.0.2] - 2021-01-21
|
|
294
|
-
|
|
295
224
|
### Changed
|
|
296
225
|
|
|
297
226
|
- Fixed rules to work in concert and allow for globs
|
|
298
227
|
- Improved README for better readability
|
|
299
228
|
|
|
300
|
-
|
|
301
229
|
## [1.0.1] - 2021-01-19
|
|
302
|
-
|
|
303
230
|
### Added
|
|
304
231
|
|
|
305
232
|
- Rule `assocs-card-flaw` in category *Model validation*
|
|
@@ -308,7 +235,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
|
|
|
308
235
|
|
|
309
236
|
- Refactoring of ruleFactory and parser code
|
|
310
237
|
|
|
311
|
-
|
|
312
238
|
## [1.0.0] - 2020-12-07
|
|
313
|
-
|
|
239
|
+
### Added
|
|
314
240
|
- Initial release 1.0.0
|
package/lib/api/index.js
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Our custom ESLint plugin API should:
|
|
3
|
-
* - Expose 'createRule'
|
|
4
|
-
* support the addition of *custom* CDS Lint rules
|
|
5
|
-
* - Expose 'getConfigPath', 'getFileExtensions', and 'genDocs' for usage in
|
|
5
|
+
* - Expose 'createRule' and 'runRuleTester' to
|
|
6
|
+
* support the addition of *custom* CDS Lint rules at runtime
|
|
7
|
+
* - Expose 'getConfigPath', 'getFileExtensions', and 'genDocs' for usage in
|
|
8
|
+
* 'cds lint' client (@sap/cds-dk)
|
|
6
9
|
* - Expose 'parserPath' for CDS Lint rule unit tests with ESLint's ruleTester
|
|
7
10
|
*/
|
|
8
11
|
|
|
9
|
-
const {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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");
|
|
12
|
+
const { runRuleTester } = require("../utils/ruleTester")
|
|
13
|
+
const { getConfigPath } = require("../utils/model");
|
|
14
|
+
const { getFileExtensions } = require("../utils/helpers");
|
|
15
|
+
const { createRule, genDocs } = require("../utils/rules");
|
|
16
|
+
const parserPath = require.resolve("../parser");
|
|
18
17
|
|
|
19
18
|
module.exports = {
|
|
20
19
|
createRule,
|
|
21
|
-
defineRule,
|
|
22
20
|
runRuleTester,
|
|
23
21
|
getConfigPath,
|
|
24
22
|
getFileExtensions,
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
|
|
2
|
+
import { Linter, Rule, RuleTester, SourceCode } from "eslint";
|
|
3
|
+
|
|
4
|
+
export interface CDSRuleContext extends Rule.RuleContext {
|
|
5
|
+
cds: any;
|
|
6
|
+
configPath: string;
|
|
7
|
+
code: string;
|
|
8
|
+
filePath: string;
|
|
9
|
+
options: [];
|
|
10
|
+
id: string;
|
|
11
|
+
sourcecode: SourceCode;
|
|
12
|
+
report: (CDSRuleReport) => void;
|
|
13
|
+
err: Error;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface CDSRuleSpec {
|
|
17
|
+
meta: CDSRuleMetaData,
|
|
18
|
+
create: (context: CDSRuleContext) => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface CDSRuleMetaData extends Rule.RuleMetaData {
|
|
22
|
+
docs: Rule.RuleMetaData['docs'] & {
|
|
23
|
+
version: string;
|
|
24
|
+
};
|
|
25
|
+
severity?: Linter.RuleLevel;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type CDSRuleReport = Rule.ReportDescriptor & {
|
|
29
|
+
loc?: Rule.ReportDescriptorLocation;
|
|
30
|
+
file?: string;
|
|
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 errors from ESLint's [RuleTester](https://eslint.org/docs/developer-guide/nodejs-api#ruletester) */
|
|
47
|
+
errors: CDSTestCaseError[]
|
|
48
|
+
}
|
package/lib/constants.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is used to store/share any constants for this plugin:
|
|
3
|
+
* - RULE_CATEGORIES: List of plugin's rule categories (used purely as documentation labels)
|
|
4
|
+
* - DEFAULT_RULE_CATEGORY: Default rule category (must be one of plugin's rule categories)
|
|
5
|
+
* - DEFAULT_RULE_SEVERITY: Default rule severity (must be one of those define by ESLint):
|
|
6
|
+
* https://eslint.org/docs/user-guide/configuring/rules#configuring-rules
|
|
7
|
+
* - DEFAULT_RULE_TYPE: Default rule type (must be one of those defined by ESLint):
|
|
8
|
+
* https://eslint.org/docs/developer-guide/working-with-rules#rule-basics
|
|
9
|
+
* - CUSTOM_RULES_DIR: Custom rules directory name in the user's project home
|
|
10
|
+
* (contains rules, docs, tests)
|
|
11
|
+
* - FILES: Files/file extensions which ESLint should lint with this plugin
|
|
12
|
+
* - MODEL_FILES: Files/file extensions which should can be compiled (to CSN)
|
|
13
|
+
* - GLOBALS: Globals which should be exposed to ESLint by this plugin
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const RULE_CATEGORIES = ["Model Validation", "Environment"];
|
|
17
|
+
const DEFAULT_RULE_CATEGORY = RULE_CATEGORIES[0];
|
|
18
|
+
const DEFAULT_RULE_SEVERITY = "error";
|
|
19
|
+
const DEFAULT_RULE_TYPE = "problem";
|
|
20
|
+
|
|
21
|
+
const PLUGIN_NAME = require("../package.json").name;
|
|
22
|
+
const PLUGIN_PREFIX = "@sap/cds";
|
|
23
|
+
const PROCESSOR_NAME = `${PLUGIN_PREFIX}/cds`
|
|
24
|
+
const CUSTOM_RULES_DIR = ".cdslint";
|
|
25
|
+
|
|
26
|
+
const FILES = ["*.cds", "*.csn", "*.csv"];
|
|
27
|
+
const MODEL_FILES = ["*.cds", "*.csn"];
|
|
28
|
+
|
|
29
|
+
const GLOBALS = {
|
|
30
|
+
SELECT: true,
|
|
31
|
+
INSERT: true,
|
|
32
|
+
UPDATE: true,
|
|
33
|
+
DELETE: true,
|
|
34
|
+
CREATE: true,
|
|
35
|
+
DROP: true,
|
|
36
|
+
CDL: true,
|
|
37
|
+
CQL: true,
|
|
38
|
+
CXL: true,
|
|
39
|
+
cds: true,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
module.exports = {
|
|
43
|
+
RULE_CATEGORIES,
|
|
44
|
+
DEFAULT_RULE_CATEGORY,
|
|
45
|
+
DEFAULT_RULE_SEVERITY,
|
|
46
|
+
DEFAULT_RULE_TYPE,
|
|
47
|
+
PLUGIN_NAME,
|
|
48
|
+
PLUGIN_PREFIX,
|
|
49
|
+
PROCESSOR_NAME,
|
|
50
|
+
CUSTOM_RULES_DIR,
|
|
51
|
+
FILES,
|
|
52
|
+
MODEL_FILES,
|
|
53
|
+
GLOBALS
|
|
54
|
+
};
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom ESLint plugin:
|
|
3
|
+
* https://eslint.org/docs/developer-guide/working-with-plugins
|
|
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
|
|
9
|
+
* - Expose any 'processors' for use in ESLint
|
|
10
|
+
* - Expose any 'rules' for use in ESLint
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const path = require("path");
|
|
14
|
+
const processor = require("./processor");
|
|
15
|
+
|
|
16
|
+
const { FILES, GLOBALS, PLUGIN_NAME, PROCESSOR_NAME } = require("./constants");
|
|
17
|
+
const { getRules } = require("./utils/rules");
|
|
18
|
+
|
|
19
|
+
const rules = getRules(path.join(__dirname, "rules"));
|
|
20
|
+
|
|
21
|
+
function _getConfig(configName) {
|
|
22
|
+
return {
|
|
23
|
+
globals: GLOBALS,
|
|
24
|
+
plugins: [PLUGIN_NAME],
|
|
25
|
+
overrides: [
|
|
26
|
+
{
|
|
27
|
+
files: FILES,
|
|
28
|
+
processor: PROCESSOR_NAME,
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
rules: rules[configName],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = {
|
|
36
|
+
configs: {
|
|
37
|
+
recommended: _getConfig("recommended"),
|
|
38
|
+
all: _getConfig("all"),
|
|
39
|
+
},
|
|
40
|
+
processors: {
|
|
41
|
+
cds: processor,
|
|
42
|
+
},
|
|
43
|
+
rules: rules.sources,
|
|
44
|
+
};
|
|
@@ -9,14 +9,7 @@
|
|
|
9
9
|
* (parserOptions). Note, that because we use a 'empty' preprocessor, the
|
|
10
10
|
* parser is only used by ESLint's ruleTester.
|
|
11
11
|
*/
|
|
12
|
-
|
|
13
|
-
const {
|
|
14
|
-
getAST,
|
|
15
|
-
getCDSProxy,
|
|
16
|
-
getLocation,
|
|
17
|
-
getRange,
|
|
18
|
-
} = require("../impl/utils/model");
|
|
19
|
-
const cds = require("@sap/cds");
|
|
12
|
+
const { getAST } = require("./utils/model");
|
|
20
13
|
|
|
21
14
|
module.exports = {
|
|
22
15
|
parse: function (code, options) {
|
|
@@ -24,13 +17,9 @@ module.exports = {
|
|
|
24
17
|
},
|
|
25
18
|
|
|
26
19
|
parseForESLint: function (code) {
|
|
27
|
-
cds.getLocation = getLocation;
|
|
28
|
-
cds.getRange = getRange;
|
|
29
20
|
return {
|
|
30
21
|
ast: getAST(code),
|
|
31
|
-
services: {
|
|
32
|
-
cdsProxy: getCDSProxy(cds),
|
|
33
|
-
},
|
|
22
|
+
services: {},
|
|
34
23
|
scopeManager: null,
|
|
35
24
|
tokensAndComments: [],
|
|
36
25
|
visitorKeys: null,
|
package/lib/processor.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom ESLint 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
|
+
*
|
|
8
|
+
* Note, that because we cache the file contents and return files contents,
|
|
9
|
+
* the plugin's parser is bypassed so we must retrieve the file contents (see createRule()
|
|
10
|
+
* in utils/rules.js).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const { Cache } = require("./utils/model");
|
|
14
|
+
const { isValidFile } = require("./utils/helpers");
|
|
15
|
+
|
|
16
|
+
module.exports = {
|
|
17
|
+
preprocess: function (text, filename) {
|
|
18
|
+
if (isValidFile(filename, 'FILES')) {
|
|
19
|
+
Cache.set(`file:${filename}`, text);
|
|
20
|
+
}
|
|
21
|
+
return [{ text: "", filename }];
|
|
22
|
+
},
|
|
23
|
+
/**
|
|
24
|
+
* Returns message objects as defined by ESLint:
|
|
25
|
+
* https://eslint.org/docs/developer-guide/working-with-custom-formatters#the-message-object
|
|
26
|
+
* @param {*} messages
|
|
27
|
+
* @returns
|
|
28
|
+
*/
|
|
29
|
+
postprocess: function (messages) {
|
|
30
|
+
const messagesSanitized = [];
|
|
31
|
+
messages.forEach(fileMessages => {
|
|
32
|
+
const fileMessagesSanitized = [];
|
|
33
|
+
fileMessages.forEach(r => {
|
|
34
|
+
if (r.message.startsWith(`CompilationError:`)) {
|
|
35
|
+
r.message = r.message.replace(`CompilationError: `,
|
|
36
|
+
'CDS model could not be compiled!\n');
|
|
37
|
+
r.ruleId = `❗${r.ruleId}`;
|
|
38
|
+
r.severity = 2;
|
|
39
|
+
}
|
|
40
|
+
fileMessagesSanitized.push(r);
|
|
41
|
+
})
|
|
42
|
+
messagesSanitized.push(fileMessagesSanitized);
|
|
43
|
+
})
|
|
44
|
+
return [].concat(...messagesSanitized);
|
|
45
|
+
},
|
|
46
|
+
supportsAutofix: true,
|
|
47
|
+
};
|