Skip to content

Commit

Permalink
feat(eslint-plugin): add rule for new lines after copyright header
Browse files Browse the repository at this point in the history
  • Loading branch information
bryceosterhaus committed Jan 30, 2025
1 parent df309db commit 585730a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions projects/eslint-plugin/configs/portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions projects/eslint-plugin/rules/portal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* SPDX-FileCopyrightText: © 2021 Liferay, Inc. <https://liferay.com>
* 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',
},
};

0 comments on commit 585730a

Please sign in to comment.