@dsplay/template-manifest 1.0.2 → 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 +40 -6
  2. package/package.json +1 -1
package/core.js CHANGED
@@ -32,8 +32,10 @@ const COLOR_NAME_HINT = /colou?r/i;
32
32
  function guessSubtype(key, type) {
33
33
  if (type !== 'string') return undefined;
34
34
  if (VIDEO_NAME_HINT.test(key)) return 'video';
35
- if (IMAGE_NAME_HINT.test(key)) return 'image';
35
+ // check color first: a literal "color"/"colour" substring is a more specific signal than
36
+ // the generic bg_/_bg prefix IMAGE_NAME_HINT also matches (e.g. "bg_color_1", "bg_font_color")
36
37
  if (COLOR_NAME_HINT.test(key)) return 'color';
38
+ if (IMAGE_NAME_HINT.test(key)) return 'image';
37
39
  return undefined;
38
40
  }
39
41
 
@@ -138,15 +140,46 @@ function getHookType(callee, scope) {
138
140
  return undefined;
139
141
  }
140
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
+
141
173
  // True when `node` is an expression representing "the template variables object" itself -
142
- // covers the ES default export, the React `useTemplate()` hook result, and the global UMD
143
- // 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).
144
177
  function isTemplateAccessBase(node, scope, seen = new Set()) {
145
178
  if (!node) return false;
146
179
 
147
180
  if (node.type === 'MemberExpression' && !node.computed && node.property.type === 'Identifier'
148
181
  && node.property.name === 'template') {
149
- return resolvesToGlobalUtilsAlias(node.object, scope);
182
+ return resolvesToGlobalUtilsAlias(node.object, scope) || resolvesToGetDataResult(node.object, scope);
150
183
  }
151
184
 
152
185
  if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
@@ -275,8 +308,9 @@ function scanFile(code, table) {
275
308
  /**
276
309
  * Statically scans a template's source tree for DSPLAY template-variable reads
277
310
  * (`tval`/`useTemplateVal`-style calls, `useTemplate()`/`.template` destructuring or direct
278
- * property access, in both the ES-import and global-UMD-alias worlds) and returns the
279
- * 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.
280
314
  * @param {string} srcDir - Directory to scan recursively for .js/.jsx files.
281
315
  * @returns {Array<{key: string, type: 'string'|'boolean'|'integer'|'float', default?: *, subtypeGuess?: string}>}
282
316
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dsplay/template-manifest",
3
- "version": "1.0.2",
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",