Skip to content

Commit

Permalink
Codemod $Shape to Partial in xplat, suppressing errors [4] (#36960)
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/react-native#36960

We're deprecating the unsafe `$Shape` and moving to the safe `Partial`: https://fb.workplace.com/groups/flowlang/posts/1251655088773485

I have previously codemodded all locations that do not cause errors. Now start on the remaining ones: codemod and suppress.

Changelog: [Internal]

Reviewed By: SamChou19815

Differential Revision: D45076273

fbshipit-source-id: 27ebf33370143e19751dbdcfcc1876cf3c586e14
  • Loading branch information
gkz authored and facebook-github-bot committed Apr 18, 2023
1 parent ba985d5 commit 6900e87
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 45 deletions.
2 changes: 1 addition & 1 deletion flow-typed/babel-traverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,7 @@ declare module '@babel/traverse' {
state: TState,
) => void;

declare export type VisitNodeObject<-TNode: BabelNode, TState> = $Shape<{
declare export type VisitNodeObject<-TNode: BabelNode, TState> = Partial<{
enter(path: NodePath<TNode>, state: TState): void,
exit(path: NodePath<TNode>, state: TState): void,
}>;
Expand Down
2 changes: 1 addition & 1 deletion flow-typed/npm/yargs_v17.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ declare module "yargs" {
...
};

declare type Options = $Shape<{
declare type Options = Partial<{
alias: string | Array<string>,
array: boolean,
boolean: boolean,
Expand Down
34 changes: 31 additions & 3 deletions packages/metro-config/src/loadConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,45 +117,56 @@ function mergeConfig<T: InputConfigT>(

resolver: {
...totalConfig.resolver,
// $FlowFixMe[exponential-spread]
...(nextConfig.resolver || {}),
dependencyExtractor:
nextConfig.resolver && nextConfig.resolver.dependencyExtractor != null
? resolve(nextConfig.resolver.dependencyExtractor)
: totalConfig.resolver.dependencyExtractor,
: // $FlowFixMe[incompatible-use]
totalConfig.resolver.dependencyExtractor,
hasteImplModulePath:
nextConfig.resolver && nextConfig.resolver.hasteImplModulePath != null
? resolve(nextConfig.resolver.hasteImplModulePath)
: totalConfig.resolver.hasteImplModulePath,
: // $FlowFixMe[incompatible-use]
totalConfig.resolver.hasteImplModulePath,
},
serializer: {
...totalConfig.serializer,
// $FlowFixMe[exponential-spread]
...(nextConfig.serializer || {}),
},
transformer: {
...totalConfig.transformer,
// $FlowFixMe[exponential-spread]
...(nextConfig.transformer || {}),
babelTransformerPath:
nextConfig.transformer &&
nextConfig.transformer.babelTransformerPath != null
? resolve(nextConfig.transformer.babelTransformerPath)
: totalConfig.transformer.babelTransformerPath,
: // $FlowFixMe[incompatible-use]
totalConfig.transformer.babelTransformerPath,
},
server: {
...totalConfig.server,
// $FlowFixMe[exponential-spread]
...(nextConfig.server || {}),
},
symbolicator: {
...totalConfig.symbolicator,
// $FlowFixMe[exponential-spread]
...(nextConfig.symbolicator || {}),
},
watcher: {
...totalConfig.watcher,
// $FlowFixMe[exponential-spread]
...nextConfig.watcher,
watchman: {
// $FlowFixMe[exponential-spread]
...totalConfig.watcher?.watchman,
...nextConfig.watcher?.watchman,
},
healthCheck: {
// $FlowFixMe[exponential-spread]
...totalConfig.watcher?.healthCheck,
// $FlowFixMe: Spreading shapes creates an explosion of union types
...nextConfig.watcher?.healthCheck,
Expand All @@ -180,16 +191,22 @@ async function loadMetroConfigFromDisk(
const rootPath = dirname(filepath);

const defaults = await getDefaultConfig(rootPath);
// $FlowFixMe[incompatible-variance]
// $FlowFixMe[incompatible-call]
const defaultConfig: ConfigT = mergeConfig(defaults, defaultConfigOverrides);

if (typeof configModule === 'function') {
// Get a default configuration based on what we know, which we in turn can pass
// to the function.

const resultedConfig = await configModule(defaultConfig);
// $FlowFixMe[incompatible-call]
// $FlowFixMe[incompatible-variance]
return mergeConfig(defaultConfig, resultedConfig);
}

// $FlowFixMe[incompatible-variance]
// $FlowFixMe[incompatible-call]
return mergeConfig(defaultConfig, configModule);
}

Expand All @@ -207,10 +224,12 @@ function overrideConfigWithArguments(
};

if (argv.port != null) {
// $FlowFixMe[incompatible-use]
output.server.port = Number(argv.port);
}

if (argv.runInspectorProxy != null) {
// $FlowFixMe[incompatible-use]
output.server.runInspectorProxy = Boolean(argv.runInspectorProxy);
}

Expand All @@ -223,14 +242,17 @@ function overrideConfigWithArguments(
}

if (argv.assetExts != null) {
// $FlowFixMe[incompatible-use]
output.resolver.assetExts = argv.assetExts;
}

if (argv.sourceExts != null) {
// $FlowFixMe[incompatible-use]
output.resolver.sourceExts = argv.sourceExts;
}

if (argv.platforms != null) {
// $FlowFixMe[incompatible-use]
output.resolver.platforms = argv.platforms;
}

Expand All @@ -239,6 +261,7 @@ function overrideConfigWithArguments(
}

if (argv.transformer != null) {
// $FlowFixMe[incompatible-use]
output.transformer.babelTransformerPath = argv.transformer;
}

Expand All @@ -255,6 +278,8 @@ function overrideConfigWithArguments(
// TODO: Ask if this is the way to go
}

// $FlowFixMe[incompatible-variance]
// $FlowFixMe[incompatible-call]
return mergeConfig(config, output);
}

Expand Down Expand Up @@ -298,6 +323,9 @@ async function loadConfig(

// Set the watchfolders to include the projectRoot, as Metro assumes that is
// the case
// $FlowFixMe[incompatible-variance]
// $FlowFixMe[incompatible-indexer]
// $FlowFixMe[incompatible-call]
return mergeConfig(configWithArgs, overriddenConfig);
}

Expand Down
86 changes: 47 additions & 39 deletions packages/metro-transform-plugins/src/constant-folding-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,56 +78,62 @@ function constantFoldingPlugin(context: {
const FunctionExpression: VisitNode<
BabelNodeFunctionExpression | BabelNodeArrowFunctionExpression,
State,
> = {
exit(path, state) {
const parentPath = path.parentPath;
const parentNode = parentPath?.node;

if (isVariableDeclarator(parentNode) && parentNode.id.name != null) {
const binding = parentPath?.scope.getBinding(parentNode.id.name);

if (binding && !binding.referenced) {
state.stripped = true;
parentPath?.remove();
> =
// $FlowFixMe[incompatible-type]
{
exit(path, state) {
const parentPath = path.parentPath;
const parentNode = parentPath?.node;

if (isVariableDeclarator(parentNode) && parentNode.id.name != null) {
const binding = parentPath?.scope.getBinding(parentNode.id.name);

if (binding && !binding.referenced) {
state.stripped = true;
parentPath?.remove();
}
}
}
},
};
},
};

const Conditional: VisitNode<
BabelNodeIfStatement | BabelNodeConditionalExpression,
State,
> = {
exit(path, state): void {
const node = path.node;
const result = evaluate(path.get('test'));

if (result.confident) {
state.stripped = true;
> =
// $FlowFixMe[incompatible-type]
{
exit(path, state): void {
const node = path.node;
const result = evaluate(path.get('test'));

if (result.confident) {
state.stripped = true;

if (result.value || node.alternate) {
// $FlowFixMe Flow error uncovered by typing Babel more strictly
path.replaceWith(result.value ? node.consequent : node.alternate);
} else if (!result.value) {
path.remove();
if (result.value || node.alternate) {
// $FlowFixMe Flow error uncovered by typing Babel more strictly
path.replaceWith(result.value ? node.consequent : node.alternate);
} else if (!result.value) {
path.remove();
}
}
}
},
};
},
};

const Expression: VisitNode<
BabelNodeUnaryExpression | BabelNodeBinaryExpression,
State,
> = {
exit(path) {
const result = evaluate(path);

if (result.confident) {
path.replaceWith(t.valueToNode(result.value));
path.skip();
}
},
};
> =
// $FlowFixMe[incompatible-type]
{
exit(path) {
const result = evaluate(path);

if (result.confident) {
path.replaceWith(t.valueToNode(result.value));
path.skip();
}
},
};

const LogicalExpression = {
exit(path: NodePath<BabelNodeLogicalExpression>) {
Expand Down Expand Up @@ -164,6 +170,7 @@ function constantFoldingPlugin(context: {
{
ArrowFunctionExpression: FunctionExpression,
ConditionalExpression: Conditional,
// $FlowFixMe[incompatible-call]
FunctionDeclaration,
FunctionExpression,
IfStatement: Conditional,
Expand All @@ -186,6 +193,7 @@ function constantFoldingPlugin(context: {

const visitor: Visitor<State> = {
BinaryExpression: Expression,
// $FlowFixMe[incompatible-type]
LogicalExpression,
Program: {...Program}, // Babel mutates objects passed.
UnaryExpression: Expression,
Expand Down
3 changes: 2 additions & 1 deletion packages/metro/src/DeltaBundler/__tests__/Graph-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {Graph} from '../Graph';

const {objectContaining} = expect;

type DependencyDataInput = $Shape<TransformResultDependency['data']>;
type DependencyDataInput = Partial<TransformResultDependency['data']>;

let mockedDependencies: Set<string> = new Set();
let mockedDependencyTree: Map<
Expand Down Expand Up @@ -304,6 +304,7 @@ beforeEach(async () => {
asyncType: null,
// $FlowFixMe[missing-empty-array-annot]
locs: [],
// $FlowFixMe[incompatible-call]
key: dep.data.key,
...dep.data,
},
Expand Down
6 changes: 6 additions & 0 deletions packages/metro/src/index.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ export type {MetroConfig};

async function getConfig(config: InputConfigT): Promise<ConfigT> {
const defaultConfig = await getDefaultConfig(config.projectRoot);
// $FlowFixMe[incompatible-variance]
// $FlowFixMe[incompatible-call]
return mergeConfig(defaultConfig, config);
}

Expand Down Expand Up @@ -198,6 +200,8 @@ const createConnectMiddleware = async function (
config: ConfigT,
options?: RunMetroOptions,
): Promise<MetroMiddleWare> {
// $FlowFixMe[incompatible-variance]
// $FlowFixMe[incompatible-call]
const metroServer = await runMetro(config, options);

let enhancedMiddleware: Middleware = metroServer.processRequest;
Expand Down Expand Up @@ -392,6 +396,8 @@ exports.runBuild = async (
map: string,
...
}> => {
// $FlowFixMe[incompatible-variance]
// $FlowFixMe[incompatible-call]
const metroServer = await runMetro(config, {
watch: false,
});
Expand Down

0 comments on commit 6900e87

Please sign in to comment.