[eslint] unify resolver configs (#19102)

* [eslint] unify resolver configs

Our eslint resolver settings currently rely on the fact that we define
our resolver with a string globally, and an object in the overrides.
This causes the override value to completely override/replace the global
setting, which is desired, but when the global setting is converted to
an object they are merged, causing both resolvers to run.

This is a problem because some dependencies in the UI side of things
will resolve with the node resolver, but will resolve incorrectly
because they are intended to use some webpack specific override.

While trying to add TypeScript I needed to pass argument to the node
resolver which uncovered this issue. The change here moves us away from
using the node resolver directly and instead uses the kibana resolver
with `forceNode: true` set when linting server code and `forceNode:
false` when resolving imports that will be handled by webpack.

* [import-resolver] use object spread operator
This commit is contained in:
Spencer 2018-05-16 10:45:41 -07:00 committed by GitHub
parent f24a725144
commit 0b03166d2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 9 deletions

View file

@ -9,7 +9,12 @@ module.exports = {
],
settings: {
'import/resolver': 'eslint-import-resolver-node',
'import/resolver': {
'@kbn/eslint-import-resolver-kibana': {
forceNode: true,
},
},
react: {
version: '16.3',
},
@ -73,6 +78,7 @@ module.exports = {
'import/resolver': {
'@kbn/eslint-import-resolver-kibana': {
forceNode: false,
rootPackageName: 'kibana',
kibanaPath: '.',
pluginMap: readdirSync(resolve(__dirname, 'x-pack/plugins')).reduce(

View file

@ -33,9 +33,21 @@ function initContext(file, config) {
return context;
}
function tryNodeResolver(importRequest, file, config) {
return nodeResolver.resolve(importRequest, file, {
...config,
extensions: ['.js', '.json'],
isFile,
});
}
exports.resolve = function resolveKibanaPath(importRequest, file, config) {
config = config || {};
if (config.forceNode) {
return tryNodeResolver(importRequest, file, config);
}
// these modules are simulated by webpack, so there is no
// path to resolve to and no reason to do any more work
if (importRequest.startsWith('uiExports/')) {
@ -79,14 +91,7 @@ exports.resolve = function resolveKibanaPath(importRequest, file, config) {
// to the node_modules directory by the node resolver, but we want
// them to resolve to the actual shim
if (isPathRequest || !isProbablyWebpackShim(importRequest, file)) {
const nodeResult = nodeResolver.resolve(
importRequest,
file,
Object.assign({}, config, {
isFile,
})
);
const nodeResult = tryNodeResolver(importRequest, file, config);
if (nodeResult && nodeResult.found) {
return nodeResult;
}