|
| 1 | +import type * as d from '@stencil/core/declarations'; |
| 2 | +import ts from 'typescript'; |
| 3 | + |
| 4 | +import { HOST_REF_ARG } from './constants'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Create a binding for an `ElementInternals` object compatible with a |
| 8 | + * lazy-load ready Stencil component. |
| 9 | + * |
| 10 | + * In order to create a lazy-loaded form-associated component we need to access |
| 11 | + * the underlying host element (via the "$hostElement$" prop on {@link d.HostRef}) |
| 12 | + * to make the `attachInternals` call on the right element. This means that the |
| 13 | + * code generated by this function depends on there being a variable in scope |
| 14 | + * called {@link HOST_REF_ARG} with type {@link HTMLElement}. |
| 15 | + * |
| 16 | + * If an `@AttachInternals` decorator is present on a component like this: |
| 17 | + * |
| 18 | + * ```ts |
| 19 | + * @AttachInternals() |
| 20 | + * internals: ElementInternals; |
| 21 | + * ``` |
| 22 | + * |
| 23 | + * then this transformer will create syntax nodes which represent the |
| 24 | + * following TypeScript source: |
| 25 | + * |
| 26 | + * ```ts |
| 27 | + * if (hostRef.$hostElement$["s-ei"]) { |
| 28 | + * this.internals = hostRef.$hostElement$["s-ei"]; |
| 29 | + * } else { |
| 30 | + * this.internals = hostRef.$hostElement$.attachInternals(); |
| 31 | + * hostRef.$hostElement$["s-ei"] = this.internals; |
| 32 | + * } |
| 33 | + * ``` |
| 34 | + * |
| 35 | + * The `"s-ei"` prop on a {@link d.HostElement} may hold a reference to the |
| 36 | + * `ElementInternals` instance for that host. We store a reference to it |
| 37 | + * there in order to support HMR because `.attachInternals` may only be |
| 38 | + * called on an `HTMLElement` one time, so we need to store a reference to |
| 39 | + * the returned value across HMR updates. |
| 40 | + * |
| 41 | + * @param cmp metadata about the component of interest, gathered during compilation |
| 42 | + * @returns a list of expression statements |
| 43 | + */ |
| 44 | +export function createLazyAttachInternalsBinding(cmp: d.ComponentCompilerMeta): ts.Statement[] { |
| 45 | + if (cmp.formAssociated && cmp.attachInternalsMemberName) { |
| 46 | + return [ |
| 47 | + ts.factory.createIfStatement( |
| 48 | + // the condition for the `if` statement here is just whether the |
| 49 | + // following is defined: |
| 50 | + // |
| 51 | + // ```ts |
| 52 | + // hostRef.$hostElement$["s-ei"] |
| 53 | + // ``` |
| 54 | + hostRefElementInternalsPropAccess(), |
| 55 | + ts.factory.createBlock( |
| 56 | + [ |
| 57 | + // this `ts.factory` call creates the following statement: |
| 58 | + // |
| 59 | + // ```ts |
| 60 | + // this.${ cmp.formInternalsMemberName } = hostRef.$hostElement$['s-ei']; |
| 61 | + // ``` |
| 62 | + ts.factory.createExpressionStatement( |
| 63 | + ts.factory.createBinaryExpression( |
| 64 | + ts.factory.createPropertyAccessExpression( |
| 65 | + ts.factory.createThis(), |
| 66 | + // use the name set on the {@link d.ComponentCompilerMeta} |
| 67 | + ts.factory.createIdentifier(cmp.attachInternalsMemberName), |
| 68 | + ), |
| 69 | + ts.factory.createToken(ts.SyntaxKind.EqualsToken), |
| 70 | + hostRefElementInternalsPropAccess(), |
| 71 | + ), |
| 72 | + ), |
| 73 | + ], |
| 74 | + true, |
| 75 | + ), |
| 76 | + ts.factory.createBlock( |
| 77 | + [ |
| 78 | + // this `ts.factory` call creates the following statement: |
| 79 | + // |
| 80 | + // ```ts |
| 81 | + // this.${ cmp.attachInternalsMemberName } = hostRef.$hostElement$.attachInternals(); |
| 82 | + // ``` |
| 83 | + ts.factory.createExpressionStatement( |
| 84 | + ts.factory.createBinaryExpression( |
| 85 | + ts.factory.createPropertyAccessExpression( |
| 86 | + ts.factory.createThis(), |
| 87 | + // use the name set on the {@link d.ComponentCompilerMeta} |
| 88 | + ts.factory.createIdentifier(cmp.attachInternalsMemberName), |
| 89 | + ), |
| 90 | + ts.factory.createToken(ts.SyntaxKind.EqualsToken), |
| 91 | + ts.factory.createCallExpression( |
| 92 | + ts.factory.createPropertyAccessExpression( |
| 93 | + ts.factory.createPropertyAccessExpression( |
| 94 | + ts.factory.createIdentifier(HOST_REF_ARG), |
| 95 | + ts.factory.createIdentifier('$hostElement$'), |
| 96 | + ), |
| 97 | + ts.factory.createIdentifier('attachInternals'), |
| 98 | + ), |
| 99 | + undefined, |
| 100 | + [], |
| 101 | + ), |
| 102 | + ), |
| 103 | + ), |
| 104 | + // this `ts.factory` call produces the following: |
| 105 | + // |
| 106 | + // ```ts |
| 107 | + // hostRef.$hostElement$['s-ei'] = this.${ cmp.attachInternalsMemberName }; |
| 108 | + // ``` |
| 109 | + ts.factory.createExpressionStatement( |
| 110 | + ts.factory.createBinaryExpression( |
| 111 | + hostRefElementInternalsPropAccess(), |
| 112 | + ts.factory.createToken(ts.SyntaxKind.EqualsToken), |
| 113 | + ts.factory.createPropertyAccessExpression( |
| 114 | + ts.factory.createThis(), |
| 115 | + // use the name set on the {@link d.ComponentCompilerMeta} |
| 116 | + ts.factory.createIdentifier(cmp.attachInternalsMemberName), |
| 117 | + ), |
| 118 | + ), |
| 119 | + ), |
| 120 | + ], |
| 121 | + true, |
| 122 | + ), |
| 123 | + ), |
| 124 | + ]; |
| 125 | + } else { |
| 126 | + return []; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Create TS syntax nodes which represent accessing the `"s-ei"` (stencil |
| 132 | + * element internals) property on `$hostElement$` (a {@link d.HostElement}) on a |
| 133 | + * {@link d.HostRef} element which is called {@link HOST_REF_ARG}. |
| 134 | + * |
| 135 | + * The corresponding TypeScript source will look like: |
| 136 | + * |
| 137 | + * ```ts |
| 138 | + * hostRef.$hostElement$["s-ei"] |
| 139 | + * ``` |
| 140 | + * |
| 141 | + * @returns TS syntax nodes |
| 142 | + */ |
| 143 | +function hostRefElementInternalsPropAccess(): ts.ElementAccessExpression { |
| 144 | + return ts.factory.createElementAccessExpression( |
| 145 | + ts.factory.createPropertyAccessExpression( |
| 146 | + ts.factory.createIdentifier(HOST_REF_ARG), |
| 147 | + ts.factory.createIdentifier('$hostElement$'), |
| 148 | + ), |
| 149 | + ts.factory.createStringLiteral('s-ei'), |
| 150 | + ); |
| 151 | +} |
0 commit comments