Skip to content

Commit 067e8ef

Browse files
authored
add no bad gdpr comments plugin (#24884)
create plugin so GPDR comments that are incorrect error on linting.
1 parent e71bfd6 commit 067e8ef

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

.eslintplugin/no-bad-gdpr-comment.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"use strict";
2+
/*---------------------------------------------------------------------------------------------
3+
* Copyright (c) Microsoft Corporation. All rights reserved.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*--------------------------------------------------------------------------------------------*/
6+
Object.defineProperty(exports, "__esModule", { value: true });
7+
var noBadGDPRComment = {
8+
create: function (context) {
9+
var _a;
10+
return _a = {},
11+
_a['Program'] = function (node) {
12+
for (var _i = 0, _a = node.comments; _i < _a.length; _i++) {
13+
var comment = _a[_i];
14+
if (comment.type !== 'Block' || !comment.loc) {
15+
continue;
16+
}
17+
if (!comment.value.includes('__GDPR__')) {
18+
continue;
19+
}
20+
var dataStart = comment.value.indexOf('\n');
21+
var data = comment.value.substring(dataStart);
22+
var gdprData = void 0;
23+
try {
24+
var jsonRaw = "{ ".concat(data, " }");
25+
gdprData = JSON.parse(jsonRaw);
26+
}
27+
catch (e) {
28+
context.report({
29+
loc: { start: comment.loc.start, end: comment.loc.end },
30+
message: 'GDPR comment is not valid JSON',
31+
});
32+
}
33+
if (gdprData) {
34+
var len = Object.keys(gdprData).length;
35+
if (len !== 1) {
36+
context.report({
37+
loc: { start: comment.loc.start, end: comment.loc.end },
38+
message: "GDPR comment must contain exactly one key, not ".concat(Object.keys(gdprData).join(', ')),
39+
});
40+
}
41+
}
42+
}
43+
},
44+
_a;
45+
},
46+
};
47+
module.exports = {
48+
rules: {
49+
'no-bad-gdpr-comment': noBadGDPRComment, // Ensure correct structure
50+
},
51+
};

.eslintplugin/no-bad-gdpr-comment.ts

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

eslint.config.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import noOnlyTests from 'eslint-plugin-no-only-tests';
1111
import prettier from 'eslint-config-prettier';
1212
import importPlugin from 'eslint-plugin-import';
1313
import js from '@eslint/js';
14+
import noBadGdprCommentPlugin from './.eslintplugin/no-bad-gdpr-comment.js'; // Ensure the path is correct
1415

1516
export default [
1617
{
@@ -273,6 +274,7 @@ export default [
273274
'no-only-tests': noOnlyTests,
274275
import: importPlugin,
275276
prettier: prettier,
277+
'no-bad-gdpr-comment': noBadGdprCommentPlugin, // Register your plugin
276278
},
277279
settings: {
278280
'import/resolver': {
@@ -282,6 +284,7 @@ export default [
282284
},
283285
},
284286
rules: {
287+
'no-bad-gdpr-comment/no-bad-gdpr-comment': 'warn', // Enable your rule
285288
// Base configurations
286289
...tseslint.configs.recommended.rules,
287290
...prettier.rules,

0 commit comments

Comments
 (0)