@react-native/babel-plugin-codegen 0.0.6

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 +151 -0
  2. package/package.json +20 -0
package/index.js ADDED
@@ -0,0 +1,151 @@
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
+ * @format
8
+ */
9
+
10
+ 'use strict';
11
+
12
+ let flow, RNCodegen;
13
+
14
+ const {basename} = require('path');
15
+
16
+ try {
17
+ flow = require('react-native-codegen/src/parsers/flow');
18
+ RNCodegen = require('react-native-codegen/src/generators/RNCodegen');
19
+ } catch (e) {
20
+ // 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
+ RNCodegen = require('react-native-codegen/lib/generators/RNCodegen');
23
+ }
24
+
25
+ function generateViewConfig(filename, code) {
26
+ const schema = flow.parseString(code);
27
+
28
+ const libraryName = basename(filename).replace(/NativeComponent\.js$/, '');
29
+ return RNCodegen.generateViewConfig({
30
+ schema,
31
+ libraryName,
32
+ });
33
+ }
34
+
35
+ function isCodegenDeclaration(declaration) {
36
+ if (!declaration) {
37
+ return false;
38
+ }
39
+
40
+ if (
41
+ declaration.left &&
42
+ declaration.left.left &&
43
+ declaration.left.left.name === 'codegenNativeComponent'
44
+ ) {
45
+ return true;
46
+ } else if (
47
+ declaration.callee &&
48
+ declaration.callee.name &&
49
+ declaration.callee.name === 'codegenNativeComponent'
50
+ ) {
51
+ return true;
52
+ } else if (
53
+ declaration.type === 'TypeCastExpression' &&
54
+ declaration.expression &&
55
+ declaration.expression.callee &&
56
+ declaration.expression.callee.name &&
57
+ declaration.expression.callee.name === 'codegenNativeComponent'
58
+ ) {
59
+ return true;
60
+ }
61
+
62
+ return false;
63
+ }
64
+
65
+ module.exports = function ({parse, types: t}) {
66
+ return {
67
+ pre(state) {
68
+ this.code = state.code;
69
+ this.filename = state.opts.filename;
70
+ this.defaultExport = null;
71
+ this.commandsExport = null;
72
+ this.codeInserted = false;
73
+ },
74
+ visitor: {
75
+ ExportNamedDeclaration(path) {
76
+ if (this.codeInserted) {
77
+ return;
78
+ }
79
+
80
+ if (
81
+ path.node.declaration &&
82
+ path.node.declaration.declarations &&
83
+ path.node.declaration.declarations[0]
84
+ ) {
85
+ const firstDeclaration = path.node.declaration.declarations[0];
86
+
87
+ if (firstDeclaration.type === 'VariableDeclarator') {
88
+ if (
89
+ firstDeclaration.init.type === 'CallExpression' &&
90
+ firstDeclaration.init.callee.type === 'Identifier' &&
91
+ firstDeclaration.init.callee.name === 'codegenNativeCommands'
92
+ ) {
93
+ if (
94
+ firstDeclaration.id.type === 'Identifier' &&
95
+ firstDeclaration.id.name !== 'Commands'
96
+ ) {
97
+ throw path.buildCodeFrameError(
98
+ "Native commands must be exported with the name 'Commands'",
99
+ );
100
+ }
101
+ this.commandsExport = path;
102
+ return;
103
+ } else {
104
+ if (firstDeclaration.id.name === 'Commands') {
105
+ throw path.buildCodeFrameError(
106
+ "'Commands' is a reserved export and may only be used to export the result of codegenNativeCommands.",
107
+ );
108
+ }
109
+ }
110
+ }
111
+ } else if (path.node.specifiers && path.node.specifiers.length > 0) {
112
+ path.node.specifiers.forEach(specifier => {
113
+ if (
114
+ specifier.type === 'ExportSpecifier' &&
115
+ specifier.local.type === 'Identifier' &&
116
+ specifier.local.name === 'Commands'
117
+ ) {
118
+ throw path.buildCodeFrameError(
119
+ "'Commands' is a reserved export and may only be used to export the result of codegenNativeCommands.",
120
+ );
121
+ }
122
+ });
123
+ }
124
+ },
125
+ ExportDefaultDeclaration(path, state) {
126
+ if (isCodegenDeclaration(path.node.declaration)) {
127
+ this.defaultExport = path;
128
+ }
129
+ },
130
+
131
+ Program: {
132
+ exit(path) {
133
+ if (this.defaultExport) {
134
+ const viewConfig = generateViewConfig(this.filename, this.code);
135
+ this.defaultExport.replaceWithMultiple(
136
+ parse(viewConfig, {
137
+ babelrc: false,
138
+ browserslistConfigFile: false,
139
+ configFile: false,
140
+ }).program.body,
141
+ );
142
+ if (this.commandsExport != null) {
143
+ this.commandsExport.remove();
144
+ }
145
+ this.codeInserted = true;
146
+ }
147
+ },
148
+ },
149
+ },
150
+ };
151
+ };
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "version": "0.0.6",
3
+ "name": "@react-native/babel-plugin-codegen",
4
+ "description": "Babel plugin to generate native module and view manager code for React Native.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git@github.com:facebook/react-native.git",
8
+ "directory": "packages/babel-plugin-codegen"
9
+ },
10
+ "files": [
11
+ "index.js"
12
+ ],
13
+ "dependencies": {
14
+ "react-native-codegen": "*"
15
+ },
16
+ "devDependencies": {
17
+ "@babel/core": "^7.14.0"
18
+ },
19
+ "license": "MIT"
20
+ }