Skip to content

Commit 38fcafe

Browse files
MaeIgfacebook-github-bot
authored andcommitted
Extract the function createParserErrorCapturer into a single function in the parsers/utils.js file (#34934)
Summary: This PR aims to reduce code duplication by extracting `createParserErrorCapturer` function from the flow and typescript folders into a shared parsers/utils.js file. It is a task of #34872: > Extract the function createParserErrorCapturer ([Flow](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/utils.js#L122-L143) [TypeScript](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/utils.js#L114-L135)) into a single function in the parsers/utils.js file and replace its invocation with this new function. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [Internal] [Changed] - Extract the function createParserErrorCapturer into a single function in the parsers/utils.js file Pull Request resolved: #34934 Test Plan: yarn flow: <img width="628" alt="image" src="https://user-images.githubusercontent.com/40902940/194948886-d6763e39-ea07-4004-86cc-5287c4783012.png"> yarn lint: <img width="509" alt="image" src="https://user-images.githubusercontent.com/40902940/194948916-3e54afa5-7e0b-4a61-ac18-8ec306d8c6d4.png"> yarn jest react-native-codegen: <img width="386" alt="image" src="https://user-images.githubusercontent.com/40902940/194948966-d1e5b12e-02ab-4d53-a4bf-6abaf4d70fbe.png"> Reviewed By: cipolleschi Differential Revision: D40256048 Pulled By: cipolleschi fbshipit-source-id: 098519a17d6e3128d236c639b246a706f9dbf66d
1 parent 966f3cd commit 38fcafe

File tree

9 files changed

+93
-68
lines changed

9 files changed

+93
-68
lines changed

packages/eslint-plugin-specs/react-native-modules.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function requireModuleParser() {
3535

3636
withBabelRegister(config, () => {
3737
RNModuleParser = require('react-native-codegen/src/parsers/flow/modules');
38-
RNParserUtils = require('react-native-codegen/src/parsers/flow/utils');
38+
RNParserUtils = require('react-native-codegen/src/parsers/utils');
3939
});
4040
} else {
4141
const config = {
@@ -45,7 +45,7 @@ function requireModuleParser() {
4545

4646
withBabelRegister(config, () => {
4747
RNModuleParser = require('react-native-codegen/lib/parsers/flow/modules');
48-
RNParserUtils = require('react-native-codegen/lib/parsers/flow/utils');
48+
RNParserUtils = require('react-native-codegen/lib/parsers/utils');
4949
});
5050
}
5151
}

packages/react-native-codegen/src/parsers/__tests__/utils-test.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict-local
78
* @format
89
* @oncall react_native
910
*/
1011

1112
'use strict';
12-
const {extractNativeModuleName} = require('../utils.js');
13+
14+
const {
15+
extractNativeModuleName,
16+
createParserErrorCapturer,
17+
} = require('../utils.js');
18+
const {ParserError} = require('../errors');
1319

1420
describe('extractnativeModuleName', () => {
1521
it('return filename when it ends with .js', () => {
@@ -63,3 +69,49 @@ describe('extractnativeModuleName', () => {
6369
expect(nativeModuleName).toBe('NativeModule');
6470
});
6571
});
72+
73+
describe('createParserErrorCapturer', () => {
74+
describe("when function doesn't throw", () => {
75+
it("returns result and doesn't change errors array", () => {
76+
const [errors, guard] = createParserErrorCapturer();
77+
const fn = () => 'result';
78+
79+
const result = guard(fn);
80+
expect(result).toBe('result');
81+
expect(errors).toHaveLength(0);
82+
});
83+
});
84+
85+
describe('when function throws a ParserError', () => {
86+
it('returns null and adds the error in errors array instead of throwing it', () => {
87+
const [errors, guard] = createParserErrorCapturer();
88+
const ErrorThrown = new ParserError(
89+
'moduleName',
90+
null,
91+
'Something went wrong :(',
92+
);
93+
const fn = () => {
94+
throw ErrorThrown;
95+
};
96+
97+
const result = guard(fn);
98+
expect(result).toBe(null);
99+
expect(errors).toHaveLength(1);
100+
expect(errors[0]).toEqual(ErrorThrown);
101+
expect(() => guard(fn)).not.toThrow();
102+
});
103+
});
104+
105+
describe('when function throws another error', () => {
106+
it("throws the error and doesn't change errors array", () => {
107+
const [errors, guard] = createParserErrorCapturer();
108+
const errorMessage = 'Something else went wrong :(';
109+
const fn = () => {
110+
throw new Error(errorMessage);
111+
};
112+
113+
expect(() => guard(fn)).toThrow(errorMessage);
114+
expect(errors).toHaveLength(0);
115+
});
116+
});
117+
});

packages/react-native-codegen/src/parsers/flow/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ import type {SchemaType} from '../../CodegenSchema.js';
1414
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
1515
const flowParser = require('flow-parser');
1616
const fs = require('fs');
17-
const {extractNativeModuleName} = require('../utils.js');
17+
const {
18+
extractNativeModuleName,
19+
createParserErrorCapturer,
20+
} = require('../utils.js');
1821
const {buildComponentSchema} = require('./components');
1922
const {wrapComponentSchema} = require('./components/schema');
2023
const {buildModuleSchema} = require('./modules');
2124
const {wrapModuleSchema} = require('../parsers-commons');
22-
const {
23-
createParserErrorCapturer,
24-
visit,
25-
isModuleRegistryCall,
26-
} = require('./utils');
25+
const {visit, isModuleRegistryCall} = require('./utils');
2726
const invariant = require('invariant');
2827

2928
function getConfigType(

packages/react-native-codegen/src/parsers/flow/modules/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import type {
2323
} from '../../../CodegenSchema.js';
2424

2525
import type {TypeDeclarationMap} from '../utils.js';
26-
import type {ParserErrorCapturer} from '../utils';
26+
import type {ParserErrorCapturer} from '../../utils';
2727
import type {NativeModuleTypeAnnotation} from '../../../CodegenSchema.js';
2828

2929
const {

packages/react-native-codegen/src/parsers/flow/utils.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
import type {TypeAliasResolutionStatus} from '../utils';
1414

15-
const {ParserError} = require('../errors');
16-
1715
/**
1816
* This FlowFixMe is supposed to refer to an InterfaceDeclaration or TypeAlias
1917
* declaration type. Unfortunately, we don't have those types, because flow-parser
@@ -119,29 +117,6 @@ function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {
119117
return value;
120118
}
121119

122-
export type ParserErrorCapturer = <T>(fn: () => T) => ?T;
123-
124-
function createParserErrorCapturer(): [
125-
Array<ParserError>,
126-
ParserErrorCapturer,
127-
] {
128-
const errors = [];
129-
function guard<T>(fn: () => T): ?T {
130-
try {
131-
return fn();
132-
} catch (error) {
133-
if (!(error instanceof ParserError)) {
134-
throw error;
135-
}
136-
errors.push(error);
137-
138-
return null;
139-
}
140-
}
141-
142-
return [errors, guard];
143-
}
144-
145120
// TODO(T71778680): Flow-type ASTNodes.
146121
function visit(
147122
astNode: $FlowFixMe,
@@ -213,7 +188,6 @@ function isModuleRegistryCall(node: $FlowFixMe): boolean {
213188
module.exports = {
214189
getValueFromTypes,
215190
resolveTypeAnnotation,
216-
createParserErrorCapturer,
217191
getTypes,
218192
visit,
219193
isModuleRegistryCall,

packages/react-native-codegen/src/parsers/typescript/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@
1313
import type {SchemaType} from '../../CodegenSchema.js';
1414
const babelParser = require('@babel/parser');
1515
const fs = require('fs');
16-
const {extractNativeModuleName} = require('../utils.js');
16+
const {
17+
extractNativeModuleName,
18+
createParserErrorCapturer,
19+
} = require('../utils.js');
1720
const {buildComponentSchema} = require('./components');
1821
const {wrapComponentSchema} = require('./components/schema');
1922
const {buildModuleSchema} = require('./modules');
2023
const {wrapModuleSchema} = require('../parsers-commons');
2124

22-
const {
23-
createParserErrorCapturer,
24-
visit,
25-
isModuleRegistryCall,
26-
} = require('./utils');
25+
const {visit, isModuleRegistryCall} = require('./utils');
2726
const invariant = require('invariant');
2827

2928
function getConfigType(

packages/react-native-codegen/src/parsers/typescript/modules/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import type {
2323
} from '../../../CodegenSchema.js';
2424

2525
import type {TypeDeclarationMap} from '../utils.js';
26-
import type {ParserErrorCapturer} from '../utils';
26+
import type {ParserErrorCapturer} from '../../utils';
2727
import type {NativeModuleTypeAnnotation} from '../../../CodegenSchema.js';
2828

2929
const {

packages/react-native-codegen/src/parsers/typescript/utils.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import type {TypeAliasResolutionStatus} from '../utils';
1414

15-
const {ParserError} = require('../errors');
1615
const {parseTopLevelType} = require('./parseTopLevelType');
1716

1817
/**
@@ -111,29 +110,6 @@ function resolveTypeAnnotation(
111110
};
112111
}
113112

114-
export type ParserErrorCapturer = <T>(fn: () => T) => ?T;
115-
116-
function createParserErrorCapturer(): [
117-
Array<ParserError>,
118-
ParserErrorCapturer,
119-
] {
120-
const errors = [];
121-
function guard<T>(fn: () => T): ?T {
122-
try {
123-
return fn();
124-
} catch (error) {
125-
if (!(error instanceof ParserError)) {
126-
throw error;
127-
}
128-
errors.push(error);
129-
130-
return null;
131-
}
132-
}
133-
134-
return [errors, guard];
135-
}
136-
137113
// TODO(T108222691): Use flow-types for @babel/parser
138114
function visit(
139115
astNode: $FlowFixMe,
@@ -204,7 +180,6 @@ function isModuleRegistryCall(node: $FlowFixMe): boolean {
204180

205181
module.exports = {
206182
resolveTypeAnnotation,
207-
createParserErrorCapturer,
208183
getTypes,
209184
visit,
210185
isModuleRegistryCall,

packages/react-native-codegen/src/parsers/utils.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
'use strict';
1212

13+
const {ParserError} = require('./errors');
14+
1315
const path = require('path');
1416

1517
export type TypeAliasResolutionStatus =
@@ -27,6 +29,30 @@ function extractNativeModuleName(filename: string): string {
2729
return path.basename(filename).split('.')[0];
2830
}
2931

32+
export type ParserErrorCapturer = <T>(fn: () => T) => ?T;
33+
34+
function createParserErrorCapturer(): [
35+
Array<ParserError>,
36+
ParserErrorCapturer,
37+
] {
38+
const errors = [];
39+
function guard<T>(fn: () => T): ?T {
40+
try {
41+
return fn();
42+
} catch (error) {
43+
if (!(error instanceof ParserError)) {
44+
throw error;
45+
}
46+
errors.push(error);
47+
48+
return null;
49+
}
50+
}
51+
52+
return [errors, guard];
53+
}
54+
3055
module.exports = {
3156
extractNativeModuleName,
57+
createParserErrorCapturer,
3258
};

0 commit comments

Comments
 (0)