Add license-comments eslint rule

This commit is contained in:
Jamie Kyle
2025-08-11 12:55:09 -07:00
committed by GitHub
parent 00bdba0d62
commit 4fc9793cae
4 changed files with 76 additions and 1 deletions

View File

@@ -0,0 +1,73 @@
// Copyright 2025 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
const COMMENT_LINE_1_EXACT = /^ Copyright \d{4} Signal Messenger, LLC$/;
const COMMENT_LINE_2_EXACT = /^ SPDX-License-Identifier: AGPL-3.0-only$/;
const COMMENT_LINE_1_LOOSE = /Copyright (\d{4}) Signal Messenger, LLC/;
const COMMENT_LINE_2_LOOSE = /SPDX-License-Identifier: AGPL-3.0-only/;
/** @type {import("eslint").Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
hasSuggestions: false,
fixable: true,
schema: [],
},
create(context) {
return {
Program(node) {
let comment1 = node.comments.at(0);
let comment2 = node.comments.at(1);
if (
comment1?.type === 'Line' &&
comment2?.type === 'Line' &&
COMMENT_LINE_1_EXACT.test(comment1.value) &&
COMMENT_LINE_2_EXACT.test(comment2.value)
) {
return;
}
context.report({
node,
message: 'Missing license comment',
fix(fixer) {
let year = null;
let remove = [];
for (let comment of node.comments) {
let match1 = comment.value.match(COMMENT_LINE_1_LOOSE);
let match2 = comment.value.match(COMMENT_LINE_2_LOOSE);
if (match1 != null) {
year = match1[1];
}
if (match1 != null || match2 != null) {
remove.push(comment);
}
}
year ??= new Date().getFullYear().toString();
let insert =
`// Copyright ${year} Signal Messenger, LLC\n` +
'// SPDX-License-Identifier: AGPL-3.0-only\n';
return [
fixer.replaceTextRange([0, 0], insert),
...remove.map(comment => {
return fixer.replaceTextRange(
[comment.range[0], comment.range[1]],
''
);
}),
];
},
});
},
};
},
};

View File

@@ -179,6 +179,7 @@ const rules = {
additionalHooks: '^(useSpring|useSprings)$',
},
],
'local-rules/license-comments': 'error',
};
const typescriptRules = {

View File

@@ -3,5 +3,6 @@
/* eslint-disable global-require */
module.exports = {
'license-comments': require('./.eslint/rules/license-comments'),
'type-alias-readonlydeep': require('./.eslint/rules/type-alias-readonlydeep'),
};

View File

@@ -1,5 +1,5 @@
// Copyright 2019 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-onlyå
// SPDX-License-Identifier: AGPL-3.0-only
import React, { memo } from 'react';
import { useSelector } from 'react-redux';
import { MessageSearchResult } from '../../components/conversationList/MessageSearchResult';