({ types: t }, options = {})
| 12 | * @param {boolean} [options.variableArity=true] If `false`, always passes exactly 3 arguments to the pragma function. |
| 13 | */ |
| 14 | export default function htmBabelPlugin({ types: t }, options = {}) { |
| 15 | const pragmaString = options.pragma===false ? false : options.pragma || 'h'; |
| 16 | const pragma = pragmaString===false ? false : dottedIdentifier(pragmaString); |
| 17 | const useBuiltIns = options.useBuiltIns; |
| 18 | const useNativeSpread = options.useNativeSpread; |
| 19 | const inlineVNodes = options.monomorphic || pragma===false; |
| 20 | const importDeclaration = pragmaImport(options.import || false); |
| 21 | |
| 22 | function pragmaImport(imp) { |
| 23 | if (pragmaString === false || imp === false) { |
| 24 | return null; |
| 25 | } |
| 26 | const pragmaRoot = t.identifier(pragmaString.split('.')[0]); |
| 27 | const { module, export: export_ } = typeof imp !== 'string' ? imp : { |
| 28 | module: imp, |
| 29 | export: null |
| 30 | }; |
| 31 | |
| 32 | let specifier; |
| 33 | if (export_ === '*') { |
| 34 | specifier = t.importNamespaceSpecifier(pragmaRoot); |
| 35 | } |
| 36 | else if (export_ === 'default') { |
| 37 | specifier = t.importDefaultSpecifier(pragmaRoot); |
| 38 | } |
| 39 | else { |
| 40 | specifier = t.importSpecifier(pragmaRoot, export_ ? t.identifier(export_) : pragmaRoot); |
| 41 | } |
| 42 | return t.importDeclaration([specifier], t.stringLiteral(module)); |
| 43 | } |
| 44 | |
| 45 | function dottedIdentifier(keypath) { |
| 46 | const path = keypath.split('.'); |
| 47 | let out; |
| 48 | for (let i=0; i<path.length; i++) { |
| 49 | const ident = propertyName(path[i]); |
| 50 | out = i===0 ? ident : t.memberExpression(out, ident); |
| 51 | } |
| 52 | return out; |
| 53 | } |
| 54 | |
| 55 | function patternStringToRegExp(str) { |
| 56 | const parts = str.split('/').slice(1); |
| 57 | const end = parts.pop() || ''; |
| 58 | return new RegExp(parts.join('/'), end); |
| 59 | } |
| 60 | |
| 61 | function propertyName(key) { |
| 62 | if (t.isValidIdentifier(key)) { |
| 63 | return t.identifier(key); |
| 64 | } |
| 65 | return t.stringLiteral(key); |
| 66 | } |
| 67 | |
| 68 | function objectProperties(obj) { |
| 69 | return Object.keys(obj).map(key => { |
| 70 | const values = obj[key].map(valueOrNode => |
| 71 | t.isNode(valueOrNode) ? valueOrNode : t.valueToNode(valueOrNode) |
nothing calls this directly
no test coverage detected
searching dependent graphs…