@webority/canvas-kit 0.120.1 → 0.121.0

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/dist/ids.js +1 -1
  2. package/package.json +1 -1
package/dist/ids.js CHANGED
@@ -1 +1 @@
1
- import{basename as e,extname as t,join as n,relative as r,sep as i}from"node:path";import a from"@babel/parser";import{readFile as o,readdir as s,writeFile as c}from"node:fs/promises";var l=new Set([`node_modules`,`dist`,`build`]),u=new Set([`.jsx`,`.tsx`,`.js`]),d=new Set([`@webority/mobile-ui`,`react-native`]),f=new Set([`@webority/ui-react`,`@webority/mobile-ui`]),p=new Set([`section`,`form`,`header`,`nav`,`main`,`aside`,`article`,`fieldset`]),m=[`aria-label`,`name`,`id`,`title`,`label`,`placeholder`],h=new Set([`ArrowFunctionExpression`,`FunctionExpression`,`FunctionDeclaration`,`ObjectMethod`,`ClassMethod`]),g=new Set([`loc`,`start`,`end`,`comments`,`leadingComments`,`trailingComments`,`innerComments`,`tokens`,`extra`,`errors`]),_=40,v=new Set([`Fragment`,`React.Fragment`]);async function y(e,{write:t=!1}={}){let n=[],a=[],s=[];for(let t of await b(e)){let c=r(e,t).split(i).join(`/`),l=await o(t,`utf8`),u=x({source:l,file:t,label:c});n.push(...u.proposals),a.push(...u.noKey),u.insertions.length>0&&s.push({file:t,source:l,insertions:u.insertions})}let l=0;if(t)for(let e of s){let t=e.source;for(let n of[...e.insertions].sort((e,t)=>t.offset-e.offset))t=`${t.slice(0,n.offset)}${n.text}${t.slice(n.offset)}`;await c(e.file,t,`utf8`),l+=e.insertions.length}return{proposals:n,noKey:a,written:l}}async function b(e){let r=await s(e,{withFileTypes:!0});r.sort((e,t)=>e.name<t.name?-1:1);let i=[];for(let a of r){let r=n(e,a.name);a.isDirectory()?l.has(a.name)||i.push(...await b(r)):a.isFile()&&u.has(t(a.name))&&!a.name.includes(`.test.`)&&i.push(r)}return i}function x({source:e,file:t,label:n}){let r;try{r=a.parse(e,{sourceType:`module`,plugins:[`jsx`,`typescript`]})}catch(e){throw Error(`${n}: ${e.message}`)}let i=new Set,o=!1;for(let e of r.program.body){if(e.type!==`ImportDeclaration`)continue;let t=e.source.value;if(o||=d.has(t),f.has(t))for(let t of e.specifiers)t.type===`ImportSpecifier`&&i.add(t.local.name)}let s=o?`testID`:`data-canvas`,c=new Map,l=[];S(r.program,null,c,l);let u=P(t),p=t=>e.slice(t.start,t.end),m=new Map,h=[],g=[];for(let e of l){let t=e.openingElement,r=j(t,s),a=O(t,i),o=C(e,c);if(a!==null&&o===null&&D(m,e,a,r,c),!r)if(o!==null){let r=t.attributes.find(e=>e.type===`JSXAttribute`&&e.name.type===`JSXIdentifier`&&e.name.name===`key`)?.value,i=r?.type===`JSXExpressionContainer`?r.expression:null,a=o.params[1]?.type===`Identifier`?o.params[1].name:null,c=v.has(p(t.name))?w(e,p):e;c===null||i===null||i.type===`JSXEmptyExpression`||R(i)||i.type===`Identifier`&&i.name===a?g.push({start:e.start,entry:{file:n,line:e.loc.start.line,element:p(t.name)}}):(c===e||!j(c.openingElement,s))&&h.push({element:c,kind:`list`,keyExpression:i})}else a===null&&k(t)&&h.push({element:e,kind:`block`})}for(let e of m.values())for(let t of e.values())if(t.length>1&&t.some(e=>e.conditional))for(let e of t)e.skipped||h.push({element:e.element,kind:`sibling`});h.sort((e,t)=>e.element.start-t.element.start);let _=new Set,y=[],b=[];for(let e of h){let{element:t,kind:r}=e,a=t.openingElement,o=p(a.name),c=O(a,i),l=`${u}.${N(t,c===null?o:F(c))}`,d=r===`list`?`${l}.\${${p(e.keyExpression)}}`:l,f=d;if(_.has(f)){let e=2;for(;_.has(`${d}-${e}`);)e+=1;f=`${d}-${e}`}_.add(f);let m=r===`list`?` ${s}={\`${f}\`}`:` ${s}="${f}"`;y.push({file:n,line:t.loc.start.line,element:o,key:f}),b.push({offset:a.name.end,text:m})}return{proposals:y,noKey:g.sort((e,t)=>e.start-t.start).map(e=>e.entry),insertions:b}}function S(e,t,n,r){t!==null&&n.set(e,t),e.type===`JSXElement`&&r.push(e);for(let t of Object.keys(e)){if(g.has(t))continue;let i=e[t];for(let t of Array.isArray(i)?i:[i])typeof t==`object`&&t&&typeof t.type==`string`&&S(t,e,n,r)}}function C(e,t){let n=t.get(e),r=null;if(n?.type===`ArrowFunctionExpression`&&n.body===e)r=n;else if(n?.type===`ReturnStatement`){let e=t.get(n),i=t.get(e);e?.type===`BlockStatement`&&h.has(i?.type)&&i.body===e&&(r=i)}if(r===null)return null;let i=t.get(r);return i?.type===`CallExpression`&&i.arguments[0]===r&&i.callee.type===`MemberExpression`&&!i.callee.computed&&i.callee.property.type===`Identifier`&&i.callee.property.name===`map`?r:null}function w(e,t){for(let n of e.children)if(n.type===`JSXFragment`||n.type===`JSXElement`&&v.has(t(n.openingElement.name))){let e=w(n,t);if(e!==null)return e}else if(n.type===`JSXElement`)return n;return null}function T(e,t){for(let n=t.get(e);n!==void 0;n=t.get(n)){if(n.type===`JSXElement`||n.type===`JSXFragment`)return n;if(h.has(n.type))return null}return null}function E(e,t,n){for(let r=n.get(e);r!==t;r=n.get(r))if(r.type===`ConditionalExpression`||r.type===`LogicalExpression`&&r.operator===`&&`)return!0;return!1}function D(e,t,n,r,i){let a=T(t,i);if(a===null)return;e.has(a)||e.set(a,new Map);let o=e.get(a);o.has(n)||o.set(n,[]),o.get(n).push({element:t,skipped:r,conditional:E(t,a,i)})}function O(e,t){return e.name.type===`JSXIdentifier`&&t.has(e.name.name)?e.name.name:null}function k(e){if(e.name.type!==`JSXIdentifier`)return!1;let t=e.name.name;return p.has(t)||t===`div`&&(A(e,`id`)||A(e,`aria-label`))}function A(e,t){return e.attributes.some(e=>e.type===`JSXAttribute`&&e.name.type===`JSXIdentifier`&&e.name.name===t)}function j(e,t){return e.attributes.some(e=>e.type===`JSXSpreadAttribute`)||A(e,t)}function M(e,t){let n=e.attributes.find(e=>e.type===`JSXAttribute`&&e.name.type===`JSXIdentifier`&&e.name.name===t)?.value;if(n){if(n.type===`StringLiteral`)return n.value;if(n.type===`JSXExpressionContainer`){let e=n.expression;if(e.type===`StringLiteral`)return e.value;if(e.type===`TemplateLiteral`&&e.expressions.length===0)return e.quasis[0].value.cooked??void 0}}}function N(e,t){for(let t of m){let n=M(e.openingElement,t),r=n===void 0?``:L(I(n));if(r!==``)return r}let n=e.children.filter(e=>e.type===`JSXText`&&e.value.trim()!==``);if(n.length===1){let e=L(I(n[0].value));if(e!==``)return e}return L(I(t))}function P(n){return I(e(n,t(n))).replace(/-(screen|page)$/,``)}function F(e){return e.replace(/^App(?=[A-Z])/,``)}function I(e){return e.replace(/([a-z0-9])([A-Z])/g,`$1-$2`).toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-+|-+$/g,``)}function L(e){if(e.length<=_)return e;let t=e.slice(0,41).lastIndexOf(`-`);return t>0?e.slice(0,t):e.slice(0,_)}function R(e){return e.type===`TemplateLiteral`?e.expressions.length===0:/Literal$/.test(e.type)}export{y as proposeCanvasKeys};
1
+ import{basename as e,dirname as t,extname as n,join as r,relative as i,sep as a}from"node:path";import o from"@babel/parser";import{readFile as s,readdir as c,writeFile as l}from"node:fs/promises";var u=new Set([`node_modules`,`dist`,`build`]),d=new Set([`.jsx`,`.tsx`,`.js`]),f=[`react-native`,`@webority/mobile-ui`],p=new Set([`@webority/ui-react`,`@webority/mobile-ui`]),m=new Set([`section`,`form`,`header`,`nav`,`main`,`aside`,`article`,`fieldset`]),h=[`aria-label`,`name`,`id`,`title`,`label`,`placeholder`],g=new Set([`ArrowFunctionExpression`,`FunctionExpression`,`FunctionDeclaration`,`ObjectMethod`,`ClassMethod`]),_=new Set([`loc`,`start`,`end`,`comments`,`leadingComments`,`trailingComments`,`innerComments`,`tokens`,`extra`,`errors`]),v=40,y=new Set([`Fragment`,`React.Fragment`]);async function b(e,{write:t=!1}={}){let n=[],r=[],o=[],c=new Map;for(let t of await C(e)){let l=i(e,t).split(a).join(`/`),u=await s(t,`utf8`),d=w({source:u,file:t,label:l,phone:await x(t,c)});n.push(...d.proposals),r.push(...d.noKey),d.insertions.length>0&&o.push({file:t,source:u,insertions:d.insertions})}let u=0;if(t)for(let e of o){let t=e.source;for(let n of[...e.insertions].sort((e,t)=>t.offset-e.offset))t=`${t.slice(0,n.offset)}${n.text}${t.slice(n.offset)}`;await l(e.file,t,`utf8`),u+=e.insertions.length}return{proposals:n,noKey:r,written:u}}async function x(e,n){let i=[],a=t(e),o;for(;;){if(n.has(a)){o=n.get(a);break}i.push(a);let s=await S(r(a,`package.json`));if(s!==null){o=f.some(e=>s.dependencies?.[e]||s.peerDependencies?.[e]);break}let c=t(a);if(c===a)throw Error(`no package.json above ${e}`);a=c}for(let e of i)n.set(e,o);return o}async function S(e){try{return JSON.parse(await s(e,`utf8`))}catch(e){if(e.code===`ENOENT`)return null;throw e}}async function C(e){let t=await c(e,{withFileTypes:!0});t.sort((e,t)=>e.name<t.name?-1:1);let i=[];for(let a of t){let t=r(e,a.name);a.isDirectory()?u.has(a.name)||i.push(...await C(t)):a.isFile()&&d.has(n(a.name))&&!a.name.includes(`.test.`)&&i.push(t)}return i}function w({source:e,file:t,label:n,phone:r}){let i;try{i=o.parse(e,{sourceType:`module`,plugins:[`jsx`,`typescript`]})}catch(e){throw Error(`${n}: ${e.message}`)}let a=new Set;for(let e of i.program.body){if(e.type!==`ImportDeclaration`)continue;let t=e.source.value;if(p.has(t))for(let t of e.specifiers)t.type===`ImportSpecifier`&&a.add(t.local.name)}let s=r?`testID`:`data-canvas`,c=new Map,l=[];T(i.program,null,c,l);let u=L(t),d=t=>e.slice(t.start,t.end),f=new Map,m=[],h=[];for(let e of l){let t=e.openingElement,r=P(t,s),i=j(t,a),o=E(e,c);if(i!==null&&o===null&&A(f,e,i,r,c),!r)if(o!==null){let r=t.attributes.find(e=>e.type===`JSXAttribute`&&e.name.type===`JSXIdentifier`&&e.name.name===`key`)?.value,i=r?.type===`JSXExpressionContainer`?r.expression:null,a=o.params[1]?.type===`Identifier`?o.params[1].name:null,c=y.has(d(t.name))?D(e,d):e;c===null||i===null||i.type===`JSXEmptyExpression`||V(i)||i.type===`Identifier`&&i.name===a?h.push({start:e.start,entry:{file:n,line:e.loc.start.line,element:d(t.name)}}):(c===e||!P(c.openingElement,s))&&m.push({element:c,kind:`list`,keyExpression:i})}else i===null&&M(t)&&m.push({element:e,kind:`block`})}for(let e of f.values())for(let t of e.values())if(t.length>1&&t.some(e=>e.conditional))for(let e of t)e.skipped||m.push({element:e.element,kind:`sibling`});m.sort((e,t)=>e.element.start-t.element.start);let g=new Set,_=[],v=[];for(let e of m){let{element:t,kind:r}=e,i=t.openingElement,o=d(i.name),c=j(i,a),l=`${u}.${I(t,c===null?o:R(c))}`,f=r===`list`?`${l}.\${${d(e.keyExpression)}}`:l,p=f;if(g.has(p)){let e=2;for(;g.has(`${f}-${e}`);)e+=1;p=`${f}-${e}`}g.add(p);let m=r===`list`?` ${s}={\`${p}\`}`:` ${s}="${p}"`;_.push({file:n,line:t.loc.start.line,element:o,key:p}),v.push({offset:i.name.end,text:m})}return{proposals:_,noKey:h.sort((e,t)=>e.start-t.start).map(e=>e.entry),insertions:v}}function T(e,t,n,r){t!==null&&n.set(e,t),e.type===`JSXElement`&&r.push(e);for(let t of Object.keys(e)){if(_.has(t))continue;let i=e[t];for(let t of Array.isArray(i)?i:[i])typeof t==`object`&&t&&typeof t.type==`string`&&T(t,e,n,r)}}function E(e,t){let n=t.get(e),r=null;if(n?.type===`ArrowFunctionExpression`&&n.body===e)r=n;else if(n?.type===`ReturnStatement`){let e=t.get(n),i=t.get(e);e?.type===`BlockStatement`&&g.has(i?.type)&&i.body===e&&(r=i)}if(r===null)return null;let i=t.get(r);return i?.type===`CallExpression`&&i.arguments[0]===r&&i.callee.type===`MemberExpression`&&!i.callee.computed&&i.callee.property.type===`Identifier`&&i.callee.property.name===`map`?r:null}function D(e,t){for(let n of e.children)if(n.type===`JSXFragment`||n.type===`JSXElement`&&y.has(t(n.openingElement.name))){let e=D(n,t);if(e!==null)return e}else if(n.type===`JSXElement`)return n;return null}function O(e,t){for(let n=t.get(e);n!==void 0;n=t.get(n)){if(n.type===`JSXElement`||n.type===`JSXFragment`)return n;if(g.has(n.type))return null}return null}function k(e,t,n){for(let r=n.get(e);r!==t;r=n.get(r))if(r.type===`ConditionalExpression`||r.type===`LogicalExpression`&&r.operator===`&&`)return!0;return!1}function A(e,t,n,r,i){let a=O(t,i);if(a===null)return;e.has(a)||e.set(a,new Map);let o=e.get(a);o.has(n)||o.set(n,[]),o.get(n).push({element:t,skipped:r,conditional:k(t,a,i)})}function j(e,t){return e.name.type===`JSXIdentifier`&&t.has(e.name.name)?e.name.name:null}function M(e){if(e.name.type!==`JSXIdentifier`)return!1;let t=e.name.name;return m.has(t)||t===`div`&&(N(e,`id`)||N(e,`aria-label`))}function N(e,t){return e.attributes.some(e=>e.type===`JSXAttribute`&&e.name.type===`JSXIdentifier`&&e.name.name===t)}function P(e,t){return e.attributes.some(e=>e.type===`JSXSpreadAttribute`)||N(e,t)}function F(e,t){let n=e.attributes.find(e=>e.type===`JSXAttribute`&&e.name.type===`JSXIdentifier`&&e.name.name===t)?.value;if(n){if(n.type===`StringLiteral`)return n.value;if(n.type===`JSXExpressionContainer`){let e=n.expression;if(e.type===`StringLiteral`)return e.value;if(e.type===`TemplateLiteral`&&e.expressions.length===0)return e.quasis[0].value.cooked??void 0}}}function I(e,t){for(let t of h){let n=F(e.openingElement,t),r=n===void 0?``:B(z(n));if(r!==``)return r}let n=e.children.filter(e=>e.type===`JSXText`&&e.value.trim()!==``);if(n.length===1){let e=B(z(n[0].value));if(e!==``)return e}return B(z(t))}function L(t){return z(e(t,n(t))).replace(/-(screen|page)$/,``)}function R(e){return e.replace(/^App(?=[A-Z])/,``)}function z(e){return e.replace(/([a-z0-9])([A-Z])/g,`$1-$2`).toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-+|-+$/g,``)}function B(e){if(e.length<=v)return e;let t=e.slice(0,41).lastIndexOf(`-`);return t>0?e.slice(0,t):e.slice(0,v)}function V(e){return e.type===`TemplateLiteral`?e.expressions.length===0:/Literal$/.test(e.type)}export{b as proposeCanvasKeys};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webority/canvas-kit",
3
- "version": "0.120.1",
3
+ "version": "0.121.0",
4
4
  "description": "Builds product kits for Webority Canvas: a kit bundle, its manifest, and the spec shape the renderers answer.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "private": false,