forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathno-bad-gdpr-comment.ts
More file actions
55 lines (49 loc) · 2.19 KB
/
no-bad-gdpr-comment.ts
File metadata and controls
55 lines (49 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as eslint from 'eslint';
const noBadGDPRComment: eslint.Rule.RuleModule = {
create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener {
return {
['Program'](node) {
for (const comment of (node as eslint.AST.Program).comments) {
if (comment.type !== 'Block' || !comment.loc) {
continue;
}
if (!comment.value.includes('__GDPR__')) {
continue;
}
const dataStart = comment.value.indexOf('\n');
const data = comment.value.substring(dataStart);
let gdprData: { [key: string]: object } | undefined;
try {
const jsonRaw = `{ ${data} }`;
gdprData = JSON.parse(jsonRaw);
} catch (e) {
context.report({
loc: { start: comment.loc.start, end: comment.loc.end },
message: 'GDPR comment is not valid JSON',
});
}
if (gdprData) {
const len = Object.keys(gdprData).length;
if (len !== 1) {
context.report({
loc: { start: comment.loc.start, end: comment.loc.end },
message: `GDPR comment must contain exactly one key, not ${Object.keys(gdprData).join(
', ',
)}`,
});
}
}
}
},
};
},
};
module.exports = {
rules: {
'no-bad-gdpr-comment': noBadGDPRComment, // Ensure correct structure
},
};