Fix wrongly suggested module paths for @kbn/eslint/module_migration rule (#117781) (#118892)

* Use an alternative KIBANA_ROOT if bazel cache detected

* Use several fallbacks to find the kibana root

* Update comments

* Add test for relative import conversion

* Improve comments

* remove console log

* Improve comments

* Add second case

* improve tests filenames

Co-authored-by: Esteban Beltran <academo@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2021-11-17 12:19:10 -05:00 committed by GitHub
parent 30629fd109
commit 9783dcc255
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 122 additions and 1 deletions

View file

@ -0,0 +1,49 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
const path = require('path');
const fs = require('fs');
function isKibanaRoot(maybeKibanaRoot) {
try {
const packageJsonPath = path.join(maybeKibanaRoot, 'package.json');
fs.accessSync(packageJsonPath, fs.constants.R_OK);
const packageJsonContent = fs.readFileSync(packageJsonPath);
return JSON.parse(packageJsonContent).name === 'kibana';
} catch (e) {
return false;
}
}
module.exports = function findKibanaRoot() {
let maybeKibanaRoot = path.resolve(__dirname, '../../..');
// when using syslinks, __dirname reports outside of the repo
// if that's the case, the path will contain .cache/bazel
if (!maybeKibanaRoot.includes('.cache/bazel')) {
return maybeKibanaRoot;
}
// process.argv[1] would be the eslint binary, a correctly-set editor
// will use a local eslint inside the repo node_modules and its value
// should be `ACTUAL_KIBANA_ROOT/node_modules/.bin/eslint`
maybeKibanaRoot = path.resolve(process.argv[1], '../../../');
if (isKibanaRoot(maybeKibanaRoot)) {
return maybeKibanaRoot;
}
// eslint should run on the repo root level
// try to use process.cwd as the kibana root
maybeKibanaRoot = process.cwd();
if (isKibanaRoot(maybeKibanaRoot)) {
return maybeKibanaRoot;
}
// fallback to the first predicted path (original script)
return maybeKibanaRoot;
};

View file

@ -7,7 +7,8 @@
*/
const path = require('path');
const KIBANA_ROOT = path.resolve(__dirname, '../../..');
const findKibanaRoot = require('../helpers/find_kibana_root');
const KIBANA_ROOT = findKibanaRoot();
function checkModuleNameNode(context, mappings, node, desc = 'Imported') {
const mapping = mappings.find(

View file

@ -77,5 +77,76 @@ ruleTester.run('@kbn/eslint/module-migration', rule, {
export const foo2 = 'bar'
`,
},
/**
* Given this tree:
* x-pack/
* - common/
* - foo.ts <-- the target import
* - other/
* - folder/
* - bar.ts <-- the linted fle
* import "x-pack/common/foo" should be
* import ../../foo
*/
{
code: dedent`
import "x-pack/common/foo"
`,
filename: 'x-pack/common/other/folder/bar.ts',
options: [
[
{
from: 'x-pack',
to: 'foo',
toRelative: 'x-pack',
},
],
],
errors: [
{
line: 1,
message: 'Imported module "x-pack/common/foo" should be "../../foo"',
},
],
output: dedent`
import '../../foo'
`,
},
/**
* Given this tree:
* x-pack/
* - common/
* - foo.ts <-- the target import
* - another/
* - posible
* - example <-- the linted file
*
* import "x-pack/common/foo" should be
* import ../../common/foo
*/
{
code: dedent`
import "x-pack/common/foo"
`,
filename: 'x-pack/another/possible/example.ts',
options: [
[
{
from: 'x-pack',
to: 'foo',
toRelative: 'x-pack',
},
],
],
errors: [
{
line: 1,
message: 'Imported module "x-pack/common/foo" should be "../../common/foo"',
},
],
output: dedent`
import '../../common/foo'
`,
},
],
});