@dsplay/template-manifest 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/core.js +37 -5
  2. package/package.json +1 -1
package/core.js CHANGED
@@ -140,15 +140,46 @@ function getHookType(callee, scope) {
140
140
  return undefined;
141
141
  }
142
142
 
143
+ // True when `node` is a call of the shape `JSON.parse(<anything>.getData())` - the manual
144
+ // escape hatch some templates (mostly the bundler-less vanilla-JS ones) use to read the raw
145
+ // DSPLAY payload directly, bypassing `@dsplay/template-utils`'s hooks/global alias entirely.
146
+ function isGetDataCall(node) {
147
+ if (!node || node.type !== 'CallExpression') return false;
148
+ const { callee } = node;
149
+ if (callee.type !== 'MemberExpression' || callee.computed) return false;
150
+ if (callee.object.type !== 'Identifier' || callee.object.name !== 'JSON') return false;
151
+ if (callee.property.type !== 'Identifier' || callee.property.name !== 'parse') return false;
152
+
153
+ const arg = node.arguments[0];
154
+ if (!arg || arg.type !== 'CallExpression') return false;
155
+ if (arg.callee.type !== 'MemberExpression' || arg.callee.computed) return false;
156
+ return arg.callee.property.type === 'Identifier' && arg.callee.property.name === 'getData';
157
+ }
158
+
159
+ // True when `node` is an Identifier that (directly, or via one or more local `var x = ...`
160
+ // indirections) refers to the result of a `JSON.parse(<anything>.getData())` call.
161
+ function resolvesToGetDataResult(node, scope, seen = new Set()) {
162
+ if (!node) return false;
163
+ if (isGetDataCall(node)) return true;
164
+ if (node.type === 'Identifier') {
165
+ if (seen.has(node.name)) return false;
166
+ const binding = scope.getBinding(node.name);
167
+ const init = binding?.path?.isVariableDeclarator?.() ? binding.path.node.init : undefined;
168
+ return init ? resolvesToGetDataResult(init, scope, new Set(seen).add(node.name)) : false;
169
+ }
170
+ return false;
171
+ }
172
+
143
173
  // True when `node` is an expression representing "the template variables object" itself -
144
- // covers the ES default export, the React `useTemplate()` hook result, and the global UMD
145
- // alias's `.template` property (plus one level of local variable indirection for each).
174
+ // covers the ES default export, the React `useTemplate()` hook result, the global UMD alias's
175
+ // `.template` property, and a manually-parsed `JSON.parse(<...>.getData()).template` (plus one
176
+ // level of local variable indirection for each).
146
177
  function isTemplateAccessBase(node, scope, seen = new Set()) {
147
178
  if (!node) return false;
148
179
 
149
180
  if (node.type === 'MemberExpression' && !node.computed && node.property.type === 'Identifier'
150
181
  && node.property.name === 'template') {
151
- return resolvesToGlobalUtilsAlias(node.object, scope);
182
+ return resolvesToGlobalUtilsAlias(node.object, scope) || resolvesToGetDataResult(node.object, scope);
152
183
  }
153
184
 
154
185
  if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
@@ -277,8 +308,9 @@ function scanFile(code, table) {
277
308
  /**
278
309
  * Statically scans a template's source tree for DSPLAY template-variable reads
279
310
  * (`tval`/`useTemplateVal`-style calls, `useTemplate()`/`.template` destructuring or direct
280
- * property access, in both the ES-import and global-UMD-alias worlds) and returns the
281
- * discovered `{key, type, default?, subtypeGuess?}` entries.
311
+ * property access, in the ES-import, global-UMD-alias, and manually-parsed
312
+ * `JSON.parse(<...>.getData()).template` worlds) and returns the discovered
313
+ * `{key, type, default?, subtypeGuess?}` entries.
282
314
  * @param {string} srcDir - Directory to scan recursively for .js/.jsx files.
283
315
  * @returns {Array<{key: string, type: 'string'|'boolean'|'integer'|'float', default?: *, subtypeGuess?: string}>}
284
316
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dsplay/template-manifest",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "Statically scans a DSPLAY HTML template's source for its dsplay_template variables and captures its dsplay-data.js as example preview data, producing template-variables.json + template-example-data.json for the CMS to auto-detect variables and seed default preview values.",
5
5
  "type": "module",
6
6
  "main": "./core.js",