@sap/eslint-plugin-cds 3.0.5 → 3.1.1

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 (46) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/README.md +1 -1
  3. package/lib/api/index.js +4 -4
  4. package/lib/conf/all.js +17 -17
  5. package/lib/conf/experimental.js +12 -0
  6. package/lib/conf/index.js +12 -3
  7. package/lib/conf/recommended.js +13 -14
  8. package/lib/constants.js +2 -0
  9. package/lib/index.js +2 -1
  10. package/lib/parser.js +10 -1
  11. package/lib/rules/assoc2many-ambiguous-key.js +40 -11
  12. package/lib/rules/auth-no-empty-restrictions.js +38 -10
  13. package/lib/rules/auth-restrict-grant-service.js +29 -29
  14. package/lib/rules/auth-use-requires.js +27 -15
  15. package/lib/rules/auth-valid-restrict-grant.js +138 -82
  16. package/lib/rules/auth-valid-restrict-keys.js +34 -18
  17. package/lib/rules/auth-valid-restrict-to.js +57 -106
  18. package/lib/rules/auth-valid-restrict-where.js +44 -43
  19. package/lib/rules/extension-restrictions.js +11 -3
  20. package/lib/rules/index.js +5 -1
  21. package/lib/rules/latest-cds-version.js +5 -4
  22. package/lib/rules/no-db-keywords.js +14 -5
  23. package/lib/rules/no-dollar-prefixed-names.js +9 -2
  24. package/lib/rules/no-java-keywords.js +181 -0
  25. package/lib/rules/no-join-on-draft.js +9 -3
  26. package/lib/rules/sql-cast-suggestion.js +19 -15
  27. package/lib/rules/sql-null-comparison.js +60 -0
  28. package/lib/rules/start-elements-lowercase.js +6 -2
  29. package/lib/rules/start-entities-uppercase.js +12 -5
  30. package/lib/rules/valid-csv-header.js +33 -13
  31. package/lib/types.d.ts +4 -4
  32. package/lib/utils/Cache.js +4 -2
  33. package/lib/utils/Colors.js +2 -0
  34. package/lib/utils/LintError.js +17 -0
  35. package/lib/utils/createRule.js +160 -134
  36. package/lib/utils/csnTraversal.js +163 -0
  37. package/lib/utils/findFuzzy.js +21 -12
  38. package/lib/utils/getConfigPath.js +4 -2
  39. package/lib/utils/getConfiguredFileTypes.js +2 -0
  40. package/lib/utils/getFileExtensions.js +2 -0
  41. package/lib/utils/getProjectRootPath.js +53 -15
  42. package/lib/utils/isConfiguredFileType.js +8 -3
  43. package/lib/utils/rules.js +15 -9
  44. package/lib/utils/runRuleTester.js +69 -36
  45. package/package.json +1 -1
  46. package/lib/utils/genDocs.js +0 -346
@@ -1,346 +0,0 @@
1
- const fs = require('fs')
2
- const os = require('os')
3
- const path = require('path')
4
- const semver = require('semver')
5
- const cp = require('child_process')
6
-
7
- const cds = require('@sap/cds')
8
- const { mkdirp } = cds.utils
9
-
10
- const Cache = require('./Cache')
11
- const IS_WIN = os.platform() === 'win32'
12
-
13
- const { exit } = require('process')
14
-
15
- const constants = require('../constants')
16
- const LOG = process.env.SILENT ? undefined : constants.log
17
-
18
- /**
19
- * Generates custom rules documentation (markdown files)
20
- * for user according to contents of:
21
- * - Rule files
22
- * - Test files (with valid/invalid/fixed examples)
23
- */
24
- module.exports = async (projectPath, customRulesDir, registry, prepareRelease = false) => {
25
- let docsPath, rulePath, testPath, release
26
-
27
- Cache.set('testerCases', true)
28
-
29
- if (!projectPath) {
30
- docsPath = path.join(__dirname, '../../docs')
31
- rulePath = path.join(__dirname, '../rules')
32
- testPath = path.join(__dirname, '../../tests/lib/rules')
33
- release = JSON.parse(fs.readFileSync(path.join(__dirname, '../../package.json')).toString()).version
34
- } else {
35
- docsPath = path.join(projectPath, `${customRulesDir}/docs`)
36
- rulePath = path.join(projectPath, `${customRulesDir}/rules`)
37
- testPath = path.join(projectPath, `${customRulesDir}/tests`)
38
- await Promise.all(
39
- [docsPath, rulePath, testPath].filter((path) => !fs.existsSync(path)).map((path) => mkdirp(path))
40
- )
41
- }
42
-
43
- if (registry) {
44
- // Get rules (internal on artifactory)
45
- const versionInternal = prepareRelease
46
- ? JSON.parse(fs.readFileSync(path.join(__dirname, '../../package.json')).toString()).version
47
- : getPackageVersion(registry)
48
- if (versionInternal) {
49
- LOG?.(`Updating internal rules from v>=${versionInternal}:\n${registry}\n`)
50
- const rulesInternal = getRules(docsPath, rulePath, testPath, versionInternal)
51
- genDocFiles(rulesInternal, docsPath)
52
- }
53
- // Get rules released (external on npm)
54
- const npmRegistry = 'https://registry.npmjs.org'
55
- const versionExternal = prepareRelease
56
- ? JSON.parse(fs.readFileSync(path.join(__dirname, '../../package.json')).toString()).version
57
- : getPackageVersion(npmRegistry)
58
- if (versionExternal) {
59
- LOG?.(`Updating external rules from v>=${versionExternal}:\n${npmRegistry}\n`)
60
- const rulesExternal = getRules(docsPath, rulePath, testPath, versionExternal, release)
61
- genDocFiles(rulesExternal, docsPath, release)
62
- }
63
- } else {
64
- // Get "custom" rules
65
- const rules = getRules(docsPath, rulePath, testPath)
66
- genDocFiles(rules, docsPath)
67
- }
68
- LOG?.('Done!')
69
- }
70
-
71
- /**
72
- * Generates markdown table of all rules with their respective properties based on input rules
73
- * @param rules array of rules with mandatory name, details properties
74
- * @param release
75
- * @returns markdown table of all rules
76
- */
77
- function genMdRules (rules, release = false) {
78
- let mdRulesTable = ''
79
- if (rules.length > 0) {
80
- const emojiRecommended = '✔️'
81
- const emojiFixable = '🔧'
82
- const emojiSuggestions = '💡'
83
- const emojiConstruction = '🚧'
84
- let mdRulesHeader = 'Rules in ESLint are grouped by type to help you understand their purpose. Each rule has emojis denoting:\n\n'
85
- mdRulesHeader += `${emojiRecommended} if the plugin's "recommended" configuration enables the rule\n\n`
86
- mdRulesHeader += `${emojiFixable} if problems reported by the rule are automatically fixable (\`--fix\`)\n\n`
87
- mdRulesHeader += `${emojiSuggestions} if problems reported by the rule are manually fixable (editor)\n\n`
88
- if (!release) {
89
- mdRulesHeader += `${emojiConstruction} if rule exists in plugin (main branch) but is not yet released (artifactory)\n\n`
90
- mdRulesHeader += '| | | | | | | |\n'
91
- mdRulesHeader += '|:-:|:-:|:-:|:-:|-:|:-|:-|\n'
92
- } else {
93
- mdRulesHeader += '| | | | | | | |\n'
94
- mdRulesHeader += '|:-:|:-:|:-:|:-:|-:|:-|:-|\n'
95
- }
96
- let mdRules = ''
97
- rules.forEach((rule) => {
98
- if (rule.name && rule.details) {
99
- const isRecommended = rule.recommended ? emojiRecommended : ''
100
- const isFixable = rule.fixable ? emojiFixable : ''
101
- const hasSuggestions = rule.hasSuggestions ? emojiSuggestions : ''
102
- const underConstruction = rule.construction ? emojiConstruction : ''
103
- mdRules += release
104
- ? `| ${isRecommended} | ${isFixable} | ${hasSuggestions} | |   | [${rule.name}](Rules-released.md#${rule.name}) | ${rule.details}|\n`
105
- : `| ${isRecommended} | ${isFixable} | ${hasSuggestions} | ${underConstruction} |   | [${rule.name}](Rules.md#${rule.name}) | ${rule.details}|\n`
106
- }
107
- })
108
- mdRulesTable = mdRules ? `${mdRulesHeader}${mdRules}\n` : ''
109
- }
110
- return mdRulesTable
111
- }
112
-
113
- /**
114
- * Generates markdown documentation files for:
115
- * - Overview of all rules in form of markdown table (RuleList)
116
- * - List of all rules details in form of markdown page (Rules)
117
- * If used internally within the @sap/eslint-plugin-cds, this
118
- * also generates 'released' files, which only contain information
119
- * on rules published until the currently released version.
120
- * @param rules
121
- * @param docsPath
122
- * @param release
123
- */
124
- function genDocFiles (rules, docsPath, release = false) {
125
- let suffix = ''
126
- if (release) {
127
- suffix = '-released'
128
- }
129
- const ruleDocsPath = path.join(docsPath, `Rules${suffix}.md`)
130
- const ruleListDocsPath = path.join(docsPath, `RuleList${suffix}.md`)
131
-
132
- if (!fs.existsSync(ruleDocsPath)) {
133
- fs.writeFileSync(ruleDocsPath, '', 'utf8')
134
- }
135
- if (!fs.existsSync(ruleListDocsPath)) {
136
- fs.writeFileSync(ruleListDocsPath, '', 'utf8')
137
- }
138
- const mdRulesCur = fs.readFileSync(ruleDocsPath, 'utf8')
139
- const mdRuleListCur = fs.readFileSync(ruleListDocsPath, 'utf8')
140
-
141
- // Get rules table
142
-
143
- const header = '# @sap/eslint-plugin-cds [latest]\n\n'
144
- const mdRuleList = genMdRules(rules, release)
145
-
146
- // Get rule details
147
- let mdRules = ''
148
-
149
- rules.forEach(rule => {
150
- mdRules += `${rule.contents}\n\n${rule.sources}\n\n---\n\n`
151
- })
152
-
153
- if (mdRuleListCur !== mdRuleList || mdRulesCur !== mdRules) {
154
- fs.writeFileSync(ruleDocsPath, header + mdRules, 'utf8')
155
- fs.writeFileSync(ruleListDocsPath, header + mdRuleList, 'utf8')
156
- }
157
- }
158
-
159
- function getPackageVersion (registry) {
160
- let result
161
- try {
162
- result = cp
163
- .execSync(`npm show @sap/eslint-plugin-cds --@sap:registry=${registry} --json`, {
164
- cwd: process.cwd(),
165
- shell: IS_WIN,
166
- stdio: 'pipe'
167
- })
168
- .toString()
169
- } catch (_err) {
170
- LOG?.(`Failed to connect to ${registry} - check your connection and try again.`)
171
- exit(0)
172
- }
173
- const version = JSON.parse(result).version
174
- if (!version) {
175
- LOG?.(`Failed to get latest plugin version from ${registry} - check your connection and try again.`)
176
- exit(0)
177
- }
178
- return version
179
- }
180
-
181
- function getRules (docsPath, rulePath, testPath, versionRequired = '0.0.0', release = false) {
182
- let mdRule, mdRuleSources, mdRuleContents
183
- const rules = []
184
- const ruleVersionsPath = path.join(docsPath, '_data', 'rule_versions.json')
185
- const ruleVersions = require(ruleVersionsPath)
186
- let fileNumber = 0
187
- fs.readdirSync(rulePath).filter((file) => {
188
- if (path.extname(file).toLowerCase() === '.js' && file !== 'index.js') {
189
- const rule = path.basename(file).replace(path.extname(file), '')
190
- const ruleTestPath = path.join(testPath, rule, 'rule.test.js')
191
- fileNumber++
192
-
193
- // Get rule meta information
194
- const ruleMeta = require(path.join(rulePath, file)).meta
195
- let version = ruleVersions.added[rule]
196
- if (!version) {
197
- version = JSON.parse(fs.readFileSync(path.join(__dirname, '../../package.json')).toString()).version
198
- ruleVersions.added[rule] = version
199
- fs.writeFileSync(ruleVersionsPath, JSON.stringify(ruleVersions, null, 4), 'utf8')
200
- }
201
- if ((release && semver.satisfies(version, `<=${versionRequired}`)) || !release) {
202
- LOG?.(`${fileNumber}> preparing docs for ${ruleTestPath}`)
203
-
204
- const details = ruleMeta.docs.description
205
- const flavor = ruleMeta.model ? ruleMeta.model : constants.DEFAULT_RULE_FLAVOR
206
- const category = (flavor === 'none') ? 'Environment' : 'Model Validation'
207
- const fixable = ruleMeta.fixable
208
- const messages = ruleMeta.messages ? ruleMeta.messages : []
209
- const recommended = ruleMeta.docs.recommended
210
- const suggestions = ruleMeta.hasSuggestions
211
-
212
- let underConstruction = ''
213
- if (!release && (version === 'TBD' || semver.satisfies(version, `>${versionRequired}`))) {
214
- underConstruction = '🚧'
215
- LOG?.(` > 🚧 Rule '${rule}' still under construction.\n`)
216
- }
217
-
218
- const isFixable = ['code', 'whitespace'].includes(fixable) ? '🔧' : ''
219
- const isRecommended = recommended === true ? '✔️' : ''
220
- const hasSuggestions = suggestions === true ? '💡' : ''
221
-
222
- const rulesEntry = {
223
- name: rule,
224
- details,
225
- recommended: isRecommended,
226
- fixable: isFixable,
227
- hasSuggestions,
228
- construction: underConstruction,
229
- messages,
230
- version
231
- }
232
- try {
233
- mdRule = getRuleExamples(rule, ruleTestPath, testPath, rulesEntry)
234
- } catch (_err) {
235
- // Just continue
236
- }
237
- mdRuleContents = ''
238
-
239
- mdRuleContents +=
240
- !release && underConstruction
241
- ? `## ${rule}\n<span class='shifted'>${underConstruction}&nbsp;&nbsp;<span class='label'>${category}</span></span>\n\n`
242
- : `## ${rule}\n<span class='shifted label'>${category}</span>\n\n`
243
-
244
- mdRuleContents += `### Rule Details\n${details}\n\n`
245
- if (mdRule) {
246
- mdRuleContents += `### Examples\n${mdRule}\n\n`
247
- }
248
- mdRuleContents += `### Version\nThis rule was introduced in \`@sap/eslint-plugin-cds ${version}\`.\n\n`
249
- mdRuleSources = `### Resources\n[Rule & Documentation source](${path
250
- .relative(docsPath, path.join(rulePath, `${rule}.js`))
251
- .replace(/\\/g, '/')})\n\n`
252
-
253
- rulesEntry.contents = mdRuleContents
254
- rulesEntry.sources = mdRuleSources
255
- rules.push(rulesEntry)
256
- }
257
- }
258
- return undefined
259
- })
260
- return rules
261
- }
262
-
263
- function getRuleExamples (rule, ruleTestPath, testPath, ruleDictEntry) {
264
- // Get rule valid/invalid tests
265
- let mdRule = ''
266
- if (fs.existsSync(ruleTestPath)) {
267
- require(`${ruleTestPath}`)
268
- const testerCases = Cache.get(`testerCases:${rule}`)
269
- const isEnvRule = testerCases.valid[0].filename === '<text>'
270
- const valid = !isEnvRule ? fs.readFileSync(testerCases.valid[0].filename, 'utf8') : JSON.stringify(testerCases.valid[0].options[0].environment, null, 4)
271
- const invalid = !isEnvRule ? fs.readFileSync(testerCases.invalid[0].filename, 'utf8') : JSON.stringify(testerCases.invalid[0].options[0].environment, null, 4)
272
- let validString = ''
273
- let invalidString = ''
274
- if (!isEnvRule) {
275
- const errors = testerCases.invalid[0].errors
276
- let errorsSorted = []
277
- errors.forEach((err) => {
278
- if (errorsSorted.length === 0) {
279
- errorsSorted = [err]
280
- } else {
281
- const errLast = errorsSorted[errorsSorted.length - 1]
282
- if (err.line > errLast.line) {
283
- errorsSorted.push(err)
284
- } else if (err.line < errLast.line) {
285
- errorsSorted.unshift(err)
286
- } else {
287
- if (err.column > errLast.column) {
288
- errorsSorted.push(err)
289
- } else if (err.line < errLast.line) {
290
- errorsSorted.unshift(err)
291
- } else {
292
- if (err.messageId) {
293
- errorsSorted[errorsSorted.length - 1].messageId += '\n' + err.messageId
294
- }
295
- if (err.message) {
296
- errorsSorted[errorsSorted.length - 1].message += '\n' + err.message
297
- }
298
- }
299
- }
300
- }
301
- })
302
- const code = invalid.split('\n')
303
- errorsSorted.forEach((err, i) => {
304
- if (err.messageId && ruleDictEntry.messages) {
305
- let msg = ruleDictEntry.messages[err.messageId]
306
- let data
307
- if (errorsSorted[i].suggestions && errorsSorted[i].suggestions[0]) {
308
- data = errorsSorted[i].suggestions[0].data
309
- }
310
- if (data && msg) {
311
- Object.keys(data).forEach((d) => {
312
- msg = msg.replace(`{{${d}}}`, data[d])
313
- })
314
- }
315
- err.message = msg
316
- }
317
- const msg = err.message && err.message.includes && err.message.includes('"') ? err.message.replace(/"/gm, '`') : err.message
318
- if (err.line) {
319
- const stringStart = errorsSorted[i - 1] ? errorsSorted[i - 1].line : 0
320
- invalidString += code.slice(stringStart, err.line - 1).join('\n')
321
- const errorString = err.line === err.endLine ? code[err.line - 1] : code.slice(err.line - 1, err.endLine - 1).join('\n')
322
- const replacedErrorLine = errorString.substring(0, err.column - 1) +
323
- `<span style="display:inline-block; position:relative; color:red; border-bottom:2pt dotted red" title="${msg}"><b><i>` +
324
- errorString.substring(err.column - 1, err.endColumn - 1) +
325
- '</i></b></span>' + errorString.substring(err.endColumn - 1)
326
- invalidString += '\n' + replacedErrorLine
327
- const stringEnd = errorsSorted[i + 1] ? code.slice(err.line, errorsSorted[i + 1].line - 1).join('\n') : code.slice(err.line).join('\n')
328
- invalidString += errorsSorted[i + 1] ? stringEnd : '\n' + stringEnd
329
- }
330
- })
331
- } else {
332
- invalidString = invalid
333
- }
334
- validString = valid
335
-
336
- mdRule +=
337
- '<span>✔️&nbsp;&nbsp; Example of ' +
338
- '<span style="color:green">correct</span> ' +
339
- `code for this rule:</span>\n\n<pre><code>${validString.trim()}</code></pre>\n\n`
340
- mdRule +=
341
- '<span>❌&nbsp;&nbsp; Example of ' +
342
- '<span style="color:red">incorrect</span> ' +
343
- `code for this rule:</span>\n\n<pre><code>${invalidString.trim()}</code></pre>`
344
- }
345
- return mdRule
346
- }