forked from jchen1/eslint-plugin-absolute-imports
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
168 lines (150 loc) · 4.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"use strict";
const fs = require("fs");
const path = require("path");
const jsonParser = require("jsonc-parser");
function has(map, path) {
let inner = map;
for (let step of path.split(".")) {
inner = inner[step];
if (inner === undefined) {
return false;
}
}
return true;
}
function findDirWithFile(filename) {
let dir = path.resolve(filename);
do {
dir = path.dirname(dir);
} while (!fs.existsSync(path.join(dir, filename)) && dir !== "/");
if (!fs.existsSync(path.join(dir, filename))) {
return;
}
return dir;
}
function getImportPrefixToAlias(paths) {
const reversed = {};
for (let key of Object.keys(paths)) {
for (let path of paths[key]) {
reversed[path] = key;
}
}
return reversed;
}
function getBaseUrlAndPaths(baseDir) {
let url = "";
let paths = {};
if (fs.existsSync(path.join(baseDir, "tsconfig.json"))) {
const tsconfig = jsonParser.parse(
fs.readFileSync(path.join(baseDir, "tsconfig.json")).toString()
);
if (has(tsconfig, "compilerOptions.baseUrl")) {
url = tsconfig.compilerOptions.baseUrl;
}
if (has(tsconfig, "compilerOptions.paths")) {
paths = tsconfig.compilerOptions.paths;
}
} else if (fs.existsSync(path.join(baseDir, "jsconfig.json"))) {
const jsconfig = jsonParser.parse(
fs.readFileSync(path.join(baseDir, "jsconfig.json")).toString()
);
if (has(jsconfig, "compilerOptions.baseUrl")) {
url = jsconfig.compilerOptions.baseUrl;
}
if (has(jsconfig, "compilerOptions.paths")) {
paths = jsconfig.compilerOptions.paths;
}
}
return [path.join(baseDir, url), paths];
}
function getExpectedPath(absolutePath, baseUrl, importPrefixToAlias, onlyPathAliases, onlyAbsoluteImports) {
const relativeToBasePath = path.relative(baseUrl, absolutePath);
if (!onlyAbsoluteImports) {
for (let prefix of Object.keys(importPrefixToAlias)) {
const aliasPath = importPrefixToAlias[prefix];
// assuming they are either a full path or a path ends with /*, which are the two standard cases
const importPrefix = prefix.endsWith("/*") ? prefix.replace("/*", "") : prefix;
const aliasImport = aliasPath.endsWith("/*") ? aliasPath.replace("/*", "") : aliasPath;
if (relativeToBasePath.startsWith(importPrefix)) {
return `${aliasImport}${relativeToBasePath.slice(importPrefix.length)}`;
}
}
}
if (!onlyPathAliases) {
return relativeToBasePath;
}
}
const optionsSchema = {
type: 'object',
properties: {
onlyPathAliases: {
type: 'boolean',
},
onlyAbsoluteImports: {
type: 'boolean',
},
}
}
function generateRule(context, errorMessagePrefix, importPathConditionCallback) {
const options = context.options[0] || {};
const onlyPathAliases = options.onlyPathAliases || false;
const onlyAbsoluteImports = options.onlyAbsoluteImports || false;
const baseDir = findDirWithFile("package.json");
const [baseUrl, paths] = getBaseUrlAndPaths(baseDir);
const importPrefixToAlias = getImportPrefixToAlias(paths);
return {
ImportDeclaration(node) {
const source = node.source.value;
if (importPathConditionCallback(source)) {
const filename = context.getFilename();
const absolutePath = path.normalize(
path.join(path.dirname(filename), source)
);
const expectedPath = getExpectedPath(
absolutePath,
baseUrl,
importPrefixToAlias,
onlyPathAliases,
onlyAbsoluteImports,
);
if (expectedPath && source !== expectedPath) {
context.report({
node,
message: `${errorMessagePrefix}. Use \`${expectedPath}\` instead of \`${source}\`.`,
fix: function (fixer) {
return fixer.replaceText(node.source, `'${expectedPath}'`);
},
});
}
}
},
};
}
module.exports.rules = {
"no-relative-import": {
meta: {
fixable: true,
schema: [optionsSchema]
},
create: function (context) {
return generateRule(
context,
"Relative imports are not allowed",
(source) => source.startsWith("."),
);
},
},
"no-relative-parent-imports": {
meta: {
fixable: true,
schema: [optionsSchema]
},
create: function (context) {
return generateRule(
context,
"Relative imports from parent directories are not allowed",
(source) => source.startsWith(".."),
)
},
},
};