1
1
import _generator from "@babel/generator"
2
- import parser from "@babel/parser"
3
- import traverse from "@babel/traverse"
4
- import t , {
2
+ import _parser from "@babel/parser"
3
+ import _traverse from "@babel/traverse"
4
+ import _t , {
5
5
addComment ,
6
6
BlockStatement ,
7
7
Declaration ,
@@ -34,6 +34,9 @@ interface NodeConfig {
34
34
// Babel is a CJS package and uses `default` as named binding (`exports.default =`).
35
35
// https://github.com/babel/babel/issues/15269.
36
36
const generator = ( _generator as any ) . default as typeof _generator || _generator
37
+ const t = ( _t as any ) . default as typeof _t || _t
38
+ const parser = ( _parser as any ) . default as typeof _parser || _parser
39
+ const traverse = ( _traverse as any ) . default as typeof _traverse || _traverse
37
40
38
41
export const builder = ( priorSource : string , _opts : { } ) => {
39
42
const sourceFile = parser . parse ( priorSource , { sourceType : "module" , plugins : [ "jsx" , "typescript" ] } )
@@ -44,7 +47,7 @@ export const builder = (priorSource: string, _opts: {}) => {
44
47
45
48
const existing = imports . find ( ( i ) => i . source . value === source )
46
49
if ( ! existing ) {
47
- const imports = [ ] as ( t . ImportSpecifier | t . ImportDefaultSpecifier ) [ ]
50
+ const imports = [ ] as ( _t . ImportSpecifier | _t . ImportDefaultSpecifier ) [ ]
48
51
if ( opts . mainImport ) {
49
52
imports . push ( t . importDefaultSpecifier ( t . identifier ( opts . mainImport ) ) )
50
53
}
@@ -121,11 +124,11 @@ export const builder = (priorSource: string, _opts: {}) => {
121
124
}
122
125
123
126
/** An internal API for describing a new area for inputting template info */
124
- const createScope = ( scopeName : string , scopeNode : t . Node , statements : Statement [ ] ) => {
127
+ const createScope = ( scopeName : string , scopeNode : _t . Node , statements : Statement [ ] ) => {
125
128
const addFunction = ( name : string ) => {
126
129
let functionNode = statements . find (
127
130
( s ) => t . isVariableDeclaration ( s ) && t . isIdentifier ( s . declarations [ 0 ] . id ) && s . declarations [ 0 ] . id . name === name
128
- ) as t . VariableDeclaration | undefined
131
+ ) as _t . VariableDeclaration | undefined
129
132
130
133
if ( ! functionNode ) {
131
134
functionNode = t . variableDeclaration ( "const" , [
@@ -134,7 +137,7 @@ export const builder = (priorSource: string, _opts: {}) => {
134
137
statements . push ( functionNode )
135
138
}
136
139
137
- const arrowFn = functionNode . declarations [ 0 ] . init as t . ArrowFunctionExpression
140
+ const arrowFn = functionNode . declarations [ 0 ] . init as _t . ArrowFunctionExpression
138
141
if ( ! t . isArrowFunctionExpression ( arrowFn ) ) throw new Error ( "Expected ArrowFunctionExpression" )
139
142
140
143
return {
@@ -154,7 +157,7 @@ export const builder = (priorSource: string, _opts: {}) => {
154
157
}
155
158
}
156
159
157
- const addVariableDeclaration = ( name : string , add : ( prior : t . Expression | undefined ) => t . Expression ) => {
160
+ const addVariableDeclaration = ( name : string , add : ( prior : _t . Expression | undefined ) => _t . Expression ) => {
158
161
const prior = statements . find (
159
162
( b ) => t . isVariableDeclaration ( b ) && t . isIdentifier ( b . declarations [ 0 ] . id ) && b . declarations [ 0 ] . id . name === name
160
163
)
@@ -256,13 +259,13 @@ export const builder = (priorSource: string, _opts: {}) => {
256
259
}
257
260
258
261
/** Experimental function for parsing out a graphql template tag, and ensuring certain fields have been called */
259
- const updateGraphQLTemplateTag = ( expression : t . Expression , path : string , modelFields : string [ ] ) => {
262
+ const updateGraphQLTemplateTag = ( expression : _t . Expression , path : string , modelFields : string [ ] ) => {
260
263
if ( path !== "." ) throw new Error ( "Only support updating the root of the graphql tag ATM" )
261
264
// @ts -expect-error - ts/js babel interop issue
262
265
traverse (
263
266
expression ,
264
267
{
265
- TaggedTemplateExpression ( path : traverse . NodePath < t . TaggedTemplateExpression > ) {
268
+ TaggedTemplateExpression ( path : _traverse . NodePath < _t . TaggedTemplateExpression > ) {
266
269
const { tag, quasi } = path . node
267
270
if ( t . isIdentifier ( tag ) && tag . name === "graphql" ) {
268
271
// This is the graphql query
0 commit comments