@react-native/babel-plugin-codegen 0.0.6 → 0.71.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +26 -6
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -9,23 +9,42 @@
9
9
 
10
10
  'use strict';
11
11
 
12
- let flow, RNCodegen;
12
+ let flowParser, typeScriptParser, RNCodegen;
13
13
 
14
14
  const {basename} = require('path');
15
15
 
16
16
  try {
17
- flow = require('react-native-codegen/src/parsers/flow');
17
+ flowParser = require('react-native-codegen/src/parsers/flow');
18
+ typeScriptParser = require('react-native-codegen/src/parsers/typescript');
18
19
  RNCodegen = require('react-native-codegen/src/generators/RNCodegen');
19
20
  } catch (e) {
20
21
  // Fallback to lib when source doesn't exit (e.g. when installed as a dev dependency)
21
- flow = require('react-native-codegen/lib/parsers/flow');
22
+ flowParser = require('react-native-codegen/lib/parsers/flow');
23
+ typeScriptParser = require('react-native-codegen/lib/parsers/typescript');
22
24
  RNCodegen = require('react-native-codegen/lib/generators/RNCodegen');
23
25
  }
24
26
 
27
+ function parse(filename, code) {
28
+ if (filename.endsWith('js')) {
29
+ return flowParser.parseString(code);
30
+ }
31
+
32
+ if (filename.endsWith('ts')) {
33
+ return typeScriptParser.parseString(code);
34
+ }
35
+
36
+ throw new Error(
37
+ `Unable to parse file '${filename}'. Unsupported filename extension.`,
38
+ );
39
+ }
40
+
25
41
  function generateViewConfig(filename, code) {
26
- const schema = flow.parseString(code);
42
+ const schema = parse(filename, code);
27
43
 
28
- const libraryName = basename(filename).replace(/NativeComponent\.js$/, '');
44
+ const libraryName = basename(filename).replace(
45
+ /NativeComponent\.(js|ts)$/,
46
+ '',
47
+ );
29
48
  return RNCodegen.generateViewConfig({
30
49
  schema,
31
50
  libraryName,
@@ -86,6 +105,7 @@ module.exports = function ({parse, types: t}) {
86
105
 
87
106
  if (firstDeclaration.type === 'VariableDeclarator') {
88
107
  if (
108
+ firstDeclaration.init &&
89
109
  firstDeclaration.init.type === 'CallExpression' &&
90
110
  firstDeclaration.init.callee.type === 'Identifier' &&
91
111
  firstDeclaration.init.callee.name === 'codegenNativeCommands'
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.6",
2
+ "version": "0.71.0",
3
3
  "name": "@react-native/babel-plugin-codegen",
4
4
  "description": "Babel plugin to generate native module and view manager code for React Native.",
5
5
  "repository": {