|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +import type {SchemaType} from '../../CodegenSchema.js'; |
| 14 | +const babelParser = require('@babel/parser'); |
| 15 | +const fs = require('fs'); |
| 16 | +const path = require('path'); |
| 17 | +const {buildComponentSchema} = require('./components'); |
| 18 | +const {wrapComponentSchema} = require('./components/schema'); |
| 19 | +const {buildModuleSchema} = require('./modules'); |
| 20 | +const {wrapModuleSchema} = require('./modules/schema'); |
| 21 | + |
| 22 | +const { |
| 23 | + createParserErrorCapturer, |
| 24 | + visit, |
| 25 | + isModuleRegistryCall, |
| 26 | +} = require('./utils'); |
| 27 | +const invariant = require('invariant'); |
| 28 | + |
| 29 | +function getConfigType( |
| 30 | + // TODO(T108222691): Use flow-types for @babel/parser |
| 31 | + ast: $FlowFixMe, |
| 32 | +): 'module' | 'component' | 'none' { |
| 33 | + let isComponent = false; |
| 34 | + let isModule = false; |
| 35 | + |
| 36 | + visit(ast, { |
| 37 | + CallExpression(node) { |
| 38 | + if ( |
| 39 | + node.callee.type === 'Identifier' && |
| 40 | + node.callee.name === 'codegenNativeComponent' |
| 41 | + ) { |
| 42 | + isComponent = true; |
| 43 | + } |
| 44 | + |
| 45 | + if (isModuleRegistryCall(node)) { |
| 46 | + isModule = true; |
| 47 | + } |
| 48 | + }, |
| 49 | + |
| 50 | + TSInterfaceDeclaration(node) { |
| 51 | + if ( |
| 52 | + Array.isArray(node.extends) && |
| 53 | + node.extends.some( |
| 54 | + extension => extension.expression.name === 'TurboModule', |
| 55 | + ) |
| 56 | + ) { |
| 57 | + isModule = true; |
| 58 | + } |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + if (isModule && isComponent) { |
| 63 | + throw new Error( |
| 64 | + 'Found type extending "TurboModule" and exported "codegenNativeComponent" declaration in one file. Split them into separated files.', |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + if (isModule) { |
| 69 | + return 'module'; |
| 70 | + } else if (isComponent) { |
| 71 | + return 'component'; |
| 72 | + } else { |
| 73 | + return 'none'; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function buildSchema(contents: string, filename: ?string): SchemaType { |
| 78 | + // Early return for non-Spec JavaScript files |
| 79 | + if ( |
| 80 | + !contents.includes('codegenNativeComponent') && |
| 81 | + !contents.includes('TurboModule') |
| 82 | + ) { |
| 83 | + return {modules: {}}; |
| 84 | + } |
| 85 | + |
| 86 | + const ast = babelParser.parse(contents, { |
| 87 | + sourceType: 'module', |
| 88 | + plugins: ['typescript'], |
| 89 | + }).program; |
| 90 | + |
| 91 | + const configType = getConfigType(ast); |
| 92 | + |
| 93 | + switch (configType) { |
| 94 | + case 'component': { |
| 95 | + return wrapComponentSchema(buildComponentSchema(ast)); |
| 96 | + } |
| 97 | + case 'module': { |
| 98 | + if (filename === undefined || filename === null) { |
| 99 | + throw new Error('Filepath expected while parasing a module'); |
| 100 | + } |
| 101 | + const hasteModuleName = path.basename(filename).replace(/\.tsx?$/, ''); |
| 102 | + |
| 103 | + const [parsingErrors, tryParse] = createParserErrorCapturer(); |
| 104 | + |
| 105 | + const schema = tryParse(() => |
| 106 | + buildModuleSchema(hasteModuleName, ast, tryParse), |
| 107 | + ); |
| 108 | + |
| 109 | + if (parsingErrors.length > 0) { |
| 110 | + /** |
| 111 | + * TODO(T77968131): We have two options: |
| 112 | + * - Throw the first error, but indicate there are more then one errors. |
| 113 | + * - Display all errors, nicely formatted. |
| 114 | + * |
| 115 | + * For the time being, we're just throw the first error. |
| 116 | + **/ |
| 117 | + |
| 118 | + throw parsingErrors[0]; |
| 119 | + } |
| 120 | + |
| 121 | + invariant( |
| 122 | + schema != null, |
| 123 | + 'When there are no parsing errors, the schema should not be null', |
| 124 | + ); |
| 125 | + |
| 126 | + return wrapModuleSchema(schema, hasteModuleName); |
| 127 | + } |
| 128 | + default: |
| 129 | + return {modules: {}}; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +function parseFile(filename: string): SchemaType { |
| 134 | + const contents = fs.readFileSync(filename, 'utf8'); |
| 135 | + |
| 136 | + return buildSchema(contents, filename); |
| 137 | +} |
| 138 | + |
| 139 | +function parseModuleFixture(filename: string): SchemaType { |
| 140 | + const contents = fs.readFileSync(filename, 'utf8'); |
| 141 | + |
| 142 | + return buildSchema(contents, 'path/NativeSampleTurboModule.ts'); |
| 143 | +} |
| 144 | + |
| 145 | +function parseString(contents: string, filename: ?string): SchemaType { |
| 146 | + return buildSchema(contents, filename); |
| 147 | +} |
| 148 | + |
| 149 | +module.exports = { |
| 150 | + parseFile, |
| 151 | + parseModuleFixture, |
| 152 | + parseString, |
| 153 | +}; |
0 commit comments