|
| 1 | +'use strict' |
| 2 | + |
| 3 | +module.exports = buildJsx |
| 4 | + |
| 5 | +var walk = require('estree-walker').walk |
| 6 | +var isIdentifierName = require('estree-util-is-identifier-name').name |
| 7 | + |
| 8 | +function buildJsx(tree, options) { |
| 9 | + var settings = options || {} |
| 10 | + var pragma = settings.pragma |
| 11 | + var pragmaFrag = settings.pragmaFrag |
| 12 | + |
| 13 | + walk(tree, {enter: enter, leave: leave}) |
| 14 | + |
| 15 | + return tree |
| 16 | + |
| 17 | + // When entering the program, check all comments, and prefer comments over |
| 18 | + // config. |
| 19 | + function enter(node) { |
| 20 | + var comments |
| 21 | + var index |
| 22 | + var match |
| 23 | + |
| 24 | + if (node.type === 'Program') { |
| 25 | + comments = node.comments || [] |
| 26 | + index = -1 |
| 27 | + |
| 28 | + while (++index < comments.length) { |
| 29 | + if ((match = /@jsx\s+(\S+)/.exec(comments[index].value))) { |
| 30 | + pragma = match[1] |
| 31 | + } |
| 32 | + |
| 33 | + if ((match = /@jsxFrag\s+(\S+)/.exec(comments[index].value))) { |
| 34 | + pragmaFrag = match[1] |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // Transform JSX. |
| 41 | + // eslint-disable-next-line complexity |
| 42 | + function leave(node) { |
| 43 | + var parameters |
| 44 | + var fields |
| 45 | + var objects |
| 46 | + var index |
| 47 | + var child |
| 48 | + var name |
| 49 | + var props |
| 50 | + var attributes |
| 51 | + |
| 52 | + if (node.type !== 'JSXElement' && node.type !== 'JSXFragment') { |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + parameters = [] |
| 57 | + index = -1 |
| 58 | + |
| 59 | + // Figure out `children`. |
| 60 | + while (++index < node.children.length) { |
| 61 | + child = node.children[index] |
| 62 | + |
| 63 | + if (child.type === 'JSXExpressionContainer') { |
| 64 | + child = child.expression |
| 65 | + |
| 66 | + // Ignore empty expressions. |
| 67 | + if (child.type === 'JSXEmptyExpression') continue |
| 68 | + } else if (child.type === 'JSXText') { |
| 69 | + child = create(child, { |
| 70 | + type: 'Literal', |
| 71 | + value: child.value |
| 72 | + // Replace tabs w/ spaces. |
| 73 | + .replace(/\t/g, ' ') |
| 74 | + // Use line feeds, drop spaces around them. |
| 75 | + .replace(/ *(\r?\n|\r) */g, '\n') |
| 76 | + // Collapse multiple line feeds. |
| 77 | + .replace(/\n+/g, '\n') |
| 78 | + // Drop final line feeds. |
| 79 | + .replace(/\n+$/, '') |
| 80 | + // Replace line feeds with spaces. |
| 81 | + .replace(/\n/g, ' ') |
| 82 | + }) |
| 83 | + |
| 84 | + // Ignore collapsible text. |
| 85 | + if (!child.value) continue |
| 86 | + } |
| 87 | + // Otherwise, this is an already compiled call. |
| 88 | + |
| 89 | + parameters.push(child) |
| 90 | + } |
| 91 | + |
| 92 | + // Do the stuff needed for elements. |
| 93 | + if (node.openingElement) { |
| 94 | + name = toIdentifier(node.openingElement.name) |
| 95 | + |
| 96 | + // If the name could be an identifier, but start with something other than |
| 97 | + // a lowercase letter, it’s not a component. |
| 98 | + if (name.type === 'Identifier' && /^[a-z]/.test(name.name)) { |
| 99 | + name = create(name, {type: 'Literal', value: name.name}) |
| 100 | + } |
| 101 | + |
| 102 | + attributes = node.openingElement.attributes |
| 103 | + objects = [] |
| 104 | + fields = [] |
| 105 | + index = -1 |
| 106 | + |
| 107 | + // Place props in the right order, because we might have duplicates |
| 108 | + // in them and what’s spread in. |
| 109 | + while (++index < attributes.length) { |
| 110 | + if (attributes[index].type === 'JSXSpreadAttribute') { |
| 111 | + if (fields.length) { |
| 112 | + objects.push({type: 'ObjectExpression', properties: fields}) |
| 113 | + fields = [] |
| 114 | + } |
| 115 | + |
| 116 | + objects.push(attributes[index].argument) |
| 117 | + } else { |
| 118 | + fields.push(toProperty(attributes[index])) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if (fields.length) { |
| 123 | + objects.push({type: 'ObjectExpression', properties: fields}) |
| 124 | + } |
| 125 | + |
| 126 | + if (objects.length > 1) { |
| 127 | + // Don’t mutate the first object, shallow clone instead. |
| 128 | + if (objects[0].type !== 'ObjectExpression') { |
| 129 | + objects.unshift({type: 'ObjectExpression', properties: []}) |
| 130 | + } |
| 131 | + |
| 132 | + props = { |
| 133 | + type: 'CallExpression', |
| 134 | + callee: toMemberExpression('Object.assign'), |
| 135 | + arguments: objects |
| 136 | + } |
| 137 | + } else if (objects.length) { |
| 138 | + props = objects[0] |
| 139 | + } |
| 140 | + } |
| 141 | + // …and fragments. |
| 142 | + else { |
| 143 | + name = toMemberExpression(pragmaFrag || 'React.Fragment') |
| 144 | + } |
| 145 | + |
| 146 | + // There are props or children. |
| 147 | + if (props || parameters.length) { |
| 148 | + parameters.unshift(props || {type: 'Literal', value: null}) |
| 149 | + } |
| 150 | + |
| 151 | + parameters.unshift(name) |
| 152 | + |
| 153 | + this.replace( |
| 154 | + create(node, { |
| 155 | + type: 'CallExpression', |
| 156 | + callee: toMemberExpression(pragma || 'React.createElement'), |
| 157 | + arguments: parameters |
| 158 | + }) |
| 159 | + ) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +function toProperty(node) { |
| 164 | + var value |
| 165 | + |
| 166 | + if (node.value) { |
| 167 | + if (node.value.type === 'JSXExpressionContainer') { |
| 168 | + value = node.value.expression |
| 169 | + } |
| 170 | + // Could be an element, fragment, or string. |
| 171 | + else { |
| 172 | + value = node.value |
| 173 | + // Remove `raw` so we don’t get character references in strings. |
| 174 | + delete value.raw |
| 175 | + } |
| 176 | + } |
| 177 | + // Boolean prop. |
| 178 | + else { |
| 179 | + value = {type: 'Literal', value: true} |
| 180 | + } |
| 181 | + |
| 182 | + return create(node, { |
| 183 | + type: 'Property', |
| 184 | + key: toIdentifier(node.name), |
| 185 | + value: value, |
| 186 | + kind: 'init' |
| 187 | + }) |
| 188 | +} |
| 189 | + |
| 190 | +function toIdentifier(node) { |
| 191 | + return create( |
| 192 | + node, |
| 193 | + node.type === 'JSXMemberExpression' |
| 194 | + ? { |
| 195 | + type: 'MemberExpression', |
| 196 | + object: toIdentifier(node.object), |
| 197 | + property: toIdentifier(node.property) |
| 198 | + } |
| 199 | + : node.type === 'JSXNamespacedName' |
| 200 | + ? {type: 'Literal', value: node.namespace.name + ':' + node.name.name} |
| 201 | + : // Must be `JSXIdentifier`. |
| 202 | + isIdentifierName(node.name) |
| 203 | + ? {type: 'Identifier', name: node.name} |
| 204 | + : {type: 'Literal', value: node.name} |
| 205 | + ) |
| 206 | +} |
| 207 | + |
| 208 | +function toMemberExpression(id) { |
| 209 | + var identifiers = id.split('.') |
| 210 | + var index = -1 |
| 211 | + var result |
| 212 | + var prop |
| 213 | + |
| 214 | + while (++index < identifiers.length) { |
| 215 | + prop = {type: 'Identifier', name: identifiers[index]} |
| 216 | + result = index |
| 217 | + ? {type: 'MemberExpression', object: result, property: prop} |
| 218 | + : prop |
| 219 | + } |
| 220 | + |
| 221 | + return result |
| 222 | +} |
| 223 | + |
| 224 | +function create(template, node) { |
| 225 | + var fields = ['start', 'end', 'loc', 'range', 'comments'] |
| 226 | + var index = -1 |
| 227 | + var field |
| 228 | + |
| 229 | + while (++index < fields.length) { |
| 230 | + field = fields[index] |
| 231 | + if (field in template) { |
| 232 | + node[field] = template[field] |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + return node |
| 237 | +} |
0 commit comments