mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
d992ae0448
Summary:
Codemod `Object.assign` with object literal first argument (e.g. `Object.assign({}, foo)`) to object spread.
This adds several suppressions for exposed errors. The codemod produces errors as `Object.assign` is more unsafe than object spread. For example, `Object.assign` doesn't handle indexers, nor does it handle inexact objects properly, and when the (currently unsealed) empty object is supplied as the first argument, it also leads to unsafe behaviour:
https://flow.org/try/#0FAehAIGJwSQOwCYFMAeSBOBnYyDGAbAQ3SXADdjwBbQgBwC5wBvAbUwBd0BLOAcwF1GHbnwC+AbmDAAFAHkARgCskudgDpCmTF15xpTQowCMogDTU6ASkbyA9rfxJCcS+PBhwAfm8ymwcAGG4Eam-gFqETS0oaKu7hAAyrQkhAjgGOi2WOCa6Si0KuxICFIe0PCohKo5AGZF6HlV7DgqRCTklDyVqowGQpw8vOYRahJSCsqqGlo6en3gAEQAFlwL5vLGZuBdKE1xHjtN4Li2AK74abZkGVzI4AAG8vfgUvphOYzLq6EB4BvBP3CEUOqhi+0SyScaQyWUwOThqAKqmKpQg0AAqnBME5HGkalwsOwcuheDIJoVptpdPotvMTNZmDV7Iw4KcqPIMLE3B5vN4gA
The codemod is safe to do, despite some Flow errors, as `Object.assign` and object spread are equivalent at runtime, with the exception that `Object.assign` triggers setters on the target object. However the codemod [does not run](https://github.com/eslint/eslint/blob/938dbdd6c310784cc8a7329efaeb0e34321b9e1f/lib/rules/prefer-object-spread.js#L283-L285) if the first argument (object literal) has getters/setters, so we are fine.
```
ag -l 'Object.assign\(' | xargs ag -l 'flow' | xargs js1 lint --rule '{"prefer-object-spread":2}' --fix
```
Some manual fixes
```
arc f
```
Reviewed By: SamChou19815
Differential Revision: D36023786
fbshipit-source-id: b682562e670410acf4175ba59ab285c7bdcfe052
52 lines
1.2 KiB
JavaScript
52 lines
1.2 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
* @flow
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
type truncateOptions = {
|
|
breakOnWords: boolean,
|
|
minDelta: number,
|
|
elipsis: string,
|
|
...
|
|
};
|
|
|
|
const defaultOptions = {
|
|
breakOnWords: true,
|
|
minDelta: 10, // Prevents truncating a tiny bit off the end
|
|
elipsis: '...',
|
|
};
|
|
|
|
// maxChars (including ellipsis)
|
|
const truncate = function (
|
|
str: ?string,
|
|
maxChars: number,
|
|
options?: truncateOptions,
|
|
): ?string {
|
|
options = {...defaultOptions, ...options};
|
|
if (
|
|
str &&
|
|
str.length &&
|
|
str.length - options.minDelta + options.elipsis.length >= maxChars
|
|
) {
|
|
// If the slice is happening in the middle of a wide char, add one more char
|
|
const extraChar =
|
|
str.charCodeAt(maxChars - options.elipsis.length) > 255 ? 1 : 0;
|
|
str = str.slice(0, maxChars - options.elipsis.length + 1 + extraChar);
|
|
if (options.breakOnWords) {
|
|
const ii = Math.max(str.lastIndexOf(' '), str.lastIndexOf('\n'));
|
|
str = str.slice(0, ii);
|
|
}
|
|
str = str.trim() + options.elipsis;
|
|
}
|
|
return str;
|
|
};
|
|
|
|
module.exports = truncate;
|