Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1774ee9

Browse files
authoredMay 14, 2025··
Merge pull request #1250 from bryceosterhaus/LPD-55581
feat(eslint-plugin): add new rule for api submodule bundles
2 parents c45e98a + ffc3afd commit 1774ee9

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
 

‎projects/eslint-plugin/configs/portal.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const config = {
1010
rules: {
1111
'@liferay/portal/deprecation': 'error',
1212
'@liferay/portal/empty-line-after-copyright': 'error',
13+
'@liferay/portal/no-api-submodule-import': 'error',
1314
'@liferay/portal/no-default-export-from-frontend-js-web': 'error',
1415
'@liferay/portal/no-document-cookie': 'error',
1516
'@liferay/portal/no-explicit-extend': 'error',

‎projects/eslint-plugin/rules/portal/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
module.exports = {
77
'portal/deprecation': require('./lib/rules/deprecation'),
88
'portal/empty-line-after-copyright': require('./lib/rules/empty-line-after-copyright'),
9+
'portal/no-api-submodule-import': require('./lib/rules/no-api-submodule-import'),
910
'portal/no-default-export-from-frontend-js-web': require('./lib/rules/no-default-export-from-frontend-js-web'),
1011
'portal/no-document-cookie': require('./lib/rules/no-document-cookie'),
1112
'portal/no-explicit-extend': require('./lib/rules/no-explicit-extend'),
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* SPDX-FileCopyrightText: © 2017 Liferay, Inc. <https://liferay.com>
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
6+
const path = require('path');
7+
8+
const DESCRIPTION =
9+
'you cannot import files outside of the "/api" directory for your submodule.';
10+
11+
const API_PATH = 'src/main/resources/META-INF/resources/js/api';
12+
13+
function isPathEscapingAPIDir(filePath, relativePath) {
14+
const resolvedTarget = path.resolve(path.dirname(filePath), relativePath);
15+
16+
return !resolvedTarget.includes(API_PATH);
17+
}
18+
19+
function checkImportPath(node, context) {
20+
if (
21+
context.getFilename().includes(API_PATH) &&
22+
node.source &&
23+
node.source.type === 'Literal' &&
24+
node.source.value.startsWith('.')
25+
) {
26+
if (isPathEscapingAPIDir(context.getFilename(), node.source.value)) {
27+
context.report({
28+
message: DESCRIPTION,
29+
node,
30+
});
31+
}
32+
}
33+
}
34+
35+
module.exports = {
36+
create(context) {
37+
return {
38+
ExportDefaultDeclaration(node) {
39+
checkImportPath(node, context);
40+
},
41+
ExportNamedDeclaration(node) {
42+
checkImportPath(node, context);
43+
},
44+
ImportDeclaration(node) {
45+
checkImportPath(node, context);
46+
},
47+
};
48+
},
49+
50+
meta: {
51+
docs: {
52+
category: 'Best Practices',
53+
description: DESCRIPTION,
54+
recommended: false,
55+
},
56+
fixable: null,
57+
schema: [],
58+
type: 'problem',
59+
},
60+
};

0 commit comments

Comments
 (0)
Please sign in to comment.