-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tooling-general): add pre-commit hook to automatically add missi…
…ng license headers (#416) * feat: add pre-commit hook to automatically add missing license headers * refactor: improve code * refactor(general): Update lockfile --------- Co-authored-by: marc2332 <[email protected]> Co-authored-by: cpl121 <[email protected]>
- Loading branch information
1 parent
21c583f
commit caa0430
Showing
8 changed files
with
2,787 additions
and
2,013 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pnpm exec lint-staged --allow-empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"{sdk,apps}/**/*.{ts,js,mjs,tsx}": "eslint -c ./linting/.eslintrc.precommit.js --fix" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright (c) 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
module.exports = { | ||
plugins: ['license-check'], | ||
rules: { | ||
'license-check/license-check': 'error', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "eslint-plugin-license-check", | ||
"version": "1.0.0", | ||
"main": "plugin.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (c) 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
const licenseCheck = require('./rules/license-check.rule'); | ||
|
||
module.exports = { | ||
rules: { | ||
'license-check': licenseCheck, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
const IOTA_COPYRIGHT_HEADER = 'Copyright (c) 2024 IOTA Stiftung'; | ||
const OLD_COPYRIGHT_HEADER = 'Copyright (c) Mysten Labs, Inc.'; | ||
const MODIFICATION_COPYRIGHT_HEADER = 'Modifications Copyright (c) 2024 IOTA Stiftung'; | ||
const LICENSE_IDENTIFIER = 'SPDX-License-Identifier: Apache-2.0'; | ||
|
||
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(); | ||
const comments = sourceCode.getAllComments(); | ||
const firstComment = comments?.[0]?.value; | ||
|
||
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 | ||
) { | ||
context.report({ | ||
node, | ||
message: MISSING_HEADER_MESSAGE, | ||
fix(fixer) { | ||
return fixer.insertTextBeforeRange([0, 0], IOTA_LICENSE_HEADER); | ||
}, | ||
}); | ||
|
||
// 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); | ||
if (!hasModificationNotice) { | ||
context.report({ | ||
node: comments[1], | ||
message: MISSING_MODIFICATION_MESSAGE, | ||
fix(fixer) { | ||
return fixer.insertTextAfter(comments[1], MODIFICATION_HEADER); | ||
}, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Check and fix license header', | ||
category: 'Stylistic Issues', | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
}, | ||
create(context) { | ||
return { | ||
Program(node) { | ||
checkHeader(node, context); | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.