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

[Codegen 104] Add getResolvedTypeAnnotation to Parsers #37373

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
3 changes: 0 additions & 3 deletions packages/eslint-plugin-specs/react-native-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ function requireModuleParser() {
buildModuleSchema: RNParserCommons.buildModuleSchema,
createParserErrorCapturer: RNParserUtils.createParserErrorCapturer,
parser: new RNFlowParser.FlowParser(),
resolveTypeAnnotation: RNFlowParserUtils.resolveTypeAnnotation,
translateTypeAnnotation: RNModuleParser.flowTranslateTypeAnnotation,
};
}
Expand Down Expand Up @@ -149,7 +148,6 @@ function rule(context) {
buildModuleSchema,
createParserErrorCapturer,
parser,
resolveTypeAnnotation,
translateTypeAnnotation,
} = requireModuleParser();

Expand All @@ -164,7 +162,6 @@ function rule(context) {
ast,
tryParse,
parser,
resolveTypeAnnotation,
translateTypeAnnotation,
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ const flowParser = new FlowParser();

const {flowTranslateTypeAnnotation} = require('../flow/modules/index');
const typeScriptTranslateTypeAnnotation = require('../typescript/modules/index');
const {resolveTypeAnnotation} = require('../flow/utils');

beforeEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -409,7 +408,7 @@ describe('buildSchemaFromConfigType', () => {
(_ast, _parser) => componentSchemaMock,
);
const buildModuleSchemaMock = jest.fn(
(_0, _1, _2, _3, _4, _5) => moduleSchemaMock,
(_0, _1, _2, _3, _4) => moduleSchemaMock,
);

const buildSchemaFromConfigTypeHelper = (
Expand All @@ -424,7 +423,6 @@ describe('buildSchemaFromConfigType', () => {
buildComponentSchemaMock,
buildModuleSchemaMock,
parser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
);

Expand Down Expand Up @@ -507,7 +505,6 @@ describe('buildSchemaFromConfigType', () => {
astMock,
expect.any(Function),
parser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
);

Expand Down Expand Up @@ -679,7 +676,6 @@ describe('buildSchema', () => {
buildModuleSchema,
Visitor,
parser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
);

Expand Down Expand Up @@ -713,7 +709,6 @@ describe('buildSchema', () => {
buildModuleSchema,
Visitor,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
);

Expand Down Expand Up @@ -768,7 +763,6 @@ describe('buildSchema', () => {
buildModuleSchema,
Visitor,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
);

Expand Down Expand Up @@ -1064,7 +1058,6 @@ describe('buildModuleSchema', () => {
ast,
tryParse,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
),
).toThrow(expected);
Expand All @@ -1079,7 +1072,6 @@ describe('buildModuleSchema', () => {
ast,
tryParse,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
),
).not.toThrow();
Expand Down Expand Up @@ -1116,7 +1108,6 @@ describe('buildModuleSchema', () => {
ast,
tryParse,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
),
).toThrow(expected);
Expand All @@ -1131,7 +1122,6 @@ describe('buildModuleSchema', () => {
ast,
tryParse,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
),
).not.toThrow();
Expand Down Expand Up @@ -1171,7 +1161,6 @@ describe('buildModuleSchema', () => {
ast,
tryParse,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
),
).toThrow(expected);
Expand All @@ -1186,7 +1175,6 @@ describe('buildModuleSchema', () => {
ast,
tryParse,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
),
).not.toThrow();
Expand Down Expand Up @@ -1229,7 +1217,6 @@ describe('buildModuleSchema', () => {
ast,
tryParse,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import type {
import type {Parser} from '../../parser';
import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';

const {resolveTypeAnnotation} = require('../utils');
const {
unwrapNullable,
wrapNullable,
Expand Down Expand Up @@ -60,7 +59,7 @@ function translateTypeAnnotation(
parser: Parser,
): Nullable<NativeModuleTypeAnnotation> {
const {nullable, typeAnnotation, typeResolutionStatus} =
resolveTypeAnnotation(flowTypeAnnotation, types);
parser.getResolvedTypeAnnotation(flowTypeAnnotation, types);

switch (typeAnnotation.type) {
case 'GenericTypeAnnotation': {
Expand Down
79 changes: 76 additions & 3 deletions packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ import type {
} from '../../CodegenSchema';
import type {ParserType} from '../errors';
import type {GetSchemaInfoFN, GetTypeAnnotationFN, Parser} from '../parser';
import type {ParserErrorCapturer, TypeDeclarationMap, PropAST} from '../utils';
import type {
ParserErrorCapturer,
TypeDeclarationMap,
PropAST,
TypeResolutionStatus,
} from '../utils';
const invariant = require('invariant');

const {
getSchemaInfo,
Expand All @@ -40,7 +46,6 @@ const {Visitor} = require('../parsers-primitives');
const {buildComponentSchema} = require('./components');
const {wrapComponentSchema} = require('../schema.js');
const {buildModuleSchema} = require('../parsers-commons.js');
const {resolveTypeAnnotation} = require('./utils');

const fs = require('fs');

Expand Down Expand Up @@ -111,7 +116,6 @@ class FlowParser implements Parser {
buildModuleSchema,
Visitor,
this,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
);
}
Expand Down Expand Up @@ -356,6 +360,75 @@ class FlowParser implements Parser {
getGetTypeAnnotationFN(): GetTypeAnnotationFN {
return getTypeAnnotation;
}

getResolvedTypeAnnotation(
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
): {
nullable: boolean,
typeAnnotation: $FlowFixMe,
typeResolutionStatus: TypeResolutionStatus,
} {
invariant(
typeAnnotation != null,
'resolveTypeAnnotation(): typeAnnotation cannot be null',
);

let node = typeAnnotation;
let nullable = false;
let typeResolutionStatus: TypeResolutionStatus = {
successful: false,
};

for (;;) {
if (node.type === 'NullableTypeAnnotation') {
nullable = true;
node = node.typeAnnotation;
continue;
}

if (node.type !== 'GenericTypeAnnotation') {
break;
}

const resolvedTypeAnnotation = types[node.id.name];
if (resolvedTypeAnnotation == null) {
break;
}

switch (resolvedTypeAnnotation.type) {
case 'TypeAlias': {
typeResolutionStatus = {
successful: true,
type: 'alias',
name: node.id.name,
};
node = resolvedTypeAnnotation.right;
break;
}
case 'EnumDeclaration': {
typeResolutionStatus = {
successful: true,
type: 'enum',
name: node.id.name,
};
node = resolvedTypeAnnotation.body;
break;
}
default: {
throw new TypeError(
`A non GenericTypeAnnotation must be a type declaration ('TypeAlias') or enum ('EnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
);
}
}
}

return {
nullable: nullable,
typeAnnotation: node,
typeResolutionStatus,
};
}
}

module.exports = {
Expand Down
75 changes: 1 addition & 74 deletions packages/react-native-codegen/src/parsers/flow/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,79 +10,7 @@

'use strict';

import type {TypeResolutionStatus, TypeDeclarationMap, ASTNode} from '../utils';

const invariant = require('invariant');

function resolveTypeAnnotation(
// TODO(T71778680): This is an Flow TypeAnnotation. Flow-type this
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
): {
nullable: boolean,
typeAnnotation: $FlowFixMe,
typeResolutionStatus: TypeResolutionStatus,
} {
invariant(
typeAnnotation != null,
'resolveTypeAnnotation(): typeAnnotation cannot be null',
);

let node = typeAnnotation;
let nullable = false;
let typeResolutionStatus: TypeResolutionStatus = {
successful: false,
};

for (;;) {
if (node.type === 'NullableTypeAnnotation') {
nullable = true;
node = node.typeAnnotation;
continue;
}

if (node.type !== 'GenericTypeAnnotation') {
break;
}

const resolvedTypeAnnotation = types[node.id.name];
if (resolvedTypeAnnotation == null) {
break;
}

switch (resolvedTypeAnnotation.type) {
case 'TypeAlias': {
typeResolutionStatus = {
successful: true,
type: 'alias',
name: node.id.name,
};
node = resolvedTypeAnnotation.right;
break;
}
case 'EnumDeclaration': {
typeResolutionStatus = {
successful: true,
type: 'enum',
name: node.id.name,
};
node = resolvedTypeAnnotation.body;
break;
}
default: {
throw new TypeError(
`A non GenericTypeAnnotation must be a type declaration ('TypeAlias') or enum ('EnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
);
}
}
}

return {
nullable: nullable,
typeAnnotation: node,
typeResolutionStatus,
};
}
import type {TypeDeclarationMap, ASTNode} from '../utils';

function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {
if (value.type === 'GenericTypeAnnotation' && types[value.id.name]) {
Expand All @@ -93,5 +21,4 @@ function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {

module.exports = {
getValueFromTypes,
resolveTypeAnnotation,
};
10 changes: 10 additions & 0 deletions packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type {
TypeDeclarationMap,
PropAST,
ASTNode,
TypeResolutionStatus,
} from './utils';

export type GetTypeAnnotationFN = (
Expand Down Expand Up @@ -318,4 +319,13 @@ export interface Parser {
getGetTypeAnnotationFN(): GetTypeAnnotationFN;

getGetSchemaInfoFN(): GetSchemaInfoFN;

getResolvedTypeAnnotation(
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
): {
nullable: boolean,
typeAnnotation: $FlowFixMe,
typeResolutionStatus: TypeResolutionStatus,
};
}
Loading