Skip to content

Commit

Permalink
Merge pull request #193 from Commencis/feature/commencis-copyright-te…
Browse files Browse the repository at this point in the history
…xt-replacement

feat(eslint-plugin): update copyright-text rule fixer for text replacement
  • Loading branch information
ymehmetcan authored Jan 1, 2025
2 parents c01a3a5 + 11473d6 commit 5c5689a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-frogs-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commencis/eslint-plugin': minor
---

feat: add support for Commencis copyright-text replacement to ESLint fixer
20 changes: 16 additions & 4 deletions packages/eslint-plugin/src/rules/copyright-text.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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: [
{
Expand All @@ -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);
},
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './createRule';
export * from './getCopyrightText';
export * from './getDocsUrl';
export * from './validateCommencisCopyright';
12 changes: 12 additions & 0 deletions packages/eslint-plugin/src/utils/validateCommencisCopyright.ts
Original file line number Diff line number Diff line change
@@ -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')
);
}

0 comments on commit 5c5689a

Please sign in to comment.