[eslint] forbid trailing slashes in package imports (#113455) (#113558)

Co-authored-by: spalger <spalger@users.noreply.github.com>

Co-authored-by: Spencer <email@spalger.com>
Co-authored-by: spalger <spalger@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2021-09-30 15:29:48 -04:00 committed by GitHub
parent 53427d92aa
commit f095537175
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 117 additions and 3 deletions

View file

@ -93,5 +93,6 @@ module.exports = {
'@kbn/eslint/no_async_promise_body': 'error',
'@kbn/eslint/no_async_foreach': 'error',
'@kbn/eslint/no_trailing_import_slash': 'error',
},
};

View file

@ -15,5 +15,6 @@ module.exports = {
no_export_all: require('./rules/no_export_all'),
no_async_promise_body: require('./rules/no_async_promise_body'),
no_async_foreach: require('./rules/no_async_foreach'),
no_trailing_import_slash: require('./rules/no_trailing_import_slash'),
},
};

View file

@ -0,0 +1,37 @@
/*
* 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.
*/
/** @typedef {import("eslint").Rule.RuleModule} Rule */
/** @typedef {import("@typescript-eslint/typescript-estree").TSESTree.ImportDeclaration} ImportDeclaration */
const ERROR_MSG =
'Using a trailing slash in package import statements causes issues with webpack and is inconsistent with the rest of the respository.';
/** @type {Rule} */
module.exports = {
meta: {
fixable: 'code',
schema: [],
},
create: (context) => ({
ImportDeclaration(_) {
const node = /** @type {ImportDeclaration} */ (_);
const req = node.source.value;
if (!req.startsWith('.') && req.endsWith('/')) {
context.report({
message: ERROR_MSG,
loc: node.source.loc,
fix(fixer) {
return fixer.replaceText(node.source, `'${req.slice(0, -1)}'`);
},
});
}
},
}),
};

View file

@ -0,0 +1,75 @@
/*
* 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 { RuleTester } = require('eslint');
const rule = require('./no_trailing_import_slash');
const dedent = require('dedent');
const ruleTester = new RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
sourceType: 'module',
ecmaVersion: 2018,
ecmaFeatures: {
jsx: true,
},
},
});
ruleTester.run('@kbn/eslint/no_trailing_import_slash', rule, {
valid: [
{
code: dedent`
import foo from 'bar';
`,
},
{
code: dedent`
import foo from './bar';
`,
},
{
code: dedent`
import foo from './bar/';
`,
},
],
invalid: [
{
code: dedent`
import foo from 'bar/';
`,
errors: [
{
line: 1,
message:
'Using a trailing slash in package import statements causes issues with webpack and is inconsistent with the rest of the respository.',
},
],
output: dedent`
import foo from 'bar';
`,
},
{
code: dedent`
import foo from 'bar/box/';
`,
errors: [
{
line: 1,
message:
'Using a trailing slash in package import statements causes issues with webpack and is inconsistent with the rest of the respository.',
},
],
output: dedent`
import foo from 'bar/box';
`,
},
],
});

View file

@ -17,7 +17,7 @@ import {
EuiPopover,
} from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
import { i18n } from '@kbn/i18n/';
import { i18n } from '@kbn/i18n';
interface ToolBarPaginationProps {
pageSize: number;

View file

@ -8,7 +8,7 @@
import { i18n } from '@kbn/i18n';
import { SemVer } from 'semver';
import { CoreSetup } from 'src/core/public';
import { ManagementAppMountParams } from 'src/plugins/management/public/';
import { ManagementAppMountParams } from 'src/plugins/management/public';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/public';
import { UIM_APP_NAME } from '../../common/constants';

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import { i18n } from '@kbn/i18n/';
import { i18n } from '@kbn/i18n';
import crypto from 'crypto';
import { upperFirst } from 'lodash';
import { Observable } from 'rxjs';