Use Object spread instead of Object.assign where it makes sense (#13903) (#14397)

* Prefer Object spread

* Add eslint rules for Object spread

* no slice
This commit is contained in:
Kim Joar Bekkelund 2017-10-10 15:55:06 +02:00 committed by GitHub
parent 4a18f3c985
commit feff479f77
28 changed files with 127 additions and 81 deletions

View file

@ -209,7 +209,7 @@
"yauzl": "2.7.0"
},
"devDependencies": {
"@elastic/eslint-config-kibana": "0.11.0",
"@elastic/eslint-config-kibana": "0.12.0",
"@elastic/eslint-import-resolver-kibana": "0.8.1",
"@elastic/eslint-plugin-kibana-custom": "1.0.3",
"angular-mocks": "1.4.7",
@ -228,6 +228,7 @@
"eslint-plugin-import": "2.3.0",
"eslint-plugin-jest": "21.0.0",
"eslint-plugin-mocha": "4.9.0",
"eslint-plugin-prefer-object-spread": "1.2.1",
"eslint-plugin-react": "7.1.0",
"event-stream": "3.3.2",
"expect.js": "0.3.1",

View file

@ -6,6 +6,7 @@ module.exports = {
'babel',
'react',
'import',
'prefer-object-spread',
],
env: {
@ -132,5 +133,7 @@ module.exports = {
'import/no-named-as-default': 'error',
'import/no-named-as-default-member': 'error',
'import/no-duplicates': 'error',
'prefer-object-spread/prefer-object-spread': 'error',
}
}

View file

@ -1,6 +1,6 @@
{
"name": "@elastic/eslint-config-kibana",
"version": "0.11.0",
"version": "0.12.0",
"description": "The eslint config used by the kibana team",
"main": ".eslintrc.js",
"scripts": {
@ -24,6 +24,7 @@
"eslint-plugin-import": "^2.6.0",
"eslint-plugin-jest": "^21.0.0",
"eslint-plugin-mocha": "^4.9.0",
"eslint-plugin-prefer-object-spread": "^1.2.1",
"eslint-plugin-react": "^7.1.0"
}
}

View file

@ -7,12 +7,16 @@ import { WildcardMatcher } from './wildcard_matcher';
export class ProxyConfig {
constructor(config) {
config = Object.assign({}, config);
config = {
...config
};
// -----
// read "match" info
// -----
const rawMatches = Object.assign({}, config.match);
const rawMatches = {
...config.match
};
this.id = formatUrl({
protocol: rawMatches.protocol,
hostname: rawMatches.host,

View file

@ -92,7 +92,12 @@ describe('plugins/elasticsearch', function () {
it('passes only whitelisted headers', () => {
const headers = { authorization: 'Basic TEST' };
const request = { headers: Object.assign({}, headers, { foo: 'Foo' }) };
const request = {
headers: {
...headers,
foo: 'Foo'
}
};
cluster.callWithRequest(request, 'nodes.info');

View file

@ -8,7 +8,9 @@ import { parseConfig } from './parse_config';
export class Cluster {
constructor(config) {
this._config = Object.assign({}, config);
this._config = {
...config
};
this.errors = elasticsearch.errors;
this._client = this.createClient();
@ -53,7 +55,10 @@ export class Cluster {
}
createClient = configOverrides => {
const config = Object.assign({}, this._getClientConfig(), configOverrides);
const config = {
...this._getClientConfig(),
...configOverrides
};
return new elasticsearch.Client(parseConfig(config));
}

View file

@ -12,7 +12,10 @@ export function createAdminCluster(server) {
const adminCluster = server.plugins.elasticsearch.createCluster(
'admin',
Object.assign({ log: AdminClientLogging }, config.get('elasticsearch'))
{
log: AdminClientLogging,
...config.get('elasticsearch')
}
);
server.on('close', bindKey(adminCluster, 'close'));

View file

@ -20,7 +20,10 @@ export function createDataCluster(server) {
const dataCluster = server.plugins.elasticsearch.createCluster(
'data',
Object.assign({ log: DataClientLogging }, getConfig())
{
log: DataClientLogging,
...getConfig()
}
);
server.on('close', bindKey(dataCluster, 'close'));

View file

@ -7,12 +7,13 @@ import Bluebird from 'bluebird';
const readFile = (file) => readFileSync(file, 'utf8');
export function parseConfig(serverConfig = {}) {
const config = Object.assign({
keepAlive: true
}, pick(serverConfig, [
'plugins', 'apiVersion', 'keepAlive', 'pingTimeout',
'requestTimeout', 'log', 'logQueries'
]));
const config = {
keepAlive: true,
...pick(serverConfig, [
'plugins', 'apiVersion', 'keepAlive', 'pingTimeout',
'requestTimeout', 'log', 'logQueries'
])
};
const uri = url.parse(serverConfig.url);
config.host = {

View file

@ -30,13 +30,10 @@ function fetchAnchorProvider(courier, Private) {
throw new Error('Failed to load anchor document.');
}
return Object.assign(
{},
_.get(response, ['hits', 'hits', 0]),
{
$$_isAnchor: true,
},
);
return {
..._.get(response, ['hits', 'hits', 0]),
$$_isAnchor: true,
};
};
}

View file

@ -59,11 +59,10 @@ function ContextAppController($scope, config, Private, timefilter) {
this.discoverUrl,
);
this.actions = _.mapValues(Object.assign(
{},
queryParameterActions,
queryActions,
), (action) => (...args) => action(this.state)(...args));
this.actions = _.mapValues({
...queryParameterActions,
...queryActions,
}, (action) => (...args) => action(this.state)(...args));
this.constants = {
FAILURE_REASONS,

View file

@ -122,7 +122,7 @@ export class DashboardState {
// in place in order for it to affect the visualizations.
this.stateDefaults.query = this.lastSavedDashboardFilters.query;
// Need to make a copy to ensure they are not overwritten.
this.stateDefaults.filters = Object.assign(new Array(), this.getLastSavedFilterBars());
this.stateDefaults.filters = [...this.getLastSavedFilterBars()];
this.isDirty = false;
this.appState.setDefaults(this.stateDefaults);

View file

@ -214,17 +214,15 @@ app.directive('discFieldChooser', function ($location, globalState, config, $rou
$scope.computeDetails = function (field, recompute) {
if (_.isUndefined(field.details) || recompute) {
field.details = Object.assign(
{
visualizeUrl: field.visualizable ? getVisualizeUrl(field) : null,
},
fieldCalculator.getFieldValueCounts({
field.details = {
visualizeUrl: field.visualizable ? getVisualizeUrl(field) : null,
...fieldCalculator.getFieldValueCounts({
hits: $scope.hits,
field: field,
count: 5,
grouped: false
}),
);
};
_.each(field.details.buckets, function (bucket) {
bucket.display = field.format.convert(bucket.value);
});

View file

@ -10,7 +10,10 @@ function Agg(props) {
if (!Component) {
Component = StdAgg;
}
const style = Object.assign({ cursor: 'default' }, props.style);
const style = {
cursor: 'default',
...props.style
};
return (
<div
aria-hidden="true"

View file

@ -26,14 +26,13 @@ describe('Saved Object', function () {
* @returns {{attributes: {}, type: string, id: *, _version: integer}}
*/
function getMockedDocResponse(indexPatternId, additionalOptions = {}) {
return Object.assign(
{
type: 'dashboard',
id: indexPatternId,
_version: 2,
attributes: {}
},
additionalOptions);
return {
type: 'dashboard',
id: indexPatternId,
_version: 2,
attributes: {},
...additionalOptions
};
}
/**
@ -481,10 +480,11 @@ describe('Saved Object', function () {
describe('defaults', function () {
function getTestDefaultConfig(extraOptions) {
return Object.assign({
return {
defaults: { testDefault: 'hi' },
type: 'dashboard'
}, extraOptions);
type: 'dashboard',
...extraOptions
};
}
function expectDefaultApplied(config) {

View file

@ -5,12 +5,13 @@ export function buildQueryFromKuery(indexPattern, queries) {
const queryASTs = _.map(queries, query => fromKueryExpression(query.query));
const compoundQueryAST = nodeTypes.function.buildNode('and', queryASTs);
const kueryQuery = toElasticsearchQuery(compoundQueryAST, indexPattern);
return Object.assign({
return {
must: [],
filter: [],
should: [],
must_not: [],
}, kueryQuery.bool);
...kueryQuery.bool
};
}

View file

@ -68,12 +68,13 @@ export function KbnTopNavControllerProvider($compile) {
}
// apply the defaults to individual options
_applyOptDefault(opt = {}) {
const defaultedOpt = Object.assign({
const defaultedOpt = {
label: capitalize(opt.key),
hasFunction: !!opt.run,
description: opt.run ? opt.key : `Toggle ${opt.key} view`,
run: (item) => this.toggle(item.key)
}, opt);
run: (item) => this.toggle(item.key),
...opt
};
defaultedOpt.hideButton = isFunction(opt.hideButton) ? opt.hideButton : () => !!opt.hideButton;
defaultedOpt.disableButton = isFunction(opt.disableButton) ? opt.disableButton : () => !!opt.disableButton;

View file

@ -10,7 +10,10 @@ export function fromKueryExpression(expression, parseOptions = {}) {
throw new Error('expression must be a string, got undefined instead');
}
parseOptions = Object.assign({}, parseOptions, { helpers: { nodeTypes } });
parseOptions = {
...parseOptions,
helpers: { nodeTypes }
};
return kueryParser.parse(expression, parseOptions);
}

View file

@ -36,7 +36,9 @@ module.directive('queryBar', function () {
};
$scope.$watch('queryBar.query', (newQuery) => {
this.localQuery = Object.assign({}, newQuery);
this.localQuery = {
...newQuery
};
}, true);
})
};

View file

@ -31,7 +31,10 @@ const hashStatesInQuery = (states, query) => {
}, {});
return Object.assign({}, query, hashedQuery);
return {
...query,
...hashedQuery
};
};
export const hashUrl = (states, redirectUrl) => {

View file

@ -37,10 +37,11 @@ export default (grunt) => {
rpm.filePath,
'-r', 'external-1'
], {
env: Object.assign({}, {
env: {
'AWS_ACCESS_KEY': rpm.awsKey,
'AWS_SECRET_KEY': rpm.awsSecret
}, process.env)
'AWS_SECRET_KEY': rpm.awsSecret,
...process.env
}
});
}

View file

@ -29,10 +29,11 @@ export function CommonPageProvider({ getService, getPageObjects }) {
* @param {string} subUrl The route after the hash (#)
*/
navigateToUrl(appName, subUrl) {
const appConfig = Object.assign({}, config.get(['apps', appName]), {
const appConfig = {
...config.get(['apps', appName]),
// Overwrite the default hash with the URL we really want.
hash: `${appName}/${subUrl}`,
});
};
const appUrl = getUrl.noAuth(config.get('servers.kibana'), appConfig);
return remote.get(appUrl);

View file

@ -10,7 +10,9 @@ export default function creatExample(examples) {
}
}
Example.propTypes = Object.assign({}, GuideExample.propTypes);
Example.propTypes = {
...GuideExample.propTypes
};
return Example;
}

View file

@ -16,26 +16,29 @@ export default function codeViewerReducer(state = defaultState, action) {
if (state.code === source) {
// If we are opening the existing code, then close the viewer.
return Object.assign({}, state, {
return {
...state,
isOpen: false,
source: undefined,
title: undefined,
});
};
}
return Object.assign({}, state, {
return {
...state,
isOpen: true,
source,
title,
});
};
}
case LOCATION_CHANGE: // Close Code Viewer when we navigate somewhere.
case ActionTypes.CLOSE_CODE_VIEWER: {
return Object.assign({}, state, {
return {
...state,
isOpen: false,
source: undefined,
});
};
}
default:

View file

@ -7,15 +7,17 @@ const defaultState = {
export default function sandboxReducer(state = defaultState, action) {
switch (action.type) {
case ActionTypes.OPEN_SANDBOX: {
return Object.assign({}, state, {
return {
...state,
isSandbox: true,
});
};
}
case ActionTypes.CLOSE_SANDBOX: {
return Object.assign({}, state, {
return {
...state,
isSandbox: false,
});
};
}
default:

View file

@ -13,9 +13,10 @@ export default function sectionsReducer(state = defaultState, action) {
name: action.name,
});
return Object.assign({}, state, {
sections,
});
return {
...state,
sections: sections,
};
}
case ActionTypes.UNREGISTER_SECTION: {
@ -23,9 +24,10 @@ export default function sectionsReducer(state = defaultState, action) {
const index = sections.findIndex(section => section.id === action.id);
sections.splice(index, 1);
return Object.assign({}, state, {
sections,
});
return {
...state,
sections: sections,
};
}
default:

View file

@ -6,9 +6,10 @@ import classNames from 'classnames';
export const KuiScreenReaderOnly = ({ children }) => {
const classes = classNames('kuiScreenReaderOnly', children.props.className);
const props = Object.assign({}, children.props, {
className: classes
});
const props = {
...children.props,
className: classes,
};
return cloneElement(children, props);
};

View file

@ -36,11 +36,12 @@ export class KuiOutsideClickDetector extends Component {
}
render() {
const props = Object.assign({}, this.props.children.props, {
const props = {
...this.props.children.props,
ref: node => {
this.wrapperRef = node;
},
});
};
const child = Children.only(this.props.children);
return cloneElement(child, props);