1
- import { IMPORT_DECLARATION , JS_EXPORT } from './constants'
1
+ import { DEFAULT_EXPORT_DECLARATION , IMPORT_DECLARATION , JS_EXPORT } from './constants'
2
+ import compose from '../../utils/compose'
2
3
import getPreprocessorTypeByAttribute from '../../utils/get-preprocessor-type-by-attribute'
3
4
import mergeOutputChunks from '../../utils/merge-output-chunks'
4
5
import preprocess from '../../utils/preprocess-node'
5
6
import recast from 'recast'
6
7
8
+ const builders = recast . types . builders
9
+
7
10
/**
8
11
* Find all the import statements
9
- * @param { Array } ast - tree structure containing the program code
12
+ * @param { Array } body - tree structure containing the program code
10
13
* @returns { Array } array containing only the import statements
11
14
*/
12
- function findImports ( ast ) {
13
- return ast . filter ( node => node . type === IMPORT_DECLARATION )
15
+ function findImports ( body ) {
16
+ return body . filter ( node => node . type === IMPORT_DECLARATION )
17
+ }
18
+
19
+ /**
20
+ * Find all the code in an ast program except for the import statements
21
+ * @param { Array } body - tree structure containing the program code
22
+ * @returns { Array } array containing all the program code except the import expressions
23
+ */
24
+ function filterNonImport ( body ) {
25
+ return body . filter ( node => node . type !== IMPORT_DECLARATION )
14
26
}
15
27
16
28
/**
@@ -22,6 +34,51 @@ function getProgramBody(ast) {
22
34
return ast . program . body
23
35
}
24
36
37
+ /**
38
+ * Find the `export default` expression
39
+ * @param { Array } body - program ast
40
+ * @returns { Object } the content of the default export statement
41
+ */
42
+ function getDefaultExportDeclaration ( body ) {
43
+ return body . find ( node => node . type === DEFAULT_EXPORT_DECLARATION )
44
+ }
45
+
46
+ /**
47
+ * Get the body of an expression call function (() => )()
48
+ * @param { Object } node node to parse
49
+ * @returns { Array } expression body
50
+ */
51
+ function getCallExpressionBody ( node ) {
52
+ return node . declaration . callee . body . body
53
+ }
54
+
55
+ /**
56
+ * Get the path to the body array where we will inject the tag logic code
57
+ * @param { Object } ast - ast program
58
+ * @returns { Array } the empty array where we will inject our tag logic
59
+ */
60
+ function getOutputInnerBodyScope ( ast ) {
61
+ return compose (
62
+ getCallExpressionBody ,
63
+ getDefaultExportDeclaration ,
64
+ getProgramBody
65
+ ) ( ast )
66
+ }
67
+
68
+ /**
69
+ * Remap the content of an ast converting the default export declaration into a return statement
70
+ * @param { Array } body - tree structure containing the program code
71
+ * @returns { Array } the body remapped containing the return statement
72
+ */
73
+ function transformExportDefaultIntoReturn ( body ) {
74
+ return body . map ( node => {
75
+ if ( node . type === DEFAULT_EXPORT_DECLARATION )
76
+ return builders . returnStatement ( node . declaration )
77
+
78
+ return node
79
+ } )
80
+ }
81
+
25
82
/**
26
83
* Generate the component javascript logic
27
84
* @param { Object } sourceNode - node generated by the riot compiler
@@ -36,10 +93,18 @@ export default async function javascript(sourceNode, source, options, { code, ma
36
93
const preprocessorOutput = await preprocess ( 'js' , preprocessorName , options , source , javascriptNode )
37
94
const outputAST = recast . parse ( JS_EXPORT )
38
95
const sourceAST = recast . parse ( preprocessorOutput . code )
96
+ const sourceAstBody = getProgramBody ( sourceAST )
97
+ const outputInnerBody = getOutputInnerBodyScope ( outputAST )
98
+
99
+ // insert the code into the scoped context filtering the import statements
100
+ // and convert the export default into "return" statement
101
+ outputInnerBody . push (
102
+ ...compose ( transformExportDefaultIntoReturn , filterNonImport ) ( sourceAstBody )
103
+ )
39
104
40
105
// move the imports to the top of the output
41
106
outputAST . program . body = [
42
- ...findImports ( getProgramBody ( sourceAST ) ) ,
107
+ ...findImports ( sourceAstBody ) ,
43
108
...getProgramBody ( outputAST )
44
109
]
45
110
0 commit comments