Skip to content

Add no bad gdpr comments eslint plugin #24892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .eslintplugin/no-bad-gdpr-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
var noBadGDPRComment = {
create: function (context) {
var _a;
return _a = {},
_a['Program'] = function (node) {
for (var _i = 0, _a = node.comments; _i < _a.length; _i++) {
var comment = _a[_i];
if (comment.type !== 'Block' || !comment.loc) {
continue;
}
if (!comment.value.includes('__GDPR__')) {
continue;
}
var dataStart = comment.value.indexOf('\n');
var data = comment.value.substring(dataStart);
var gdprData = void 0;
try {
var jsonRaw = "{ ".concat(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) {
var 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 ".concat(Object.keys(gdprData).join(', ')),
});
}
}
}
},
_a;
},
};
module.exports = {
rules: {
'no-bad-gdpr-comment': noBadGDPRComment, // Ensure correct structure
},
};
55 changes: 55 additions & 0 deletions .eslintplugin/no-bad-gdpr-comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
},
};
3 changes: 3 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import noOnlyTests from 'eslint-plugin-no-only-tests';
import prettier from 'eslint-config-prettier';
import importPlugin from 'eslint-plugin-import';
import js from '@eslint/js';
import noBadGdprCommentPlugin from './.eslintplugin/no-bad-gdpr-comment.js';

export default [
{
Expand Down Expand Up @@ -273,6 +274,7 @@ export default [
'no-only-tests': noOnlyTests,
import: importPlugin,
prettier: prettier,
'no-bad-gdpr-comment': noBadGdprCommentPlugin,
},
settings: {
'import/resolver': {
Expand All @@ -282,6 +284,7 @@ export default [
},
},
rules: {
'no-bad-gdpr-comment/no-bad-gdpr-comment': 'error',
// Base configurations
...tseslint.configs.recommended.rules,
...prettier.rules,
Expand Down