From d6d098430943c169f364b920f6c81d5802b22305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmetcan=20Y=C4=B1lmaz?= Date: Wed, 1 Jan 2025 19:00:57 +0300 Subject: [PATCH 1/4] feat(eslint-plugin): add support for commencis copyright text replacement --- .changeset/four-frogs-agree.md | 5 +++++ .../eslint-plugin/src/rules/copyright-text.ts | 21 +++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 .changeset/four-frogs-agree.md diff --git a/.changeset/four-frogs-agree.md b/.changeset/four-frogs-agree.md new file mode 100644 index 0000000..a7848d0 --- /dev/null +++ b/.changeset/four-frogs-agree.md @@ -0,0 +1,5 @@ +--- +'@commencis/eslint-plugin': minor +--- + +feat: add support for Commencis copyright-text replacement to ESLint fixer diff --git a/packages/eslint-plugin/src/rules/copyright-text.ts b/packages/eslint-plugin/src/rules/copyright-text.ts index ede347d..d9800b9 100644 --- a/packages/eslint-plugin/src/rules/copyright-text.ts +++ b/packages/eslint-plugin/src/rules/copyright-text.ts @@ -1,4 +1,4 @@ -import { TSESTree } from '@typescript-eslint/utils'; +import { AST_TOKEN_TYPES, TSESTree } from '@typescript-eslint/utils'; import { DEFAULT_START_YEAR } from '@/constants'; import { createRule, getCopyrightText } from '@/utils'; @@ -19,7 +19,7 @@ export default createRule<[RuleOptions], MessageIds>({ }, messages: { missingCopyright: - 'File must start with the required Commencis copyright text.', + 'File must start with the required Commencis copyright text with correct dates.', }, schema: [ { @@ -46,15 +46,28 @@ export default createRule<[RuleOptions], MessageIds>({ return { Program(node: TSESTree.Program): void { const sourceCode = context.sourceCode.getText(); + const comments = context.sourceCode.getAllComments(); const trimmedText = sourceCode.trimStart(); - if (!trimmedText.startsWith(expectedCopyrightText)) { + const isCopyrightValid = trimmedText.startsWith(expectedCopyrightText); + + if (!isCopyrightValid) { + const firstComment = comments[0]; + const isCommencisCopyrightTextExists = + firstComment && + firstComment.type === AST_TOKEN_TYPES.Block && + firstComment.value.includes('Copyright') && + firstComment.value.includes('Commencis'); + context.report({ node, messageId: 'missingCopyright', fix(fixer) { const insertText = `${expectedCopyrightText}\n\n`; - return fixer.insertTextBeforeRange([0, 0], insertText); + + return isCommencisCopyrightTextExists + ? fixer.replaceText(firstComment, insertText) + : fixer.insertTextBeforeRange([0, 0], insertText); }, }); } From 408a5de63067b47448afc20701082a424d267a9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmetcan=20Y=C4=B1lmaz?= Date: Wed, 1 Jan 2025 19:11:04 +0300 Subject: [PATCH 2/4] feat(eslint-plugin): add validate commencis copyright helper --- .../eslint-plugin/src/rules/copyright-text.ts | 24 +++++++++---------- packages/eslint-plugin/src/utils/index.ts | 1 + .../src/utils/validateCommencisCopyright.ts | 12 ++++++++++ 3 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 packages/eslint-plugin/src/utils/validateCommencisCopyright.ts diff --git a/packages/eslint-plugin/src/rules/copyright-text.ts b/packages/eslint-plugin/src/rules/copyright-text.ts index d9800b9..f3cff52 100644 --- a/packages/eslint-plugin/src/rules/copyright-text.ts +++ b/packages/eslint-plugin/src/rules/copyright-text.ts @@ -1,7 +1,11 @@ -import { AST_TOKEN_TYPES, TSESTree } from '@typescript-eslint/utils'; +import { TSESTree } from '@typescript-eslint/utils'; import { DEFAULT_START_YEAR } from '@/constants'; -import { createRule, getCopyrightText } from '@/utils'; +import { + createRule, + getCopyrightText, + validateCommencisCopyright, +} from '@/utils'; type RuleOptions = { startYear?: number; @@ -46,18 +50,12 @@ export default createRule<[RuleOptions], MessageIds>({ return { Program(node: TSESTree.Program): void { const sourceCode = context.sourceCode.getText(); - const comments = context.sourceCode.getAllComments(); + const firstComment = context.sourceCode.getAllComments()[0]; const trimmedText = sourceCode.trimStart(); + const isCopyrightExists = trimmedText.startsWith(expectedCopyrightText); - const isCopyrightValid = trimmedText.startsWith(expectedCopyrightText); - - if (!isCopyrightValid) { - const firstComment = comments[0]; - const isCommencisCopyrightTextExists = - firstComment && - firstComment.type === AST_TOKEN_TYPES.Block && - firstComment.value.includes('Copyright') && - firstComment.value.includes('Commencis'); + if (!isCopyrightExists) { + const isCommencisCopyright = validateCommencisCopyright(firstComment); context.report({ node, @@ -65,7 +63,7 @@ export default createRule<[RuleOptions], MessageIds>({ fix(fixer) { const insertText = `${expectedCopyrightText}\n\n`; - return isCommencisCopyrightTextExists + return isCommencisCopyright ? fixer.replaceText(firstComment, insertText) : fixer.insertTextBeforeRange([0, 0], insertText); }, diff --git a/packages/eslint-plugin/src/utils/index.ts b/packages/eslint-plugin/src/utils/index.ts index 029c21e..1979fc3 100644 --- a/packages/eslint-plugin/src/utils/index.ts +++ b/packages/eslint-plugin/src/utils/index.ts @@ -1,3 +1,4 @@ export * from './createRule'; export * from './getCopyrightText'; export * from './getDocsUrl'; +export * from './validateCommencisCopyright'; diff --git a/packages/eslint-plugin/src/utils/validateCommencisCopyright.ts b/packages/eslint-plugin/src/utils/validateCommencisCopyright.ts new file mode 100644 index 0000000..2cffff5 --- /dev/null +++ b/packages/eslint-plugin/src/utils/validateCommencisCopyright.ts @@ -0,0 +1,12 @@ +import { AST_TOKEN_TYPES, TSESTree } from '@typescript-eslint/utils'; + +export function validateCommencisCopyright( + commentNode?: TSESTree.Comment +): boolean { + if (!commentNode) return false; + return ( + commentNode.type === AST_TOKEN_TYPES.Block && + commentNode.value.includes('Commencis') && + commentNode.value.includes('Copyright') + ); +} From 0a3aaf195a8ce629e870e412b4bc6c7b5ad3558a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmetcan=20Y=C4=B1lmaz?= Date: Wed, 1 Jan 2025 19:19:31 +0300 Subject: [PATCH 3/4] feat(eslint-plugin): update copyright-text rule --- packages/eslint-plugin/src/rules/copyright-text.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/src/rules/copyright-text.ts b/packages/eslint-plugin/src/rules/copyright-text.ts index f3cff52..71ed681 100644 --- a/packages/eslint-plugin/src/rules/copyright-text.ts +++ b/packages/eslint-plugin/src/rules/copyright-text.ts @@ -52,10 +52,11 @@ export default createRule<[RuleOptions], MessageIds>({ const sourceCode = context.sourceCode.getText(); const firstComment = context.sourceCode.getAllComments()[0]; const trimmedText = sourceCode.trimStart(); - const isCopyrightExists = trimmedText.startsWith(expectedCopyrightText); + const isCopyrightValid = trimmedText.startsWith(expectedCopyrightText); - if (!isCopyrightExists) { - const isCommencisCopyright = validateCommencisCopyright(firstComment); + if (!isCopyrightValid) { + const isCommencisCopyrightExists = + validateCommencisCopyright(firstComment); context.report({ node, @@ -63,7 +64,7 @@ export default createRule<[RuleOptions], MessageIds>({ fix(fixer) { const insertText = `${expectedCopyrightText}\n\n`; - return isCommencisCopyright + return isCommencisCopyrightExists ? fixer.replaceText(firstComment, insertText) : fixer.insertTextBeforeRange([0, 0], insertText); }, From 11473d695210581cb073ccbac45dd529116087ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmetcan=20Y=C4=B1lmaz?= Date: Wed, 1 Jan 2025 19:23:07 +0300 Subject: [PATCH 4/4] chore: improve copyright validator --- packages/eslint-plugin/src/utils/validateCommencisCopyright.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/utils/validateCommencisCopyright.ts b/packages/eslint-plugin/src/utils/validateCommencisCopyright.ts index 2cffff5..081153a 100644 --- a/packages/eslint-plugin/src/utils/validateCommencisCopyright.ts +++ b/packages/eslint-plugin/src/utils/validateCommencisCopyright.ts @@ -3,8 +3,8 @@ import { AST_TOKEN_TYPES, TSESTree } from '@typescript-eslint/utils'; export function validateCommencisCopyright( commentNode?: TSESTree.Comment ): boolean { - if (!commentNode) return false; return ( + !!commentNode && commentNode.type === AST_TOKEN_TYPES.Block && commentNode.value.includes('Commencis') && commentNode.value.includes('Copyright')