Skip to content

Commit c63fbc3

Browse files
chore: lock file maintenance (#171)
* chore: lock file maintenance * fix: foo * fix: foo * chore: foo --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Sebastian Landwehr <[email protected]>
1 parent 5a59d6e commit c63fbc3

File tree

8 files changed

+2314
-2174
lines changed

8 files changed

+2314
-2174
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"features": {
33
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
44
},
5-
"image": "mcr.microsoft.com/devcontainers/javascript-node:0-20",
5+
"image": "mcr.microsoft.com/devcontainers/javascript-node:1-20",
66
"updateContentCommand": "yarn --frozen-lockfile"
77
}

.gitpod.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ tasks:
66
PUPPETEER_CACHE_DIR=/workspace/eslint-plugin-import-alias/node_modules/.cache/puppeteer"
77
>> /home/gitpod/.bashrc
88
9-
gitpod-env-per-project >> /home/gitpod/.bashrc && source /home/gitpod/.bashrc
9+
echo "export PLAYWRIGHT_BROWSERS_PATH=0" >> /home/gitpod/.bashrc
10+
11+
gitpod-env-per-project >> /home/gitpod/.bashrc && source
12+
/home/gitpod/.bashrc
1013
init: |-
1114
git config --global user.name "Sebastian Landwehr"
1215
git config diff.lfs.textconv cat

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"@dword-design/tester-plugin-tmp-dir": "^2.1.26",
5656
"depcheck-package-name": "^3.0.1",
5757
"eslint": "^8.0.0",
58-
"execa": "^8.0.0",
58+
"execa": "^9.3.1",
5959
"fs-extra": "^11.1.1",
6060
"in-folder": "^1.0.3",
6161
"output-files": "^2.0.0"

src/index.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
import preferAlias from './rules/prefer-alias.js'
1+
import preferAlias from './rules/prefer-alias.js';
22

33
export default {
44
configs: {
55
recommended: {
66
plugins: ['@dword-design/import-alias'],
7-
rules: {
8-
'@dword-design/import-alias/prefer-alias': 'error',
9-
},
7+
rules: { '@dword-design/import-alias/prefer-alias': 'error' },
108
},
119
},
12-
rules: {
13-
'prefer-alias': preferAlias,
14-
},
15-
}
10+
rules: { 'prefer-alias': preferAlias },
11+
};

src/index.spec.js

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { endent, flatten, join, map, pick } from '@dword-design/functions'
2-
import tester from '@dword-design/tester'
3-
import testerPluginTmpDir from '@dword-design/tester-plugin-tmp-dir'
4-
import packageName from 'depcheck-package-name'
5-
import { ESLint } from 'eslint'
6-
import { execaCommand } from 'execa'
7-
import outputFiles from 'output-files'
8-
import P from 'path'
1+
import { endent, flatten, join, map, pick } from '@dword-design/functions';
2+
import tester from '@dword-design/tester';
3+
import testerPluginTmpDir from '@dword-design/tester-plugin-tmp-dir';
4+
import packageName from 'depcheck-package-name';
5+
import { ESLint } from 'eslint';
6+
import { execaCommand } from 'execa';
7+
import outputFiles from 'output-files';
8+
import P from 'path';
99

1010
export default tester(
1111
{
@@ -38,48 +38,45 @@ export default tester(
3838
testerPluginTmpDir(),
3939
{
4040
transform: test => async () => {
41-
test.filename = test.filename || 'index.js'
42-
test.output = test.output || test.code
43-
test.messages = test.messages || []
41+
test.filename = test.filename || 'index.js';
42+
test.output = test.output || test.code;
43+
test.messages = test.messages || [];
44+
4445
await outputFiles({
4546
'node_modules/eslint-plugin-self/index.js':
4647
"module.exports = require('../../..')",
4748
...test.files,
48-
})
49+
});
4950

5051
const lintingConfig = {
5152
overrideConfig: {
5253
extends: ['plugin:self/recommended'],
53-
parserOptions: {
54-
ecmaVersion: 2015,
55-
sourceType: 'module',
56-
},
54+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
5755
},
5856
useEslintrc: false,
59-
}
60-
61-
const eslintToLint = new ESLint(lintingConfig)
57+
};
6258

63-
const eslintToFix = new ESLint({ ...lintingConfig, fix: true })
59+
const eslintToLint = new ESLint(lintingConfig);
60+
const eslintToFix = new ESLint({ ...lintingConfig, fix: true });
6461

6562
const lintedMessages =
66-
eslintToLint.lintText(test.code, {
67-
filePath: test.filename,
68-
})
63+
eslintToLint.lintText(test.code, { filePath: test.filename })
6964
|> await
7065
|> map('messages')
7166
|> flatten
72-
|> map(pick(['ruleId', 'message']))
73-
expect(lintedMessages).toEqual(test.messages)
67+
|> map(pick(['ruleId', 'message']));
68+
69+
expect(lintedMessages).toEqual(test.messages);
7470

7571
const lintedOutput =
7672
eslintToFix.lintText(test.code, { filePath: test.filename })
7773
|> await
7874
|> map('output')
79-
|> join('\n')
80-
expect(lintedOutput).toEqual(test.output)
75+
|> join('\n');
76+
77+
expect(lintedOutput).toEqual(test.output);
8178
},
8279
},
8380
{ before: () => execaCommand('base prepublishOnly') },
8481
],
85-
)
82+
);

src/rules/prefer-alias.js

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,84 @@
1-
import { OptionManager } from '@babel/core'
2-
import { find, keys, replace, some, startsWith } from '@dword-design/functions'
3-
import { resolvePath as defaultResolvePath } from 'babel-plugin-module-resolver'
4-
import deepmerge from 'deepmerge'
5-
import P from 'path'
1+
import { OptionManager } from '@babel/core';
2+
import { find, keys, replace, some, startsWith } from '@dword-design/functions';
3+
import { resolvePath as defaultResolvePath } from 'babel-plugin-module-resolver';
4+
import deepmerge from 'deepmerge';
5+
import P from 'path';
66

7-
const isParentImport = path => /^(\.\/)?\.\.\//.test(path)
7+
const isParentImport = path => /^(\.\/)?\.\.\//.test(path);
88

99
const findMatchingAlias = (sourcePath, currentFile, options) => {
10-
const resolvePath = options.resolvePath || defaultResolvePath
10+
const resolvePath = options.resolvePath || defaultResolvePath;
11+
const absoluteSourcePath = P.resolve(P.dirname(currentFile), sourcePath);
1112

12-
const absoluteSourcePath = P.resolve(P.dirname(currentFile), sourcePath)
1313
for (const aliasName of options.alias |> keys) {
1414
const path = P.resolve(
1515
P.dirname(currentFile),
1616
resolvePath(`${aliasName}/`, currentFile, options),
17-
)
17+
);
18+
1819
if (absoluteSourcePath |> startsWith(path)) {
19-
return { name: aliasName, path }
20+
return { name: aliasName, path };
2021
}
2122
}
2223

23-
return undefined
24-
}
24+
return undefined;
25+
};
2526

2627
export default {
2728
create: context => {
28-
const currentFile = context.getFilename()
29-
30-
const folder = P.dirname(currentFile)
29+
const currentFile = context.getFilename();
30+
const folder = P.dirname(currentFile);
3131
// can't check a non-file
32-
if (currentFile === '<text>') return {}
33-
34-
const manager = new OptionManager()
32+
if (currentFile === '<text>') return {};
33+
const manager = new OptionManager();
3534

3635
const babelConfig = manager.init({
3736
filename: currentFile,
3837
rootMode: 'upward-optional',
39-
})
38+
});
4039

41-
const plugin = babelConfig.plugins |> find({ key: 'module-resolver' })
40+
const plugin = babelConfig.plugins |> find({ key: 'module-resolver' });
4241

4342
const options = deepmerge.all([
4443
{ alias: [] },
4544
plugin?.options || {},
4645
context.options[0] || {},
47-
])
46+
]);
47+
4848
if (options.alias.length === 0) {
4949
throw new Error(
5050
'No alias configured. You have to define aliases by either passing them to the babel-plugin-module-resolver plugin in your Babel config, or directly to the prefer-alias rule.',
51-
)
51+
);
5252
}
5353

54-
const resolvePath = options.resolvePath || defaultResolvePath
55-
54+
const resolvePath = options.resolvePath || defaultResolvePath;
5655
return {
5756
ImportDeclaration: node => {
58-
const sourcePath = node.source.value
57+
const sourcePath = node.source.value;
5958

6059
const hasAlias =
6160
options.alias
6261
|> keys
63-
|> some(alias => sourcePath |> startsWith(`${alias}/`))
62+
|> some(alias => sourcePath |> startsWith(`${alias}/`));
63+
6464
// relative parent
6565
if (sourcePath |> isParentImport) {
6666
const matchingAlias = findMatchingAlias(
6767
sourcePath,
6868
currentFile,
6969
options,
70-
)
70+
);
71+
7172
if (!matchingAlias) {
72-
return undefined
73+
return undefined;
7374
}
7475

75-
const absoluteImportPath = P.resolve(folder, sourcePath)
76+
const absoluteImportPath = P.resolve(folder, sourcePath);
7677

7778
const rewrittenImport = `${matchingAlias.name}/${
7879
P.relative(matchingAlias.path, absoluteImportPath)
7980
|> replace(/\\/g, '/')
80-
}`
81+
}`;
8182

8283
return context.report({
8384
fix: fixer =>
@@ -87,10 +88,15 @@ export default {
8788
),
8889
message: `Unexpected parent import '${sourcePath}'. Use '${rewrittenImport}' instead`,
8990
node,
90-
})
91+
});
9192
}
9293

93-
const importWithoutAlias = resolvePath(sourcePath, currentFile, options)
94+
const importWithoutAlias = resolvePath(
95+
sourcePath,
96+
currentFile,
97+
options,
98+
);
99+
94100
if (!(importWithoutAlias |> isParentImport) && hasAlias) {
95101
return context.report({
96102
fix: fixer =>
@@ -100,26 +106,22 @@ export default {
100106
),
101107
message: `Unexpected subpath import via alias '${sourcePath}'. Use '${importWithoutAlias}' instead`,
102108
node,
103-
})
109+
});
104110
}
105111

106-
return undefined
112+
return undefined;
107113
},
108-
}
114+
};
109115
},
110116
meta: {
111117
fixable: true,
112118
schema: [
113119
{
114120
additionalProperties: false,
115-
properties: {
116-
alias: {
117-
type: 'object',
118-
},
119-
},
121+
properties: { alias: { type: 'object' } },
120122
type: 'object',
121123
},
122124
],
123125
type: 'suggestion',
124126
},
125-
}
127+
};

0 commit comments

Comments
 (0)