Skip to content

Commit d7d3c49

Browse files
committed
feat: adding initial files and folders for no aliased import rule
1 parent 6e5d0df commit d7d3c49

File tree

3 files changed

+132
-4
lines changed

3 files changed

+132
-4
lines changed

index.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import noAliasedImports from "./lib/rules/no-aliased-imports.js";
2+
3+
export default {
4+
rules: {
5+
"no-aliased-imports": noAliasedImports,
6+
},
7+
};

lib/rules/no-aliased-imports.mjs

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import path from 'node:path';
2+
3+
module.exports = {
4+
meta: {
5+
type: 'suggestion',
6+
fixable: 'code',
7+
hasSuggestions: true,
8+
schema: [
9+
{
10+
type: 'object',
11+
properties: {
12+
// We'll allow multiple alias mappings
13+
aliases: {
14+
type: 'array',
15+
items: {
16+
type: 'object',
17+
properties: {
18+
prefix: { type: 'string' },
19+
target: { type: 'string' },
20+
},
21+
required: ['prefix', 'target'],
22+
},
23+
default: [{ prefix: '#', target: 'src' }],
24+
},
25+
// Folders or partial strings that the filename must include
26+
// in order for us to apply the rule
27+
includeFolders: {
28+
type: 'array',
29+
items: { type: 'string' },
30+
default: ['src'],
31+
},
32+
autoFix: {
33+
type: 'boolean',
34+
},
35+
},
36+
additionalProperties: false,
37+
},
38+
],
39+
messages: {
40+
noAlias: 'Use relative import instead of alias import in src files "{{ aliasImport }}".',
41+
noAliasNoAutofix: 'Use relative import instead of alias import in src files "{{ aliasImport }}" (auto-fix is disabled - enable in config if desired.)',
42+
},
43+
},
44+
// Provide defaults if none are given
45+
defaultOptions: [
46+
{
47+
aliases: [{ prefix: '#', target: 'src' }],
48+
includeFolders: ['src'],
49+
autoFix: true,
50+
},
51+
],
52+
create(context) {
53+
const options = context.options[0] || {};
54+
const {
55+
aliases,
56+
includeFolders,
57+
autoFix,
58+
} = options;
59+
return {
60+
ImportDeclaration(node) {
61+
const importPath = node.source.value;
62+
63+
// The absolute path of the current file being linted
64+
const filename = context.getFilename();
65+
66+
// 1) Check if the file is in one of the "includeFolders"
67+
const isInIncludedFolder = includeFolders.some((folder) => {
68+
return filename.includes(`${path.sep}${folder}${path.sep}`)
69+
|| filename.endsWith(`${path.sep}${folder}`);
70+
// ^ endsWith check so that if 'src' is the last part of the path, it still matches
71+
});
72+
73+
// File is not in any of the included folders, so skip
74+
if (!isInIncludedFolder) { return; }
75+
76+
// 2) Check if the import path starts with any of the alias prefixes
77+
const matchedAlias = aliases.find((aliasObj) => importPath.startsWith(aliasObj.prefix));
78+
79+
// doesn't match any alias prefix, so skip
80+
if (matchedAlias == null) { return; }
81+
82+
// For example: prefix = '#' => target = 'src'
83+
const { prefix, target } = matchedAlias;
84+
85+
// localPart is the substring after the alias prefix
86+
// e.g. `#db/utils.js` => `db/utils.js`
87+
const localPart = importPath.slice(prefix.length);
88+
89+
// 3) Build the absolute path to the *real* file
90+
// e.g. => <PROJECT_ROOT>/src/db/utils.js
91+
// or if alias is { prefix: '@', target: 'lib' } => <PROJECT_ROOT>/lib/db/utils.js
92+
const projectRoot = context.cwd;
93+
const absoluteImportPath = path.join(projectRoot, target, localPart);
94+
95+
// 4) Compute the relative path from the current file
96+
const currentFileDir = path.dirname(filename);
97+
98+
let relativePath = path.relative(currentFileDir, absoluteImportPath);
99+
if (!relativePath.startsWith('.')) {
100+
relativePath = `.${path.sep}${relativePath}`;
101+
}
102+
// If autoFix is false, don't give a fix - forces manual fix.
103+
const fix = autoFix
104+
? (fixer) => {
105+
return fixer.replaceTextRange(
106+
[node.source.range[0], node.source.range[1]],
107+
`'${relativePath}'`
108+
);
109+
}
110+
: null;
111+
112+
context.report({
113+
node: node.source,
114+
messageId: autoFix ? 'noAlias' : 'noAliasNoAutofix',
115+
data: { aliasImport: importPath },
116+
fix,
117+
});
118+
},
119+
};
120+
},
121+
};

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)