Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hermes-parser for Flow codegen specs #39036

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/eslint-plugin-specs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"@babel/plugin-transform-flow-strip-types": "^7.20.0",
"@babel/preset-flow": "^7.20.0",
"@react-native/codegen": "*",
"flow-parser": "^0.206.0",
"make-dir": "^2.1.0",
"pirates": "^4.0.1",
"source-map-support": "0.5.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-specs/react-native-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function rule(context) {
const [parsingErrors, tryParse] = createParserErrorCapturer();

const sourceCode = context.getSourceCode().getText();
const ast = parser.getAst(sourceCode);
const ast = parser.getAst(sourceCode, filename);

tryParse(() => {
buildModuleSchema(
Expand Down
3 changes: 2 additions & 1 deletion packages/react-native-codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
],
"dependencies": {
"@babel/parser": "^7.20.0",
"flow-parser": "^0.206.0",
"hermes-parser": "0.17.1",
"jscodeshift": "^0.14.0",
"nullthrows": "^1.1.1"
},
Expand All @@ -47,6 +47,7 @@
"@babel/preset-env": "^7.20.0",
"chalk": "^4.0.0",
"glob": "^7.1.1",
"hermes-estree": "0.17.1",
"invariant": "^2.2.4",
"micromatch": "^4.0.4",
"mkdirp": "^0.5.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ describe('buildSchema', () => {

expect(getConfigTypeSpy).toHaveBeenCalledTimes(1);
expect(getConfigTypeSpy).toHaveBeenCalledWith(
parser.getAst(contents),
parser.getAst(contents, 'fileName'),
Visitor,
);
expect(schema).toEqual({
Expand Down Expand Up @@ -770,7 +770,7 @@ describe('buildSchema', () => {

expect(getConfigTypeSpy).toHaveBeenCalledTimes(1);
expect(getConfigTypeSpy).toHaveBeenCalledWith(
parser.getAst(contents),
parser.getAst(contents, 'fileName'),
Visitor,
);
expect(schema).toEqual({
Expand Down Expand Up @@ -824,7 +824,7 @@ describe('buildSchema', () => {

expect(getConfigTypeSpy).toHaveBeenCalledTimes(1);
expect(getConfigTypeSpy).toHaveBeenCalledWith(
parser.getAst(contents),
parser.getAst(contents, 'fileName'),
Visitor,
);
expect(schema).toEqual({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

exports[`RN Codegen Flow Parser Fails with error message EMPTY_ENUM_NATIVE_MODULE 1`] = `"Module NativeSampleTurboModule: Failed parsing the enum SomeEnum in NativeSampleTurboModule with the error: Enums should have at least one member and member values can not be mixed- they all must be either blank, number, or string values."`;

exports[`RN Codegen Flow Parser Fails with error message MIXED_VALUES_ENUM_NATIVE_MODULE 1`] = `"Module NativeSampleTurboModule: Failed parsing the enum SomeEnum in NativeSampleTurboModule with the error: Enums should have at least one member and member values can not be mixed- they all must be either blank, number, or string values."`;
exports[`RN Codegen Flow Parser Fails with error message MIXED_VALUES_ENUM_NATIVE_MODULE 1`] = `
"Syntax error in path/NativeSampleTurboModule.js: cannot use string initializer in number enum (19:2)
STR = 'str',
^~~~~~~~~~~
note: start of enum body (17:21)
export enum SomeEnum {
^"
`;

exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_ARRAY_WITH_NO_TYPE_FOR_CONTENT 1`] = `"Module NativeSampleTurboModule: Generic 'Array' must have type parameters."`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('Flow Module Parser', () => {
import type {TurboModule} from 'RCTExport';
import * as TurboModuleRegistry from 'TurboModuleRegistry';
export interface Spec extends TurboModule {
+useArg(arg: any): void;
useArg(arg: any): void;
}
export default TurboModuleRegistry.get<Spec>('Foo');
`);
Expand All @@ -98,7 +98,7 @@ describe('Flow Module Parser', () => {
import type {TurboModule} from 'RCTExport';
import * as TurboModuleRegistry from 'TurboModuleRegistry';
export interface Spec extends TurboModule {
+useArg(boolean): void;
useArg(boolean): void;
}
export default TurboModuleRegistry.get<Spec>('Foo');
`);
Expand Down Expand Up @@ -145,7 +145,7 @@ describe('Flow Module Parser', () => {
${TYPE_ALIAS_DECLARATIONS}

export interface Spec extends TurboModule {
+useArg(${annotateArg(paramName, paramType)}): void;
useArg(${annotateArg(paramName, paramType)}): void;
}
export default TurboModuleRegistry.get<Spec>('Foo');
`);
Expand Down Expand Up @@ -356,7 +356,7 @@ describe('Flow Module Parser', () => {
type AnimalPointer = Animal;

export interface Spec extends TurboModule {
+useArg(${annotateArg('arg', 'AnimalPointer')}): void;
useArg(${annotateArg('arg', 'AnimalPointer')}): void;
}
export default TurboModuleRegistry.get<Spec>('Foo');
`);
Expand Down Expand Up @@ -693,7 +693,7 @@ describe('Flow Module Parser', () => {
import type {TurboModule} from 'RCTExport';
import * as TurboModuleRegistry from 'TurboModuleRegistry';
export interface Spec extends TurboModule {
+useArg(): void;
useArg(): void;
}
export default TurboModuleRegistry.get<Spec>('Foo');
`);
Expand Down Expand Up @@ -727,7 +727,7 @@ describe('Flow Module Parser', () => {
${TYPE_ALIAS_DECLARATIONS}

export interface Spec extends TurboModule {
+useArg(): ${annotateRet(flowType)};
useArg(): ${annotateRet(flowType)};
}
export default TurboModuleRegistry.get<Spec>('Foo');
`);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* 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.
*
* @flow strict
* @format
*/

'use strict';

import type {Program as ESTreeProgram} from 'hermes-estree';

const hermesParser = require('hermes-parser');

function parseFlowAndThrowErrors(
code: string,
options: $ReadOnly<{filename?: ?string}> = {},
): ESTreeProgram {
let ast;
try {
ast = hermesParser.parse(code, {
// Produce an ESTree-compliant AST
babel: false,
// Parse Flow without a pragma
flow: 'all',
...(options.filename != null ? {sourceFilename: options.filename} : {}),
});
} catch (e) {
if (options.filename != null) {
e.message = `Syntax error in ${options.filename}: ${e.message}`;
}
throw e;
}
return ast;
}

module.exports = {
parseFlowAndThrowErrors,
};
9 changes: 3 additions & 6 deletions packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ const {
getTypeAnnotation,
} = require('./components/componentsUtils');
const {flowTranslateTypeAnnotation} = require('./modules');
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
const flowParser = require('flow-parser');
const {parseFlowAndThrowErrors} = require('./parseFlowAndThrowErrors');
const fs = require('fs');
const invariant = require('invariant');

Expand Down Expand Up @@ -143,10 +142,8 @@ class FlowParser implements Parser {
return this.parseString(contents, 'path/NativeSampleTurboModule.js');
}

getAst(contents: string): $FlowFixMe {
return flowParser.parse(contents, {
enums: true,
});
getAst(contents: string, filename?: ?string): $FlowFixMe {
return parseFlowAndThrowErrors(contents, {filename});
}

getFunctionTypeAnnotationParameters(
Expand Down
4 changes: 3 additions & 1 deletion packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,11 @@ export interface Parser {
/**
* Given the content of a file, it returns an AST.
* @parameter contents: the content of the file.
* @parameter filename: the name of the file, if available.
* @throws if there is a syntax error.
* @returns: the AST of the file.
*/
getAst(contents: string): $FlowFixMe;
getAst(contents: string, filename?: ?string): $FlowFixMe;

/**
* Given a FunctionTypeAnnotation, it returns an array of its parameters.
Expand Down
9 changes: 3 additions & 6 deletions packages/react-native-codegen/src/parsers/parserMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ import invariant from 'invariant';
const {
UnsupportedObjectPropertyTypeAnnotationParserError,
} = require('./errors');
const {parseFlowAndThrowErrors} = require('./flow/parseFlowAndThrowErrors');
const {buildPropSchema} = require('./parsers-commons');
const {flattenProperties} = require('./typescript/components/componentsUtils');
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
const flowParser = require('flow-parser');

type ExtendsForProp = null | {
type: 'ReactNativeBuiltInType',
Expand Down Expand Up @@ -122,10 +121,8 @@ export class MockedParser implements Parser {
return schemaMock;
}

getAst(contents: string): $FlowFixMe {
return flowParser.parse(contents, {
enums: true,
});
getAst(contents: string, filename?: ?string): $FlowFixMe {
return parseFlowAndThrowErrors(contents, {filename});
}

getFunctionTypeAnnotationParameters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ function buildSchema(
return {modules: {}};
}

const ast = parser.getAst(contents);
const ast = parser.getAst(contents, filename);
const configType = getConfigType(ast, Visitor);

return buildSchemaFromConfigType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class TypeScriptParser implements Parser {
return this.parseString(contents, 'path/NativeSampleTurboModule.ts');
}

getAst(contents: string): $FlowFixMe {
getAst(contents: string, filename?: ?string): $FlowFixMe {
return babelParser.parse(contents, {
sourceType: 'module',
plugins: ['typescript'],
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5149,7 +5149,7 @@ flow-enums-runtime@^0.0.6:
resolved "https://registry.yarnpkg.com/flow-enums-runtime/-/flow-enums-runtime-0.0.6.tgz#5bb0cd1b0a3e471330f4d109039b7eba5cb3e787"
integrity sha512-3PYnM29RFXwvAN6Pc/scUfkI7RwhQ/xqyLUyPNlXUp9S40zI8nup9tUSrTLSVnWGBN38FNiGWbwZOB6uR4OGdw==

flow-parser@0.*, flow-parser@^0.206.0:
flow-parser@0.*:
version "0.206.0"
resolved "https://registry.yarnpkg.com/flow-parser/-/flow-parser-0.206.0.tgz#f4f794f8026535278393308e01ea72f31000bfef"
integrity sha512-HVzoK3r6Vsg+lKvlIZzaWNBVai+FXTX1wdYhz/wVlH13tb/gOdLXmlTqy6odmTBhT5UoWUbq0k8263Qhr9d88w==
Expand Down