Skip to content

Commit

Permalink
refactor(tooling-general): update custom header check precommit (#588)
Browse files Browse the repository at this point in the history
* refactor: modify license check rule

* fix: improve rule

* fix: remove newline from modification comment

---------

Co-authored-by: Bran <[email protected]>
  • Loading branch information
VmMad and brancoder authored Jun 7, 2024
1 parent a502568 commit 24f438a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
10 changes: 8 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) Mysten Labs, Inc.
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

module.exports = {
Expand Down Expand Up @@ -68,8 +69,13 @@ module.exports = {
2,
'line',
[
{ pattern: ' Copyright \\(c\\) (2024 IOTA Stiftung|Mysten Labs, Inc.)' },
' SPDX-License-Identifier: Apache-2.0',
{
pattern: ' Copyright \\(c\\) (2024 IOTA Stiftung|Mysten Labs, Inc.)?',
},
{
pattern:
' ((SPDX-License-Identifier: Apache-2.0)|(Modifications Copyright \\(c\\) 2024 IOTA Stiftung))',
},
],
],
'@typescript-eslint/no-unused-vars': [
Expand Down
17 changes: 7 additions & 10 deletions linting/license-check/rules/license-check.rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const MISSING_HEADER_MESSAGE = 'Missing or incorrect license header.';
const MISSING_MODIFICATION_MESSAGE = 'Add modification notice to the license header.';

const IOTA_LICENSE_HEADER = `// ${IOTA_COPYRIGHT_HEADER}\n// ${LICENSE_IDENTIFIER}\n\n`;
const MODIFICATION_HEADER = `\n\n// ${MODIFICATION_COPYRIGHT_HEADER}\n// ${LICENSE_IDENTIFIER}\n\n`;

function checkHeader(node, context) {
const sourceCode = context.getSourceCode();
Expand All @@ -19,14 +18,9 @@ function checkHeader(node, context) {

const hasIotaCopyrightHeader = firstComment?.includes(IOTA_COPYRIGHT_HEADER);
const hasOldCopyrightHeader = firstComment?.includes(OLD_COPYRIGHT_HEADER);
const hasLicenseIdentifier = comments?.[1]?.value.includes(LICENSE_IDENTIFIER);

// Check if the file has any license header.
if (
(!hasIotaCopyrightHeader && !hasOldCopyrightHeader) ||
!firstComment ||
!hasLicenseIdentifier
) {
if ((!hasIotaCopyrightHeader && !hasOldCopyrightHeader) || !firstComment) {
context.report({
node,
message: MISSING_HEADER_MESSAGE,
Expand All @@ -37,13 +31,16 @@ function checkHeader(node, context) {

// Check if the file has the old copyright notice and has the modification header.
} else if (firstComment.includes(OLD_COPYRIGHT_HEADER)) {
const hasModificationNotice = comments[2]?.value?.includes(MODIFICATION_COPYRIGHT_HEADER);
const hasModificationNotice = comments[1]?.value?.includes(MODIFICATION_COPYRIGHT_HEADER);
if (!hasModificationNotice) {
context.report({
node: comments[1],
node: comments[0],
message: MISSING_MODIFICATION_MESSAGE,
fix(fixer) {
return fixer.insertTextAfter(comments[1], MODIFICATION_HEADER);
return fixer.insertTextAfter(
comments[0],
`\n// ${MODIFICATION_COPYRIGHT_HEADER}`,
);
},
});
}
Expand Down

0 comments on commit 24f438a

Please sign in to comment.