Skip to content

Commit 4d7c247

Browse files
feat: use babel plugin to resolve options
1 parent 53c33dd commit 4d7c247

File tree

5 files changed

+60
-19
lines changed

5 files changed

+60
-19
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"test": "base test"
1919
},
2020
"dependencies": {
21+
"@babel/core": "^7.10.2",
2122
"@dword-design/functions": "^1.1.2",
2223
"babel-plugin-module-resolver": "^4.0.0",
2324
"find-up": "^4.1.0",

src/index.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ const runTest = config => () => {
5555

5656
export default {
5757
'prefer-alias': {
58+
files: {
59+
'.babelrc.json': JSON.stringify({
60+
plugins: [['module-resolver', { alias: { '@': '.' } }]],
61+
}),
62+
},
5863
code: endent`
5964
import foo from '../foo/bar'
6065
`,

src/rules/prefer-alias.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { OptionManager } from '@babel/core'
12
import {
3+
find,
24
findKey,
35
keys,
46
replace,
@@ -16,24 +18,20 @@ export default {
1618
meta: {
1719
type: 'suggestion',
1820
fixable: true,
19-
schema: [
20-
{
21-
type: 'object',
22-
properties: {
23-
alias: { type: 'object' },
24-
},
25-
},
26-
],
2721
},
2822
create: context => {
29-
const options = {
30-
alias: { '@': '.' },
31-
...context.options[0],
32-
}
3323
const path = context.getFilename()
3424
const folder = P.dirname(path)
3525
// can't check a non-file
3626
if (path === '<text>') return {}
27+
const manager = new OptionManager()
28+
const babelConfig = manager.init({
29+
babelrc: true,
30+
root: folder,
31+
filename: path,
32+
})
33+
const plugin = babelConfig.plugins |> find({ key: 'module-resolver' })
34+
const options = plugin.options
3735
return {
3836
ImportDeclaration: node => {
3937
const importPath = node.source.value

src/rules/prefer-alias.spec.js

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const runTest = config => () => {
1010
const filename = config.filename || 'index.js'
1111
const output = config.output || config.code
1212
const messages = config.messages || []
13-
const options = config.options || []
1413
return withLocalTmpDir(async () => {
1514
await outputFiles(config.files)
1615
const linter = new Linter()
@@ -21,7 +20,7 @@ const runTest = config => () => {
2120
ecmaVersion: 2015,
2221
},
2322
rules: {
24-
'self/self': [1, ...options],
23+
'self/self': 'error',
2524
},
2625
}
2726
const lintedMessages = linter.verify(config.code, lintingConfig, {
@@ -37,9 +36,19 @@ const runTest = config => () => {
3736

3837
export default {
3938
external: {
39+
files: {
40+
'.babelrc.json': JSON.stringify({
41+
plugins: [['module-resolver', { alias: { '@': '.' } }]],
42+
}),
43+
},
4044
code: "import foo from 'foo'",
4145
},
4246
parent: {
47+
files: {
48+
'.babelrc.json': JSON.stringify({
49+
plugins: [['module-resolver', { alias: { '@': '.' } }]],
50+
}),
51+
},
4352
code: "import foo from '../foo/bar'",
4453
filename: P.join('sub', 'index.js'),
4554
messages: [
@@ -48,26 +57,42 @@ export default {
4857
output: "import foo from '@/foo/bar'",
4958
},
5059
'parent in-between folder': {
60+
files: {
61+
'.babelrc.json': JSON.stringify({
62+
plugins: [['module-resolver', { alias: { '@': '.' } }]],
63+
}),
64+
},
5165
code: "import foo from '../foo'",
5266
filename: P.join('sub', 'sub', 'index.js'),
5367
messages: ["Unexpected parent import '../foo'. Use '@/sub/foo' instead"],
5468
output: "import foo from '@/sub/foo'",
5569
},
5670
'parent import but no matching alias': {
71+
files: {
72+
'.babelrc.json': JSON.stringify({
73+
plugins: [['module-resolver', { alias: { '@': '.' } }]],
74+
}),
75+
},
5776
code: "import foo from '../../foo'",
5877
messages: [
5978
"Unexpected parent import '../../foo'. No matching alias found to fix the issue",
6079
],
6180
},
6281
'alias parent': {
6382
files: {
83+
'.babelrc.json': JSON.stringify({
84+
plugins: [['module-resolver', { alias: { '@': '.' } }]],
85+
}),
6486
'foo.js': '',
6587
},
6688
code: "import foo from '@/foo'",
6789
filename: 'sub/index.js',
6890
},
6991
'alias subpath': {
7092
files: {
93+
'.babelrc.json': JSON.stringify({
94+
plugins: [['module-resolver', { alias: { '@': '.' } }]],
95+
}),
7196
'foo.js': '',
7297
},
7398
code: "import foo from '@/foo'",
@@ -78,17 +103,22 @@ export default {
78103
},
79104
scoped: {
80105
files: {
106+
'.babelrc.json': JSON.stringify({
107+
plugins: [['module-resolver', { alias: { '@': '.' } }]],
108+
}),
81109
'foo.js': '',
82110
},
83111
code: "import foo from '@foo/bar'",
84112
},
85113
'cwd: subfolder': {
86114
files: {
115+
'.babelrc.json': JSON.stringify({
116+
plugins: [['module-resolver', { alias: { '@': '.' }, cwd: 'sub' }]],
117+
}),
87118
'sub/foo.js': '',
88119
},
89120
code: "import foo from '../foo'",
90121
filename: P.join('sub', 'sub', 'index.js'),
91-
options: [{ cwd: 'sub' }],
92122
messages: ["Unexpected parent import '../foo'. Use '@/foo' instead"],
93123
output: "import foo from '@/foo'",
94124
},
@@ -97,24 +127,31 @@ export default {
97127
sub: {
98128
'foo.js': '',
99129
'package.json': JSON.stringify({}),
130+
'.babelrc.json': JSON.stringify({
131+
plugins: [
132+
['module-resolver', { alias: { '@': '.' }, cwd: 'packagejson' }],
133+
],
134+
}),
100135
},
101136
},
102137
code: "import foo from '../foo'",
103138
filename: P.join('sub', 'sub', 'index.js'),
104-
options: [{ cwd: 'packagejson' }],
105139
messages: ["Unexpected parent import '../foo'. Use '@/foo' instead"],
106140
output: "import foo from '@/foo'",
107141
},
108142
'cwd: babelrc': {
109143
files: {
110144
sub: {
111145
'foo.js': '',
112-
'.babelrc.json': JSON.stringify({}),
146+
'.babelrc.json': JSON.stringify({
147+
plugins: [
148+
['module-resolver', { alias: { '@': '.' }, cwd: 'babelrc' }],
149+
],
150+
}),
113151
},
114152
},
115153
code: "import foo from '../foo'",
116154
filename: P.join('sub', 'sub', 'index.js'),
117-
options: [{ cwd: 'babelrc' }],
118155
messages: ["Unexpected parent import '../foo'. Use '@/foo' instead"],
119156
output: "import foo from '@/foo'",
120157
},

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
invariant "^2.2.4"
4747
semver "^5.5.0"
4848

49-
"@babel/core@^7.6.4", "@babel/core@^7.7.5", "@babel/core@^7.7.7":
49+
"@babel/core@^7.10.2", "@babel/core@^7.6.4", "@babel/core@^7.7.5", "@babel/core@^7.7.7":
5050
version "7.10.2"
5151
resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.10.2.tgz#bd6786046668a925ac2bd2fd95b579b92a23b36a"
5252
integrity sha512-KQmV9yguEjQsXqyOUGKjS4+3K8/DlOCE2pZcq4augdQmtTy5iv5EHtmMSJ7V4c1BIPjuwtZYqYLCq9Ga+hGBRQ==

0 commit comments

Comments
 (0)