mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
[eslint] forbid trailing slashes in package imports (#113455)
Co-authored-by: spalger <spalger@users.noreply.github.com>
This commit is contained in:
parent
747212ce45
commit
6cccf69451
7 changed files with 117 additions and 3 deletions
|
@ -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',
|
||||
},
|
||||
};
|
||||
|
|
|
@ -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'),
|
||||
},
|
||||
};
|
||||
|
|
|
@ -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)}'`);
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
}),
|
||||
};
|
|
@ -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';
|
||||
`,
|
||||
},
|
||||
],
|
||||
});
|
|
@ -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;
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue