@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
|
@@ -1,28 +1,36 @@
|
|
|
1
|
-
module.exports =
|
|
1
|
+
module.exports = {
|
|
2
2
|
meta: {
|
|
3
3
|
docs: {
|
|
4
|
-
description:
|
|
4
|
+
description:
|
|
5
|
+
"Ambiguous key with a `TO MANY` relationship since entries could appear multiple times with the same key.",
|
|
5
6
|
category: "Model Validation",
|
|
7
|
+
recommended: true,
|
|
6
8
|
version: "1.0.1",
|
|
7
9
|
},
|
|
10
|
+
severity: "warn",
|
|
8
11
|
type: "problem",
|
|
9
12
|
},
|
|
10
13
|
create(context) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
return { all: check_assoc2many_ambiguous_key };
|
|
15
|
+
|
|
16
|
+
function check_assoc2many_ambiguous_key() {
|
|
17
|
+
let reports = [];
|
|
18
|
+
let csnOdata;
|
|
19
|
+
const m = context.cds.model;
|
|
20
|
+
if (m && m.definitions) {
|
|
15
21
|
csnOdata = context.cds.compile.for.odata(m);
|
|
16
22
|
const csnOdataLinked = context.cds.linked(csnOdata);
|
|
17
|
-
associationCardinalityFlaw(csnOdataLinked, context);
|
|
18
|
-
}
|
|
19
|
-
|
|
23
|
+
reports = associationCardinalityFlaw(csnOdataLinked, context);
|
|
24
|
+
}
|
|
25
|
+
if (reports.length > 0) {
|
|
26
|
+
return reports;
|
|
20
27
|
}
|
|
21
28
|
}
|
|
22
29
|
},
|
|
23
|
-
}
|
|
30
|
+
};
|
|
24
31
|
|
|
25
32
|
function associationCardinalityFlaw(csn, context) {
|
|
33
|
+
const reports = [];
|
|
26
34
|
processEntity(csn, (definition, sourceEntity, sourceAlias) => {
|
|
27
35
|
let refCardinalityMult = false;
|
|
28
36
|
let refPlainElement = false;
|
|
@@ -36,10 +44,7 @@ function associationCardinalityFlaw(csn, context) {
|
|
|
36
44
|
refPlainElement = false;
|
|
37
45
|
},
|
|
38
46
|
(refEntity, refElement) => {
|
|
39
|
-
if (
|
|
40
|
-
refElement.type === "cds.Association" ||
|
|
41
|
-
refElement.type === "cds.Composition"
|
|
42
|
-
) {
|
|
47
|
+
if (refElement.type === "cds.Association" || refElement.type === "cds.Composition") {
|
|
43
48
|
if (refElement.cardinality && refElement.cardinality.max === "*") {
|
|
44
49
|
refCardinalityMult = true;
|
|
45
50
|
}
|
|
@@ -55,20 +60,35 @@ function associationCardinalityFlaw(csn, context) {
|
|
|
55
60
|
refCardinalityMult &&
|
|
56
61
|
refPlainElement
|
|
57
62
|
) {
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
63
|
+
const keyName = Object.keys(definition.keys)[0];
|
|
64
|
+
const key = definition.keys[keyName];
|
|
65
|
+
const keyLoc = context.cds.getLocation(keyName, key, csn);
|
|
66
|
+
const colName = column.as ? column.as : column.name;
|
|
67
|
+
if (context.sourcecode.lines[column.$location.line - 1]) {
|
|
68
|
+
const endCol = context.sourcecode.lines[column.$location.col - 1].length;
|
|
69
|
+
const colLoc = {
|
|
70
|
+
start: { line: column.$location.line, column: column.$location.col - 1 },
|
|
71
|
+
end: { line: column.$location.line, column: endCol },
|
|
72
|
+
};
|
|
73
|
+
const message = `Ambiguous key in '${definition.name}'. Element '${colName}' leads to multiple entries so that key '${keyName}' is not unique.`;
|
|
74
|
+
reports.push(
|
|
75
|
+
{
|
|
76
|
+
message,
|
|
77
|
+
loc: keyLoc,
|
|
78
|
+
file: key.$location.file,
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
message,
|
|
82
|
+
loc: colLoc,
|
|
83
|
+
file: column.$location.file,
|
|
84
|
+
}
|
|
85
|
+
);
|
|
86
|
+
}
|
|
68
87
|
}
|
|
69
88
|
}
|
|
70
89
|
);
|
|
71
90
|
});
|
|
91
|
+
return reports;
|
|
72
92
|
}
|
|
73
93
|
|
|
74
94
|
function processEntity(csn, eachCallback) {
|
|
@@ -87,21 +107,14 @@ function processEntity(csn, eachCallback) {
|
|
|
87
107
|
const sourceAlias = [];
|
|
88
108
|
if (definition.query.SELECT.from.ref) {
|
|
89
109
|
// From
|
|
90
|
-
sourceEntity =
|
|
91
|
-
csn.definitions[definition.query.SELECT.from.ref.join("_")];
|
|
110
|
+
sourceEntity = csn.definitions[definition.query.SELECT.from.ref.join("_")];
|
|
92
111
|
sourceAlias.push({
|
|
93
112
|
from: sourceEntity.name,
|
|
94
|
-
as:
|
|
95
|
-
definition.query.SELECT.from.as ||
|
|
96
|
-
definition.query.SELECT.from.ref.slice(-1)[0].split(".").pop(),
|
|
113
|
+
as: definition.query.SELECT.from.as || definition.query.SELECT.from.ref.slice(-1)[0].split(".").pop(),
|
|
97
114
|
});
|
|
98
|
-
} else if (
|
|
99
|
-
definition.query.SELECT.from.args &&
|
|
100
|
-
definition.query.SELECT.from.args[0].ref
|
|
101
|
-
) {
|
|
115
|
+
} else if (definition.query.SELECT.from.args && definition.query.SELECT.from.args[0].ref) {
|
|
102
116
|
// Join
|
|
103
|
-
sourceEntity =
|
|
104
|
-
csn.definitions[definition.query.SELECT.from.args[0].ref.join("_")];
|
|
117
|
+
sourceEntity = csn.definitions[definition.query.SELECT.from.args[0].ref.join("_")];
|
|
105
118
|
definition.query.SELECT.from.args.forEach((arg) => {
|
|
106
119
|
sourceAlias.push({
|
|
107
120
|
from: arg.ref.join("_"),
|
|
@@ -117,15 +130,7 @@ function processEntity(csn, eachCallback) {
|
|
|
117
130
|
});
|
|
118
131
|
}
|
|
119
132
|
|
|
120
|
-
function processElement(
|
|
121
|
-
csn,
|
|
122
|
-
definition,
|
|
123
|
-
sourceEntity,
|
|
124
|
-
sourceAlias,
|
|
125
|
-
beforeCallback,
|
|
126
|
-
eachCallback,
|
|
127
|
-
afterCallback
|
|
128
|
-
) {
|
|
133
|
+
function processElement(csn, definition, sourceEntity, sourceAlias, beforeCallback, eachCallback, afterCallback) {
|
|
129
134
|
definition.query.SELECT.columns.forEach((column) => {
|
|
130
135
|
if (column.ref && column.ref.length > 1) {
|
|
131
136
|
let refEntity = sourceEntity;
|
|
@@ -148,18 +153,12 @@ function processElement(
|
|
|
148
153
|
if (!refElement && definition.query.SELECT.mixin) {
|
|
149
154
|
refElement = definition.query.SELECT.mixin[ref];
|
|
150
155
|
if (!refElement && definition.query.SELECT.mixin[column.ref[0]]) {
|
|
151
|
-
refElement =
|
|
152
|
-
definition.query.SELECT.mixin[column.ref[0]]._target.elements[
|
|
153
|
-
ref
|
|
154
|
-
];
|
|
156
|
+
refElement = definition.query.SELECT.mixin[column.ref[0]]._target.elements[ref];
|
|
155
157
|
}
|
|
156
158
|
}
|
|
157
159
|
}
|
|
158
160
|
eachCallback(refEntity, refElement);
|
|
159
|
-
if (
|
|
160
|
-
refElement.type === "cds.Association" ||
|
|
161
|
-
refElement.type === "cds.Composition"
|
|
162
|
-
) {
|
|
161
|
+
if (refElement.type === "cds.Association" || refElement.type === "cds.Composition") {
|
|
163
162
|
refEntity = csn.definitions[refElement.target];
|
|
164
163
|
}
|
|
165
164
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const cp = require("child_process");
|
|
2
|
+
const semver = require("semver");
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
meta: {
|
|
6
|
+
docs: {
|
|
7
|
+
description: "Checks whether the latest `@sap/cds` version is being used.",
|
|
8
|
+
category: "Environment",
|
|
9
|
+
version: "1.0.4",
|
|
10
|
+
},
|
|
11
|
+
type: "suggestion",
|
|
12
|
+
hasSuggestions: true,
|
|
13
|
+
messages: {
|
|
14
|
+
latestCDSVersion: `A newer CDS version is available!`,
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
create: function (context) {
|
|
18
|
+
return { all: check_latest_cds_version };
|
|
19
|
+
|
|
20
|
+
function check_latest_cds_version() {
|
|
21
|
+
let cdsVersions;
|
|
22
|
+
let e = context.cds.environment;
|
|
23
|
+
if (!e) {
|
|
24
|
+
e = cp
|
|
25
|
+
.execSync(`npm outdated @sap/cds --json`, {
|
|
26
|
+
cwd: process.cwd(),
|
|
27
|
+
stdio: "pipe",
|
|
28
|
+
})
|
|
29
|
+
.toString();
|
|
30
|
+
}
|
|
31
|
+
if (e && e["@sap/cds"]) {
|
|
32
|
+
cdsVersions = e["@sap/cds"];
|
|
33
|
+
// If current cds version is not the latest
|
|
34
|
+
if (Object.keys(cdsVersions).length !== 0 && !semver.satisfies(cdsVersions.latest, cdsVersions.current)) {
|
|
35
|
+
return {
|
|
36
|
+
messageId: "latestCDSVersion",
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const path = require("path");
|
|
2
|
+
const semver = require("semver");
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
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
|
+
return { all: check_min_node_version }
|
|
17
|
+
|
|
18
|
+
function check_min_node_version() {
|
|
19
|
+
const e = context.cds.environment;
|
|
20
|
+
let nodeVersion, nodeVersionCDS;
|
|
21
|
+
if (!e) {
|
|
22
|
+
// Get current and required node versions
|
|
23
|
+
try {
|
|
24
|
+
const CDSPath = require.resolve("@sap/cds/package.json", {
|
|
25
|
+
paths: [path.dirname(context.filePath)],
|
|
26
|
+
});
|
|
27
|
+
const jsonCDS = require(CDSPath);
|
|
28
|
+
nodeVersion = process.version;
|
|
29
|
+
nodeVersionCDS = jsonCDS.engines.node;
|
|
30
|
+
} catch (err) {
|
|
31
|
+
// Do not throw
|
|
32
|
+
}
|
|
33
|
+
} else {
|
|
34
|
+
nodeVersion = e.nodeVersion;
|
|
35
|
+
nodeVersionCDS = e.nodeVersionCDS;
|
|
36
|
+
}
|
|
37
|
+
if (
|
|
38
|
+
nodeVersion &&
|
|
39
|
+
nodeVersionCDS &&
|
|
40
|
+
!semver.satisfies(nodeVersion, nodeVersionCDS, { loose: true })
|
|
41
|
+
) {
|
|
42
|
+
return `CDS minimum node version of ${nodeVersionCDS} required, found ${nodeVersion}!`;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
},
|
|
47
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// TODO: TEST API
|
|
2
|
+
//module.exports = require("../../api").createRule({ // TODO: eliminate that and allow std eslint style module.export = { ...
|
|
3
|
+
module.exports = {
|
|
4
|
+
meta: {
|
|
5
|
+
docs: {
|
|
6
|
+
description: `Avoid using reserved SQL keywords.`,
|
|
7
|
+
category: "Model Validation", //> values are specific for cds lint
|
|
8
|
+
recommended: true,
|
|
9
|
+
version: "2.1.0",
|
|
10
|
+
},
|
|
11
|
+
severity: "error" //> convenience option by cds.lint
|
|
12
|
+
},
|
|
13
|
+
create(context) {
|
|
14
|
+
const { cds } = context //> specific for cds lint
|
|
15
|
+
const { db = { kind: "sql" } } = cds.env.requires
|
|
16
|
+
|
|
17
|
+
return { //> return standard eslint visitor callbacks registered to CSN kinds, types, ...
|
|
18
|
+
entity: check_name_is_not_reserved,
|
|
19
|
+
element: check_name_is_not_reserved
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function check_name_is_not_reserved(d) {
|
|
23
|
+
if (d.name in RESERVED) {
|
|
24
|
+
// Do not blame in case of external services
|
|
25
|
+
let srv = d._service || (d.parent && d.parent._service)
|
|
26
|
+
if (srv && srv["@cds.external"]) return
|
|
27
|
+
|
|
28
|
+
// Do blame
|
|
29
|
+
return `'${d.name}' is a reserved keyword in ${db.kind.toUpperCase()}` //> convenience options by cds lint
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// REVISIT: Replace by compiler-provided check
|
|
36
|
+
const RESERVED = {
|
|
37
|
+
ORDER: 1,
|
|
38
|
+
Order: 1,
|
|
39
|
+
order: 1,
|
|
40
|
+
GROUP: 1,
|
|
41
|
+
Group: 1,
|
|
42
|
+
group: 1,
|
|
43
|
+
LIMIT: 1,
|
|
44
|
+
Limit: 1,
|
|
45
|
+
limit: 1,
|
|
46
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
meta: {
|
|
3
|
+
docs: {
|
|
4
|
+
description: `Names must not start with $ to avoid possible shadowing of reserved variables.`,
|
|
5
|
+
category: "Model Validation",
|
|
6
|
+
recommended: true,
|
|
7
|
+
version: "2.3.3",
|
|
8
|
+
},
|
|
9
|
+
severity: "warn",
|
|
10
|
+
},
|
|
11
|
+
create(context) {
|
|
12
|
+
return { element: _check };
|
|
13
|
+
|
|
14
|
+
function _check(d) {
|
|
15
|
+
|
|
16
|
+
// Do not blame in case of external services
|
|
17
|
+
let srv = d._service || (d.parent && d.parent._service);
|
|
18
|
+
if (srv && srv["@cds.external"]) return;
|
|
19
|
+
|
|
20
|
+
const results = [];
|
|
21
|
+
for (const m in context.cds.model.messages) {
|
|
22
|
+
const msg = context.cds.model.messages[m];
|
|
23
|
+
if (msg.messageId === "syntax-dollar-ident") {
|
|
24
|
+
results.push({
|
|
25
|
+
message: msg.message,
|
|
26
|
+
loc: {
|
|
27
|
+
start: { line: msg.location.line, column: msg.location.col - 1 },
|
|
28
|
+
end: {
|
|
29
|
+
line: msg.location.endLine,
|
|
30
|
+
column: !msg.location.endCol
|
|
31
|
+
? context.sourcecode.lines[msg.location.line - 1].length
|
|
32
|
+
: msg.location.endCol - 1,
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
file: msg.location.file,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (d.name.startsWith("$")) {
|
|
41
|
+
// Do blame
|
|
42
|
+
results.push(`“${d.name}” is prefixed with a dollar sign ($)`);
|
|
43
|
+
}
|
|
44
|
+
return results.length > 0 ? results : undefined;
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
};
|
|
@@ -1,35 +1,40 @@
|
|
|
1
|
-
module.exports =
|
|
1
|
+
module.exports = {
|
|
2
2
|
meta: {
|
|
3
3
|
docs: {
|
|
4
4
|
description: `Draft-enabled entities shall not be used in views that make use of \`JOIN\`.`,
|
|
5
5
|
category: "Model Validation",
|
|
6
|
+
recommended: true,
|
|
6
7
|
version: "2.2.1",
|
|
7
8
|
},
|
|
9
|
+
severity: "warn",
|
|
8
10
|
type: "suggestion",
|
|
9
11
|
messages: {
|
|
10
12
|
noJoinOnDraftEnabledEntities: `Do not use draft-enabled entities in views that make use of \`JOIN\`.`,
|
|
11
13
|
},
|
|
12
14
|
},
|
|
13
15
|
create: function (context) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
|
|
17
|
+
return { entity: check_nojoin_draftenabled }
|
|
18
|
+
|
|
19
|
+
function check_nojoin_draftenabled(e) {
|
|
20
|
+
if (e["@odata.draft.enabled"]) {
|
|
21
|
+
if (e.query.SELECT.from.join) {
|
|
22
|
+
const location = e.query.$location;
|
|
19
23
|
if (context.sourcecode.lines[location.line - 1]) {
|
|
20
24
|
const endCol = context.sourcecode.lines[location.line - 1].length;
|
|
21
25
|
const loc = {
|
|
22
26
|
start: { line: location.line, column: location.col - 1 },
|
|
23
27
|
end: { line: location.line, column: endCol },
|
|
24
28
|
};
|
|
25
|
-
|
|
29
|
+
return {
|
|
26
30
|
messageId: "noJoinOnDraftEnabledEntities",
|
|
27
31
|
loc,
|
|
28
|
-
file:
|
|
29
|
-
}
|
|
32
|
+
file: e.$location.file,
|
|
33
|
+
};
|
|
30
34
|
}
|
|
31
35
|
}
|
|
32
36
|
}
|
|
33
|
-
}
|
|
37
|
+
}
|
|
38
|
+
|
|
34
39
|
},
|
|
35
|
-
}
|
|
40
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module.exports = {
|
|
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
|
+
|
|
14
|
+
return { element: check_2many_oncond }
|
|
15
|
+
|
|
16
|
+
function check_2many_oncond(e) {
|
|
17
|
+
if (e.is2many && !e.on && typeof e.target === 'string') {
|
|
18
|
+
const loc = context.cds.getLocation(e.name, e);
|
|
19
|
+
return {
|
|
20
|
+
message: `You must provide an \`ON\` condition for \`TO MANY\` relationship '${e.name}'.`,
|
|
21
|
+
loc,
|
|
22
|
+
file: e.parent.$location.file,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/* eslint-disable no-undef */
|
|
2
|
+
module.exports = {
|
|
3
|
+
meta: {
|
|
4
|
+
docs: {
|
|
5
|
+
description: "Should make suggestions for possible missing SQL casts.",
|
|
6
|
+
category: "Model Validation",
|
|
7
|
+
recommended: true,
|
|
8
|
+
version: "1.0.8",
|
|
9
|
+
},
|
|
10
|
+
severity: "warn",
|
|
11
|
+
type: "suggestion",
|
|
12
|
+
hasSuggestions: true,
|
|
13
|
+
messages: {
|
|
14
|
+
missingSQLCast: "Potential issue - Missing SQL cast for column expression?",
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
create: function (context) {
|
|
18
|
+
return { view: check_sql_cast };
|
|
19
|
+
|
|
20
|
+
function check_sql_cast(v) {
|
|
21
|
+
const reports = [];
|
|
22
|
+
if (v.query && 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].cast) {
|
|
29
|
+
continue;
|
|
30
|
+
} else {
|
|
31
|
+
if (context.sourcecode.lines[location.line - 1]) {
|
|
32
|
+
const endCol = context.sourcecode.lines[location.line - 1].length;
|
|
33
|
+
const loc = {
|
|
34
|
+
start: { line: location.line, column: location.col - 1 },
|
|
35
|
+
end: { line: location.line, column: endCol },
|
|
36
|
+
};
|
|
37
|
+
reports.push({
|
|
38
|
+
messageId: "missingSQLCast",
|
|
39
|
+
loc,
|
|
40
|
+
file: location.file,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (reports.length > 0 ) return reports;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
},
|
|
52
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
module.exports = {
|
|
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: "Element name '{{entityName}}.{{elementName}}' should start with a lowercase letter.",
|
|
12
|
+
fixLowercase: "Start element name with a lowercase letter.",
|
|
13
|
+
},
|
|
14
|
+
fixable: "code",
|
|
15
|
+
},
|
|
16
|
+
create: function (context) {
|
|
17
|
+
const { cds, filePath, sourcecode } = context;
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
element: check_start_lowercase,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function check_start_lowercase(e) {
|
|
24
|
+
if (!filePath.endsWith(".cds")) return;
|
|
25
|
+
const elementName = e.name;
|
|
26
|
+
const entityName = e.parent.name;
|
|
27
|
+
if (elementName && !(entityName.startsWith("localized") || entityName.endsWith("texts"))) {
|
|
28
|
+
if (elementName.charAt(0) !== elementName.charAt(0).toLowerCase() && !["ID"].includes(elementName)) {
|
|
29
|
+
if (e.$location && e.$location.file) {
|
|
30
|
+
const file = e.$location.file;
|
|
31
|
+
const loc = cds.getLocation(elementName, e);
|
|
32
|
+
const fix = (fixer) => {
|
|
33
|
+
const elementNameSanitized = elementName.charAt(0).toLowerCase() + elementName.slice(1);
|
|
34
|
+
const rangeEnd = sourcecode.getIndexFromLoc({
|
|
35
|
+
line: loc.end.line,
|
|
36
|
+
column: loc.end.column,
|
|
37
|
+
});
|
|
38
|
+
const rangeBeg = rangeEnd ? rangeEnd - elementNameSanitized.length : 0;
|
|
39
|
+
return fixer.replaceTextRange([rangeBeg, rangeEnd], elementNameSanitized);
|
|
40
|
+
};
|
|
41
|
+
return {
|
|
42
|
+
messageId: "startLowercase",
|
|
43
|
+
loc,
|
|
44
|
+
file,
|
|
45
|
+
data: {
|
|
46
|
+
entityName,
|
|
47
|
+
elementName,
|
|
48
|
+
},
|
|
49
|
+
suggest: [
|
|
50
|
+
{
|
|
51
|
+
messageId: "fixLowercase",
|
|
52
|
+
fix,
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const { splitEntityName } = require("../utils/ruleHelpers");
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
meta: {
|
|
5
|
+
docs: {
|
|
6
|
+
description: "Regular entity names should start with uppercase letters.",
|
|
7
|
+
category: "Model Validation",
|
|
8
|
+
version: "1.0.4",
|
|
9
|
+
},
|
|
10
|
+
type: "suggestion",
|
|
11
|
+
hasSuggestions: true,
|
|
12
|
+
messages: {
|
|
13
|
+
startUppercase: "Entity name '{{entityName}}' should start with an uppercase letter.",
|
|
14
|
+
fixUppercase: "Start entity name with an uppercase letter.",
|
|
15
|
+
},
|
|
16
|
+
fixable: "code",
|
|
17
|
+
},
|
|
18
|
+
create: function (context) {
|
|
19
|
+
const { cds, filePath, sourcecode } = context;
|
|
20
|
+
|
|
21
|
+
return { entity: check_starts_uppercase };
|
|
22
|
+
|
|
23
|
+
function check_starts_uppercase(e) {
|
|
24
|
+
if (!filePath.endsWith(".cds")) return;
|
|
25
|
+
const entityName = splitEntityName(e).entity;
|
|
26
|
+
if (entityName.charAt(0) !== entityName.charAt(0).toUpperCase()) {
|
|
27
|
+
if (e.$location && e.$location.file) {
|
|
28
|
+
const file = e.$location.file;
|
|
29
|
+
const loc = cds.getLocation(entityName, e);
|
|
30
|
+
const fix = (fixer) => {
|
|
31
|
+
const entityNameSanitized = entityName.charAt(0).toUpperCase() + entityName.slice(1);
|
|
32
|
+
const rangeEnd = sourcecode.getIndexFromLoc({
|
|
33
|
+
line: loc.end.line,
|
|
34
|
+
column: loc.end.column,
|
|
35
|
+
});
|
|
36
|
+
const rangeBeg = rangeEnd ? rangeEnd - entityNameSanitized.length : 0;
|
|
37
|
+
return fixer.replaceTextRange([rangeBeg, rangeEnd], entityNameSanitized);
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
messageId: "startUppercase",
|
|
41
|
+
loc,
|
|
42
|
+
file,
|
|
43
|
+
data: { entityName },
|
|
44
|
+
suggest: [
|
|
45
|
+
{
|
|
46
|
+
messageId: "fixUppercase",
|
|
47
|
+
fix,
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
};
|