Skip to content

Commit ac081ac

Browse files
gkzfacebook-github-bot
authored andcommitted
Codemod Object.assign with object literal first argument to object spread in Xplat
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
1 parent 9f42f24 commit ac081ac

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

packages/metro-react-native-babel-transformer/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function buildBabelConfig(
170170
if (mayContainEditableReactComponents) {
171171
const hmrConfig = makeHMRConfig();
172172
hmrConfig.plugins = withExtrPlugins.concat(hmrConfig.plugins);
173-
config = Object.assign({}, config, hmrConfig);
173+
config = {...config, ...hmrConfig};
174174
}
175175
}
176176

packages/metro/src/Bundler/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function filterObject(
155155
object: AssetDataWithoutFiles,
156156
blockList: Set<string>,
157157
): AssetDataFiltered {
158-
const copied = Object.assign({}, object);
158+
const copied = {...object};
159159
for (const key of blockList) {
160160
delete copied[key];
161161
}

packages/metro/src/index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,11 @@ exports.runServer = async (
259259
if (secure || secureServerOptions != null) {
260260
let options = secureServerOptions;
261261
if (typeof secureKey === 'string' && typeof secureCert === 'string') {
262-
options = Object.assign(
263-
{
264-
key: fs.readFileSync(secureKey),
265-
cert: fs.readFileSync(secureCert),
266-
},
267-
secureServerOptions,
268-
);
262+
options = {
263+
key: fs.readFileSync(secureKey),
264+
cert: fs.readFileSync(secureCert),
265+
...secureServerOptions,
266+
};
269267
}
270268
httpServer = https.createServer(options, serverApp);
271269
} else {

0 commit comments

Comments
 (0)