Skip to content

Commit c078aa4

Browse files
committed
Adds new fixable lint rule to rewrite src/ imports
1 parent e729ed3 commit c078aa4

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

eslint.config.mjs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import antiTrojanSource from 'eslint-plugin-anti-trojan-source';
66
import importX from 'eslint-plugin-import-x';
77
import { configs as litConfigs } from 'eslint-plugin-lit';
88
import { configs as wcConfigs } from 'eslint-plugin-wc';
9+
import noSrcImports from './scripts/eslint-rules/no-src-imports.js';
910

1011
export default ts.config(
1112
js.configs.recommended,
@@ -20,8 +21,14 @@ export default ts.config(
2021
plugins: {
2122
'import-x': importX,
2223
'anti-trojan-source': antiTrojanSource,
24+
'@gitlens': {
25+
rules: {
26+
'no-src-imports': noSrcImports,
27+
},
28+
},
2329
},
2430
rules: {
31+
'@gitlens/no-src-imports': 'error',
2532
'anti-trojan-source/no-bidi': 'error',
2633
curly: ['error', 'multi-line', 'consistent'],
2734
eqeqeq: ['error', 'always', { null: 'ignore' }],
@@ -250,10 +257,6 @@ export default ts.config(
250257
group: ['**/env/**/*'],
251258
message: 'Use @env/ instead',
252259
},
253-
{
254-
group: ['src/*'],
255-
message: 'Use relative paths instead',
256-
},
257260
{
258261
group: ['react-dom'],
259262
importNames: ['Container'],
@@ -340,10 +343,6 @@ export default ts.config(
340343
'error',
341344
{
342345
patterns: [
343-
{
344-
group: ['src/*'],
345-
message: 'Use relative paths instead',
346-
},
347346
{
348347
group: ['react-dom'],
349348
importNames: ['Container'],
@@ -419,10 +418,6 @@ export default ts.config(
419418
'error',
420419
{
421420
patterns: [
422-
{
423-
group: ['src/*'],
424-
message: 'Use relative paths instead',
425-
},
426421
{
427422
group: ['react-dom'],
428423
importNames: ['Container'],
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
meta: {
5+
type: 'problem',
6+
docs: {
7+
description: 'Disallow import statements that start with src/',
8+
recommended: true,
9+
},
10+
fixable: 'code',
11+
schema: [],
12+
},
13+
create(context) {
14+
return {
15+
ImportDeclaration(node) {
16+
const importPath = node.source.value;
17+
if (importPath.startsWith('src/')) {
18+
context.report({
19+
node,
20+
message: 'Import from src/ should be rewritten to a relative path',
21+
fix(fixer) {
22+
const importPathAbsolute = path.resolve('.', importPath);
23+
const relativePath = path.relative(path.dirname(context.getFilename()), importPathAbsolute);
24+
const normalizedPath = path.normalize(relativePath).replace(/\\/g, '/');
25+
return fixer.replaceText(node.source, `'${normalizedPath}'`);
26+
},
27+
});
28+
}
29+
},
30+
};
31+
},
32+
};

0 commit comments

Comments
 (0)