Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix eslint rule for restricting certain lodash imports #151023

Merged
merged 15 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 49 additions & 68 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,10 @@ const DEV_PATTERNS = [
const RESTRICTED_IMPORTS = [
{
name: 'lodash',
importNames: ['set', 'setWith'],
message: 'Please use @kbn/safer-lodash-set instead',
importNames: ['set', 'setWith', 'template'],
message:
'lodash.set/setWith: Please use @kbn/safer-lodash-set instead.\n' +
'lodash.template: Function is unsafe, and not compatible with our content security policy.',
},
{
name: 'lodash.set',
Expand All @@ -204,8 +206,10 @@ const RESTRICTED_IMPORTS = [
},
{
name: 'lodash/fp',
importNames: ['set', 'setWith', 'assoc', 'assocPath'],
message: 'Please use @kbn/safer-lodash-set instead',
importNames: ['set', 'setWith', 'assoc', 'assocPath', 'template'],
message:
'lodash.set/setWith/assoc/assocPath: Please use @kbn/safer-lodash-set instead\n' +
'lodash.template: Function is unsafe, and not compatible with our content security policy.',
},
{
name: 'lodash/fp/set',
Expand All @@ -223,11 +227,6 @@ const RESTRICTED_IMPORTS = [
name: 'lodash/fp/assocPath',
message: 'Please use @kbn/safer-lodash-set instead',
},
{
name: 'lodash',
importNames: ['template'],
message: 'lodash.template is unsafe, and not compatible with our content security policy.',
},
{
name: 'lodash.template',
message: 'lodash.template is unsafe, and not compatible with our content security policy.',
Expand All @@ -236,11 +235,6 @@ const RESTRICTED_IMPORTS = [
name: 'lodash/template',
message: 'lodash.template is unsafe, and not compatible with our content security policy.',
},
{
name: 'lodash/fp',
importNames: ['template'],
message: 'lodash.template is unsafe, and not compatible with our content security policy.',
},
{
name: 'lodash/fp/template',
message: 'lodash.template is unsafe, and not compatible with our content security policy.',
Expand Down Expand Up @@ -734,68 +728,47 @@ module.exports = {
{
files: ['**/*.{js,mjs,ts,tsx}'],
rules: {
'no-restricted-imports': [
2,
'no-restricted-imports': ['error', ...RESTRICTED_IMPORTS],
'no-restricted-modules': [
'error',
{
paths: RESTRICTED_IMPORTS,
name: 'lodash.set',
message: 'Please use @kbn/safer-lodash-set instead',
},
],
'no-restricted-modules': [
2,
{
paths: [
{
name: 'lodash.set',
message: 'Please use @kbn/safer-lodash-set instead',
},
{
name: 'lodash.setwith',
message: 'Please use @kbn/safer-lodash-set instead',
},
{
name: 'lodash.template',
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
},
{
name: 'lodash/set',
message: 'Please use @kbn/safer-lodash-set instead',
},
{
name: 'lodash/setWith',
message: 'Please use @kbn/safer-lodash-set instead',
},
{
name: 'lodash/template',
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
},
],
name: 'lodash.setwith',
message: 'Please use @kbn/safer-lodash-set instead',
},
],
'no-restricted-properties': [
2,
{
object: 'lodash',
property: 'set',
name: 'lodash.template',
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
},
{
name: 'lodash/set',
message: 'Please use @kbn/safer-lodash-set instead',
},
{
object: '_',
property: 'set',
name: 'lodash/setWith',
message: 'Please use @kbn/safer-lodash-set instead',
},
{
object: 'lodash',
property: 'template',
name: 'lodash/template',
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
},
],
'no-restricted-properties': [
'error',
{
object: 'lodash',
property: 'set',
message: 'Please use @kbn/safer-lodash-set instead',
},
{
object: '_',
property: 'template',
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
property: 'set',
message: 'Please use @kbn/safer-lodash-set instead',
},
{
object: 'lodash',
Expand Down Expand Up @@ -827,22 +800,30 @@ module.exports = {
property: 'assocPath',
message: 'Please use @kbn/safer-lodash-set instead',
},
{
object: 'lodash',
property: 'template',
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
},
{
object: '_',
property: 'template',
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
},
],
},
},
{
files: ['**/common/**/*.{js,mjs,ts,tsx}', '**/public/**/*.{js,mjs,ts,tsx}'],
rules: {
'no-restricted-imports': [
2,
'error',
...RESTRICTED_IMPORTS,
{
paths: [
...RESTRICTED_IMPORTS,
{
name: 'semver',
message: 'Please use "semver/*/{function}" instead',
},
],
name: 'semver',
message: 'Please use "semver/*/{function}" instead',
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { set } from 'lodash';
import { set } from '@kbn/safer-lodash-set';

export function dedot(source: Record<string, any>, target: Record<string, any>) {
// eslint-disable-next-line guard-for-in
Expand Down
3 changes: 3 additions & 0 deletions packages/kbn-apm-synthtrace-client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
"include": ["**/*.ts"],
"exclude": [
"target/**/*",
],
"kbn_references": [
"@kbn/safer-lodash-set",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { set } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import {
buildInlineScriptForPhraseFilter,
buildPhraseFilter,
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-es-query/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"kbn_references": [
"@kbn/utility-types",
"@kbn/i18n",
"@kbn/safer-lodash-set",
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*/

import { toExpression } from './to_expression';
import { cloneDeep, set, unset } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { cloneDeep, unset } from 'lodash';

describe('toExpression', () => {
describe('single expression', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-interpreter/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"**/*.js"
],
"kbn_references": [
"@kbn/safer-lodash-set",
],
"exclude": [
"target/**/*",
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-safer-lodash-set/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Lodash v4.x.
## Example Usage

```js
const { set } = require('@elastic/safer-loadsh-set');
const { set } = require('@kbn/safer-lodash-set');

const object = { a: [{ b: { c: 3 } }] };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { set } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { PersistableStateService } from '@kbn/kibana-utils-plugin/common';
import {
ControlGroupTelemetry,
Expand Down
1 change: 1 addition & 0 deletions src/plugins/controls/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@kbn/config-schema",
"@kbn/storybook",
"@kbn/ui-theme",
"@kbn/safer-lodash-set",
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* Side Public License, v 1.
*/

import { defaults, get, set } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { defaults, get } from 'lodash';
import { DataViewsService, DataView } from '.';
import { fieldFormatsMock } from '@kbn/field-formats-plugin/common/mocks';

Expand Down
1 change: 1 addition & 0 deletions src/plugins/data_views/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@kbn/core-test-helpers-http-setup-browser",
"@kbn/config-schema",
"@kbn/utility-types-jest",
"@kbn/safer-lodash-set",
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import type { Logger } from '@kbn/core/server';
import { set } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { Readable } from 'stream';
import { encode } from 'cbor-x';
import { elasticsearchServiceMock, loggingSystemMock } from '@kbn/core/server/mocks';
Expand Down
1 change: 1 addition & 0 deletions src/plugins/files/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@kbn/core-saved-objects-api-server",
"@kbn/core-logging-server-mocks",
"@kbn/ecs",
"@kbn/safer-lodash-set",
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 2.0.
*/

import { isEmpty, set } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { isEmpty } from 'lodash';
import { IEvent, SAVED_OBJECT_REL_PRIMARY } from '@kbn/event-log-plugin/server';
import { RelatedSavedObjects } from './related_saved_objects';

Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/actions/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"@kbn/std",
"@kbn/logging",
"@kbn/logging-mocks",
"@kbn/core-elasticsearch-client-server-mocks"
"@kbn/core-elasticsearch-client-server-mocks",
"@kbn/safer-lodash-set"
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { set } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { ActionsCompletion } from '../types';
import { ActionsConfigMap } from './get_actions_config_map';
import { SearchMetrics } from './types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { set, get, isEqual } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { get, isEqual } from 'lodash';
import type { BulkEditOperation, BulkEditFields } from '../types';

// defining an union type that will passed directly to generic function as a workaround for the issue similar to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 2.0.
*/

import { cloneDeep, get, set } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { cloneDeep, get } from 'lodash';
import { NewPackagePolicy } from '@kbn/fleet-plugin/common';
import { AgentConfiguration } from '../../../common/agent_configuration/configuration_types';
import { AGENT_NAME } from '../../../common/es_fields/apm';
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/apm/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"@kbn/core-elasticsearch-server",
"@kbn/shared-ux-prompt-not-found",
"@kbn/core-saved-objects-api-server",
"@kbn/safer-lodash-set",
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
EuiText,
} from '@elastic/eui';
import { ExpressionAstExpression } from '@kbn/expressions-plugin/common';
import { set } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { defaultExpression } from './default_expression';
import { Fields } from './types';
import { getFieldPath, getFieldValue } from './utils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React, { FunctionComponent, useCallback, useEffect } from 'react';
import PropTypes from 'prop-types';
import { EuiSwitch, EuiSwitchEvent } from '@elastic/eui';
import { ExpressionAstExpression } from '@kbn/expressions-plugin/common';
import { set } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { defaultExpression } from './default_expression';
import { getFieldPath, getFieldValue } from './utils';

Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/cases/public/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 2.0.
*/

import { isArray, set, camelCase, isObject, omit, get } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { isArray, camelCase, isObject, omit, get } from 'lodash';
import {
isCommentRequestTypeExternalReference,
isCommentRequestTypePersistableState,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/public/common/mock/connectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { set } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import type { ActionConnector, ActionTypeConnector } from '../../../common/api';
import { basicPush } from '../../containers/mock';
import type { CaseConnectors } from '../../containers/types';
Expand Down
Loading