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..71ed681 100644 --- a/packages/eslint-plugin/src/rules/copyright-text.ts +++ b/packages/eslint-plugin/src/rules/copyright-text.ts @@ -1,7 +1,11 @@ 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; @@ -19,7 +23,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 +50,23 @@ export default createRule<[RuleOptions], MessageIds>({ return { Program(node: TSESTree.Program): void { const sourceCode = context.sourceCode.getText(); + const firstComment = context.sourceCode.getAllComments()[0]; const trimmedText = sourceCode.trimStart(); + const isCopyrightValid = trimmedText.startsWith(expectedCopyrightText); + + if (!isCopyrightValid) { + const isCommencisCopyrightExists = + validateCommencisCopyright(firstComment); - if (!trimmedText.startsWith(expectedCopyrightText)) { context.report({ node, messageId: 'missingCopyright', fix(fixer) { const insertText = `${expectedCopyrightText}\n\n`; - return fixer.insertTextBeforeRange([0, 0], insertText); + + return isCommencisCopyrightExists + ? 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..081153a --- /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 { + return ( + !!commentNode && + commentNode.type === AST_TOKEN_TYPES.Block && + commentNode.value.includes('Commencis') && + commentNode.value.includes('Copyright') + ); +}