From 585730a3f8d422090b2fd4ac2bcff689f17128ce Mon Sep 17 00:00:00 2001 From: Bryce Osterhaus Date: Thu, 30 Jan 2025 09:56:59 +0400 Subject: [PATCH] feat(eslint-plugin): add rule for new lines after copyright header --- projects/eslint-plugin/configs/portal.js | 1 + projects/eslint-plugin/rules/portal/index.js | 1 + .../lib/rules/empty-line-after-copyright.js | 52 +++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 projects/eslint-plugin/rules/portal/lib/rules/empty-line-after-copyright.js diff --git a/projects/eslint-plugin/configs/portal.js b/projects/eslint-plugin/configs/portal.js index 86012b1829..7e13f27c6b 100644 --- a/projects/eslint-plugin/configs/portal.js +++ b/projects/eslint-plugin/configs/portal.js @@ -9,6 +9,7 @@ const config = { extends: [require.resolve('./react')], rules: { '@liferay/portal/deprecation': 'error', + '@liferay/portal/empty-line-after-copyright': 'error', '@liferay/portal/no-default-export-from-frontend-js-web': 'error', '@liferay/portal/no-document-cookie': 'error', '@liferay/portal/no-explicit-extend': 'error', diff --git a/projects/eslint-plugin/rules/portal/index.js b/projects/eslint-plugin/rules/portal/index.js index 1d9750c3a4..6284b4b94f 100644 --- a/projects/eslint-plugin/rules/portal/index.js +++ b/projects/eslint-plugin/rules/portal/index.js @@ -5,6 +5,7 @@ module.exports = { 'portal/deprecation': require('./lib/rules/deprecation'), + 'portal/empty-line-after-copyright': require('./lib/rules/empty-line-after-copyright'), 'portal/no-default-export-from-frontend-js-web': require('./lib/rules/no-default-export-from-frontend-js-web'), 'portal/no-document-cookie': require('./lib/rules/no-document-cookie'), 'portal/no-explicit-extend': require('./lib/rules/no-explicit-extend'), diff --git a/projects/eslint-plugin/rules/portal/lib/rules/empty-line-after-copyright.js b/projects/eslint-plugin/rules/portal/lib/rules/empty-line-after-copyright.js new file mode 100644 index 0000000000..eeae0b44ec --- /dev/null +++ b/projects/eslint-plugin/rules/portal/lib/rules/empty-line-after-copyright.js @@ -0,0 +1,52 @@ +/** + * SPDX-FileCopyrightText: © 2021 Liferay, Inc. + * SPDX-License-Identifier: MIT + */ + +const message = 'Expected an empty line after the copyright notice.'; + +module.exports = { + create(context) { + return { + Program() { + const comments = context.getSourceCode().getAllComments(); + + const copyrightComment = comments.find((item) => + item.value.match('SPDX-FileCopyrightText:') + ); + + if (!copyrightComment) { + return; + } + + const endLine = copyrightComment.loc.end.line; + + const firstNode = context.getSourceCode().ast.body[0]; + + if (firstNode && firstNode.loc.start.line === endLine + 1) { + context.report({ + fix: (fixer) => { + return fixer.insertTextAfter( + copyrightComment, + '\n' + ); + }, + message, + node: firstNode, + }); + } + }, + }; + }, + + meta: { + docs: { + category: 'Best Practices', + description: message, + recommended: false, + }, + fixable: 'code', + schema: [], + type: 'problem', + }, +};