@lexical/compiler 0.51.0 → 0.51.1-nightly.20260921.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.
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import{parse as e}from"@babel/parser";import t from"magic-string";import*as n from"node:fs";import*as r from"node:path";function o(e,t,n=["explicitResourceManagement"]){const r="string"==typeof e?e:"",o=/\.[cm]?tsx?$/i.test(r),s=[...n];return o&&s.push("typescript"),o&&!/\.[cm]?tsx$/i.test(r)||s.push("jsx"),t?s.concat(t.filter(e=>{const t=Array.isArray(e)?e[0]:e;return!o||"flow"!==t&&"flowComments"!==t})):s}const s=["aliasedValue","arrayValue","booleanValue","configExtension","createCommand","createContextState","createImportState","createLinkMatcherWithRegExp","createRefCountedRegistry","createRenderState","createState","declarePeerDependency","defineExtension","defineImportRule","defineOverlayRules","domOverride","enumValue","newContext","nodeSchema","nullable","numberValue","objectValue","optional","rawValue","safeCast","stringValue","transformValue","unionValue","warnOnlyOnce","withAccessors","withField"],i=new Map([["configExtension",{form:"args"}],["declarePeerDependency",{form:"args"}],["defineExtension",{form:"identity"}],["defineImportRule",{form:"identity"}],["safeCast",{form:"identity"}]]),a=/^[^\S\n]*\*?[^\S\n]*@lexical-inline(?:[^\S\n]+(\S+))?[^\S\n]*$/m;class c extends Error{constructor(e){super(e),this.name="PureAnnotationsError"}}const l=/^[^\S\n]*\*?[^\S\n]*@lexical-pure-namespace[^\S\n]*$/m,f=new Set(["args","identity"]),u=["sel"],p=/* @__PURE__ */d();function d(){return new Map(Array.from(i,([e,t])=>[e,t.form]))}const m={contentOnly:!0},y=/[#@]__PURE__/,h=/\.(?:[cm]?[jt]sx?)$/,g=[/^lexical$/,/^@lexical\//],S=/[#@]__NO_SIDE_EFFECTS__/,w=/\bfrom\s*['"]\./,x=new Set(["ArrayExpression","BigIntLiteral","BooleanLiteral","DecimalLiteral","Identifier","JSXElement","JSXFragment","NullLiteral","NumericLiteral","ObjectExpression","RegExpLiteral","StringLiteral","TemplateLiteral","ThisExpression"]),v=[".ts",".tsx",".mts",".cts",".js",".jsx",".mjs",".cjs"],E=new Set(["Array","BigInt","Boolean","Map","Number","Object","RegExp","Set","String","Symbol","WeakMap","WeakSet"]),b=/[\\/]node_modules[\\/]/,I=new Set(["ArrowFunctionExpression","ClassAccessorProperty","ClassMethod","ClassPrivateMethod","ClassPrivateProperty","ClassProperty","FunctionDeclaration","FunctionExpression","ObjectMethod","PropertyDefinition","StaticBlock","TSDeclareMethod"]),C=new Set,_=new Set(["comments","extra","innerComments","leadingComments","loc","trailingComments"]);function j(e,t){let n=t;for(;n>0&&/\s/.test(e[n-1]);)n--;if(n<4||"/"!==e[n-1]||"*"!==e[n-2])return null;const r=e.lastIndexOf("/*",n-2);return-1===r?null:{end:n,start:r,value:e.slice(r+2,n-2)}}function M(e,t){const n=j(e,t);return null!==n&&y.test(n.value)}function D(e,t,n,r){const{functions:o,namespaces:s}=t,i=r||{discarded:!1,shadowed:C,statementStarts:new Set};if(Array.isArray(e)){for(const r of e)D(r,t,n,{discarded:!1,shadowed:i.shadowed,statementStarts:i.statementStarts});return}if(!e||"object"!=typeof e||"string"!=typeof e.type)return;if(I.has(e.type))return;if("ExpressionStatement"===e.type&&i.statementStarts.add(e.expression.start),"CallExpression"===e.type&&e.callee){const t={discarded:i.discarded,statementStart:i.statementStarts.has(e.start)};if("Identifier"===e.callee.type&&o.has(e.callee.name)&&!i.shadowed.has(e.callee.name))n(e,t);else if(void 0!==s&&s.size>0){const r=function(e){for(let t=e;t;){if("Identifier"===t.type)return t.name;if("MemberExpression"===t.type)t=t.object;else{if("CallExpression"!==t.type)return null;t=t.callee}}return null}(e.callee);null!==r&&s.has(r)&&!i.shadowed.has(r)&&n(e,{...t,namespace:!0})}}const a=function(e){const t="BlockStatement"===e.type?e.body:"SwitchCase"===e.type?e.consequent:null;if(null===t)return null;let n=null;const r=e=>{n=n||new Set,n.add(e)};for(const e of t)if("VariableDeclaration"===e.type&&"var"!==e.kind)for(const t of e.declarations)"Identifier"===t.id.type&&r(t.id.name);else"FunctionDeclaration"!==e.type&&"ClassDeclaration"!==e.type||!e.id||r(e.id.name);return n}(e),c=null===a?i.shadowed:new Set([...i.shadowed,...a]);for(const r of Object.keys(e))if(!_.has(r)){const o=e[r];o&&"object"==typeof o&&D(o,t,n,{discarded:"ExpressionStatement"===e.type&&"expression"===r,shadowed:c,statementStarts:i.statementStarts})}}function $(e,t,n,r){if(Array.isArray(e))for(const o of e)$(o,t,n,r);else if(e&&"object"==typeof e&&"string"==typeof e.type){t(e,n,r);for(const n of Object.keys(e))if(!_.has(n)){const r=e[n];r&&"object"==typeof r&&$(r,t,e,n)}}}function O(e,t,n,r){const o=new Set(r.map(e=>e.callee)),s=new Set(r.map(e=>e.callee.name));if(0!==s.size){$(n,(e,t,n)=>{s.has(e.name)&&!o.has(e)&&function(e,t,n){if("Identifier"!==e.type&&"JSXIdentifier"!==e.type)return!1;if(!t)return!0;switch(t.type){case"ImportDefaultSpecifier":case"ImportNamespaceSpecifier":case"ImportSpecifier":return!1;case"ExportSpecifier":return"local"===n;case"OptionalMemberExpression":case"MemberExpression":return"property"!==n||!0===t.computed;case"ClassMethod":case"ClassProperty":case"ObjectMethod":case"ObjectProperty":return"key"!==n||!0===t.computed;default:return!0}}(e,t,n)&&s.delete(e.name)});for(const r of n.body){if("ImportDeclaration"!==r.type||"type"===r.importKind)continue;const{specifiers:n}=r,o=n.filter(e=>"ImportSpecifier"===e.type),i=o.filter(e=>"type"!==e.importKind&&s.has(e.local.name));if(0===i.length)continue;if(i.length<o.length){const t=new Set(i);for(let n=0;n<o.length;n++){if(!t.has(o[n]))continue;let r=n;for(;r+1<o.length&&t.has(o[r+1]);)r++;r+1<o.length?e.remove(o[n].start,o[r+1].start):e.remove(o[n-1].end,o[r].end),n=r}continue}const a=t.indexOf("}",o[o.length-1].end);if(o.length<n.length){const r=n[n.indexOf(o[0])-1];e.remove(t.indexOf(",",r.end),a+1);continue}e.remove(r.start,r.end)}}}function A(e,t,n,r,o){const s=j(t,n.start);null!==s&&y.test(s.value)&&e.remove(s.start,n.start);const i=n.arguments;if("identity"!==r.form)return e.overwrite(n.start,function(e,t){const n=t.typeArguments||t.typeParameters,r=Math.max(t.callee.end,n?n.end:t.callee.end);return e.indexOf("(",r)+1}(t,n),"([",m),void e.overwrite(n.end-1,n.end,"])",m);!0===o||!x.has(i[0].type)?(e.overwrite(n.start,i[0].start,"(",m),e.overwrite(i[0].end,n.end,")",m)):(e.remove(n.start,i[0].start),e.remove(i[0].end,n.end))}const P=new Map,F=new Map;function k(e,t){if(e)for(const n of e){const e=a.exec(n.value);if(e){const n=e[1]||"";if(!f.has(n)){throw new c(`@lexical/compiler: unknown @lexical-inline form ${n?`"${n}"`:"(none given)"}${t?` in ${t}`:""}. Write \`@lexical-inline <form>\` with one of: ${Array.from(f).sort().join(", ")}.`)}return n}}return null}function R(e){return null!=e&&e.some(e=>S.test(e.value))}function V(e,t){const n=new Map;for(const r of e.body){const e="ExportNamedDeclaration"===r.type&&r.declaration?r.declaration:r,o=(r.leadingComments||[]).concat(e===r?[]:e.leadingComments||[]),s=R(o),i=k(o,t);if("FunctionDeclaration"!==e.type&&"TSDeclareFunction"!==e.type||!e.id){if("VariableDeclaration"===e.type)for(const r of e.declarations){const e=(r.leadingComments||[]).concat(r.init&&r.init.leadingComments||[]);"Identifier"===r.id.type&&(s||R(e))&&n.set(r.id.name,i||k(e,t))}}else s&&n.set(e.id.name,i)}return n}function T(e,t){const o=r.resolve(r.dirname(e),t),s=P.get(o);if(void 0!==s)return s;const i=[],a=r.extname(o);if(""!==a&&(i.push(o),/^\.[cm]?js$/.test(a))){const e=o.slice(0,-a.length);i.push(e+a.replace("js","ts"),e+".tsx")}for(const e of v)i.push(o+e);for(const e of v)i.push(r.join(o,`index${e}`));let c=null;for(const e of i)try{if(n.statSync(e).isFile()){c=e;break}}catch(e){}return P.set(o,c),c}function L(e,t){return N(e,t).functions}function N(t,r){const s=F.get(t);if(void 0!==s)return s;let i=null;try{i=e(n.readFileSync(t,"utf8"),{plugins:o(t,r),sourceType:"module"}).program}catch(e){}const a=null===i?{functions:new Map,namespaces:new Set}:{functions:V(i,t),namespaces:W(i)};return F.set(t,a),a}function B(e,t,n){const r=K(n.sources,g),o=new Map;function s(e,t,n,r){const s=i.get(t),a=void 0!==s?r||n===s.form?s:void 0:n?{form:n}:void 0;o.set(e,{inline:a,name:t})}for(const[t,r]of V(e,n.filename))s(t,t,r,!1);for(const o of e.body){if("ImportDeclaration"!==o.type)continue;const e=o.source.value,i=U(r,e),a=!i&&!1!==n.relativeImports&&"string"==typeof n.filename&&e.startsWith(".");if(i||a)for(const r of o.specifiers){if("ImportDefaultSpecifier"===r.type){const n=e.slice(e.lastIndexOf("/")+1);i&&t.has(n)&&s(r.local.name,n,null,!0);continue}if("ImportSpecifier"!==r.type||"Identifier"!==r.imported.type)continue;if(i){t.has(r.imported.name)&&s(r.local.name,r.imported.name,null,!0);continue}const o=T(n.filename,e),a=null===o?void 0:L(o,n.parserPlugins).get(r.imported.name);void 0!==a&&s(r.local.name,r.imported.name,a,!1)}}return o}function W(e){const t=new Set,n=e=>(e||[]).some(e=>l.test(e.value));for(const r of e.body){const e="ExportNamedDeclaration"===r.type&&r.declaration?r.declaration:r,o=n(r.leadingComments);if("VariableDeclaration"===e.type)for(const r of e.declarations)"Identifier"===r.id.type&&(o||n(e.leadingComments)||n(r.leadingComments)||r.init&&n(r.init.leadingComments))&&t.add(r.id.name)}return t}function z(e,t){return N(e,t).namespaces}function J(e,t,n){const r=new Set,o=K(n.sources,g);for(const t of W(e))r.add(t);for(const s of e.body){if("ImportDeclaration"!==s.type)continue;const e=s.source.value,i=U(o,e),a=!i&&!1!==n.relativeImports&&"string"==typeof n.filename&&e.startsWith(".");if(!i&&!a)continue;let c;for(const o of s.specifiers)if("ImportSpecifier"===o.type&&"Identifier"===o.imported.type)if(i)t.has(o.imported.name)&&r.add(o.local.name);else{if(void 0===c){const t=T(n.filename,e);c=null===t?new Set:z(t,n.parserPlugins)}c.has(o.imported.name)&&r.add(o.local.name)}}for(const t of e.body)if("VariableDeclaration"===t.type)for(const e of t.declarations)"Identifier"===e.id.type&&e.init&&"Identifier"===e.init.type&&r.has(e.init.name)&&r.add(e.id.name);return r}function X(e,t){const n=e=>{if(Array.isArray(e))e.forEach(n);else if(e&&"object"==typeof e&&"string"==typeof e.type&&!I.has(e.type)){"CallExpression"!==e.type&&"NewExpression"!==e.type&&"TaggedTemplateExpression"!==e.type||t(e);for(const t of Object.keys(e))_.has(t)||n(e[t])}};e.arguments.forEach(n)}function q(n,r){const i=r||{},a=new Set(i.functions||s),l=new Set(i.namespaces||u);if(!function(e,t,n){if(e.includes("__NO_SIDE_EFFECTS__")||e.includes("@lexical-pure-namespace")||w.test(e))return!0;for(const n of t)if(e.includes(n))return!0;for(const t of n)if(e.includes(t))return!0;return!1}(n,a,l))return null;const f=e(n,{errorRecovery:!1,plugins:o(i.filename,i.parserPlugins),sourceFilename:i.filename,sourceType:"module"}),p=B(f.program,a,i),d=J(f.program,l,i);if(0===p.size&&0===d.size)return null;const m=new Set,y=[],h=[];D(f.program,{functions:p,namespaces:d},(e,t)=>{if(!0===t.namespace)return void(M(n,e.start)||m.add(e.start));h.push(e);const r=p.get(e.callee.name),o=!0===i.inline?r.inline:void 0;void 0!==o&&!t.discarded&&function(e,t){const n=e.arguments;return"identity"!==t.form||1===n.length&&"SpreadElement"!==n[0].type&&"SequenceExpression"!==n[0].type}(e,o)?y.push([e,o,t.statementStart]):M(n,e.start)||m.add(e.start)});const g=Array.from(m);if(!0===i.strict){const e=new Set(m);for(const[t]of y)e.add(t.start);!function(e,t,n,r){const o=[],s=new Set;for(const t of n)X(t,n=>{const i=n.callee||n.tag;if(r.has(n.start)||M(e,n.start)||s.has(n.start)||"Identifier"===i.type&&E.has(i.name))return;s.add(n.start);const a=e.slice(i.start,i.end).replace(/\s+/g," "),c=n.loc?`${n.loc.start.line}:${n.loc.start.column+1}`:"?";o.push(` ${c} ${a}(...) inside ${e.slice(t.callee.start,t.callee.end)}(...)`)});if(o.length>0)throw new c(`@lexical/compiler: ${o.length} call(s) evaluated inside a definition in ${t||"<unknown>"} are not known to be side-effect free, so the definition cannot be tree-shaken:\n${o.join("\n")}\nMake the call lazy, or declare the function side-effect free (@__NO_SIDE_EFFECTS__ plus the transform's \`functions\`/\`namespaces\` options) so that its calls are annotated too.`)}(n,i.filename,h,e)}if(0===g.length&&0===y.length)return null;const S=new t(n);for(const e of g)S.appendLeft(e,"/* @__PURE__ */");for(const[e,t,r]of y)A(S,n,e,t,r);return O(S,n,f.program,y.map(([e])=>e)),{code:S.toString(),count:g.length,inlined:y.length,map:!1===i.sourceMap?void 0:S.generateMap({hires:"boundary",source:i.filename})}}function K(e,t){return void 0===e?t:Array.isArray(e)?e:[e]}function U(e,t){return e.some(e=>e.test(t))}const G=s,H=u,Q=p;function Y(e,t){return q(e,t)}function Z(t,n){return function(t,n){const r=n||{},i=new Set(r.functions||s),a=e(t,{errorRecovery:!1,plugins:o(r.filename,r.parserPlugins),sourceFilename:r.filename,sourceType:"module"});return{functions:new Set(B(a.program,i,r).keys()),namespaces:J(a.program,new Set(r.namespaces||u),r)}}(t,n)}function ee(e){return function(e){const t=e||{},n=new Set(t.functions||s),r=K(t.include,[h]),o=K(t.exclude,[]);return{enforce:void 0===t.enforce?"post":t.enforce,name:"@lexical/compiler",transform(e,s){const i=function(e){return e.replace(/[?#][\s\S]*$/,"")}(s);if(!U(r,i)||U(o,i))return null;const a=b.test(i);let l=null;try{l=q(e,{filename:i,functions:n,inline:t.inline,namespaces:t.namespaces,parserPlugins:t.parserPlugins,relativeImports:t.relativeImports,sourceMap:t.sourceMap,sources:t.sources,strict:!0===t.strict&&!a})}catch(e){if(e instanceof c&&!a)throw e;return this&&"function"==typeof this.warn&&this.warn(`@lexical/compiler could not parse ${s}, no annotations were added: ${e instanceof Error?e.message:String(e)}`),null}return null===l?null:{code:l.code,map:void 0===l.map?null:l.map}}}}(e)}export{Q as INLINE_FACTORY_FORMS,G as PURE_FACTORY_FUNCTIONS,H as PURE_NAMESPACES,ee as pureAnnotations,Z as pureCallNames,Y as transformPureAnnotations};
|
|
9
|
+
import{parse as e}from"@babel/parser";import t from"magic-string";import*as n from"node:fs";import*as r from"node:path";function o(e,t,n=["explicitResourceManagement"]){const r="string"==typeof e?e:"",o=/\.[cm]?tsx?$/i.test(r),s=[...n];return o&&s.push("typescript"),o&&!/\.[cm]?tsx$/i.test(r)||s.push("jsx"),t?s.concat(t.filter(e=>{const t=Array.isArray(e)?e[0]:e;return!o||"flow"!==t&&"flowComments"!==t})):s}const s=["aliasedValue","arrayValue","booleanValue","configExtension","createCommand","createContextState","createImportState","createLinkMatcherWithRegExp","createRefCountedRegistry","createRenderState","createState","declarePeerDependency","defineExtension","defineImportRule","defineOverlayRules","domOverride","enumValue","newContext","nodeSchema","nullable","numberValue","objectValue","optional","rawValue","safeCast","stringValue","transformValue","unionValue","warnOnlyOnce","withAccessors","withField"],i=new Map([["configExtension",{form:"args"}],["declarePeerDependency",{form:"args"}],["defineExtension",{form:"identity"}],["defineImportRule",{form:"identity"}],["safeCast",{form:"identity"}]]),a=/^[^\S\n]*\*?[^\S\n]*@lexical-inline(?:[^\S\n]+(\S+))?[^\S\n]*$/m;class c extends Error{constructor(e){super(e),this.name="PureAnnotationsError"}}const l=/^[^\S\n]*\*?[^\S\n]*@lexical-pure-namespace[^\S\n]*$/m,f=new Set(["args","identity"]),u=["sel"],p=/* @__PURE__ */d();function d(){return new Map(Array.from(i,([e,t])=>[e,t.form]))}const m={contentOnly:!0},y=/[#@]__PURE__/,h=/\.(?:[cm]?[jt]sx?)$/,g=[/^lexical$/,/^@lexical\//],S=/[#@]__NO_SIDE_EFFECTS__/,w=/\bfrom\s*['"]\./,x=new Set(["ArrayExpression","BigIntLiteral","BooleanLiteral","DecimalLiteral","Identifier","JSXElement","JSXFragment","NullLiteral","NumericLiteral","ObjectExpression","RegExpLiteral","StringLiteral","TemplateLiteral","ThisExpression"]),v=[".ts",".tsx",".mts",".cts",".js",".jsx",".mjs",".cjs"],E=new Set(["Array","BigInt","Boolean","Map","Number","Object","RegExp","Set","String","Symbol","WeakMap","WeakSet"]),b=/[\\/]node_modules[\\/]/,I=new Set(["ArrowFunctionExpression","ClassAccessorProperty","ClassMethod","ClassPrivateMethod","ClassPrivateProperty","ClassProperty","FunctionDeclaration","FunctionExpression","ObjectMethod","PropertyDefinition","StaticBlock","TSDeclareMethod"]),C=new Set,_=new Set(["comments","extra","innerComments","leadingComments","loc","trailingComments"]);function j(e,t){let n=t;for(;n>0&&/\s/.test(e[n-1]);)n--;if(n<4||"/"!==e[n-1]||"*"!==e[n-2])return null;const r=e.lastIndexOf("/*",n-2);return-1===r?null:{end:n,start:r,value:e.slice(r+2,n-2)}}function M(e,t){const n=j(e,t);return null!==n&&y.test(n.value)}function D(e,t,n,r){const{functions:o,namespaces:s}=t,i=r||{discarded:!1,shadowed:C,statementStarts:new Set};if(Array.isArray(e)){for(const r of e)D(r,t,n,{discarded:!1,shadowed:i.shadowed,statementStarts:i.statementStarts});return}if(!e||"object"!=typeof e||"string"!=typeof e.type)return;if(I.has(e.type))return;if("ExpressionStatement"===e.type&&i.statementStarts.add(e.expression.start),"CallExpression"===e.type&&e.callee){const t={discarded:i.discarded,statementStart:i.statementStarts.has(e.start)};if("Identifier"===e.callee.type&&o.has(e.callee.name)&&!i.shadowed.has(e.callee.name))n(e,t);else if(void 0!==s&&s.size>0){const r=function(e){for(let t=e;t;){if("Identifier"===t.type)return t.name;if("MemberExpression"===t.type)t=t.object;else{if("CallExpression"!==t.type)return null;t=t.callee}}return null}(e.callee);null!==r&&s.has(r)&&!i.shadowed.has(r)&&n(e,{...t,namespace:!0})}}const a=function(e){const t="BlockStatement"===e.type?e.body:"SwitchCase"===e.type?e.consequent:null;if(null===t)return null;let n=null;const r=e=>{n=n||new Set,n.add(e)};for(const e of t)if("VariableDeclaration"===e.type&&"var"!==e.kind)for(const t of e.declarations)"Identifier"===t.id.type&&r(t.id.name);else"FunctionDeclaration"!==e.type&&"ClassDeclaration"!==e.type||!e.id||r(e.id.name);return n}(e),c=null===a?i.shadowed:new Set([...i.shadowed,...a]);for(const r of Object.keys(e))if(!_.has(r)){const o=e[r];o&&"object"==typeof o&&D(o,t,n,{discarded:"ExpressionStatement"===e.type&&"expression"===r,shadowed:c,statementStarts:i.statementStarts})}}function $(e,t,n,r){if(Array.isArray(e))for(const o of e)$(o,t,n,r);else if(e&&"object"==typeof e&&"string"==typeof e.type){t(e,n,r);for(const n of Object.keys(e))if(!_.has(n)){const r=e[n];r&&"object"==typeof r&&$(r,t,e,n)}}}function O(e,t,n,r){const o=new Set(r.map(e=>e.callee)),s=new Set(r.map(e=>e.callee.name));if(0!==s.size){$(n,(e,t,n)=>{s.has(e.name)&&!o.has(e)&&function(e,t,n){if("Identifier"!==e.type&&"JSXIdentifier"!==e.type)return!1;if(!t)return!0;switch(t.type){case"ImportDefaultSpecifier":case"ImportNamespaceSpecifier":case"ImportSpecifier":return!1;case"ExportSpecifier":return"local"===n;case"OptionalMemberExpression":case"MemberExpression":return"property"!==n||!0===t.computed;case"ClassMethod":case"ClassProperty":case"ObjectMethod":case"ObjectProperty":return"key"!==n||!0===t.computed;default:return!0}}(e,t,n)&&s.delete(e.name)});for(const r of n.body){if("ImportDeclaration"!==r.type||"type"===r.importKind)continue;const{specifiers:n}=r,o=n.filter(e=>"ImportSpecifier"===e.type),i=o.filter(e=>"type"!==e.importKind&&s.has(e.local.name));if(0===i.length)continue;if(i.length<o.length){const t=new Set(i);for(let n=0;n<o.length;n++){if(!t.has(o[n]))continue;let r=n;for(;r+1<o.length&&t.has(o[r+1]);)r++;r+1<o.length?e.remove(o[n].start,o[r+1].start):e.remove(o[n-1].end,o[r].end),n=r}continue}const a=t.indexOf("}",o[o.length-1].end);if(o.length<n.length){const r=n[n.indexOf(o[0])-1];e.remove(t.indexOf(",",r.end),a+1);continue}e.remove(r.start,r.end)}}}function A(e,t,n,r,o){const s=j(t,n.start);null!==s&&y.test(s.value)&&e.remove(s.start,n.start);const i=n.arguments;if("identity"!==r.form)return e.overwrite(n.start,function(e,t){const n=t.typeArguments||t.typeParameters,r=Math.max(t.callee.end,n?n.end:t.callee.end);return e.indexOf("(",r)+1}(t,n),"([",m),void e.overwrite(n.end-1,n.end,"])",m);!0!==o&&x.has(i[0].type)?(e.remove(n.start,i[0].start),e.remove(i[0].end,n.end)):(e.overwrite(n.start,i[0].start,"(",m),e.overwrite(i[0].end,n.end,")",m))}const P=new Map,F=new Map;function k(e,t){if(e)for(const n of e){const e=a.exec(n.value);if(e){const n=e[1]||"";if(!f.has(n))throw new c(`@lexical/compiler: unknown @lexical-inline form ${n?`"${n}"`:"(none given)"}${t?` in ${t}`:""}. Write \`@lexical-inline <form>\` with one of: ${Array.from(f).sort().join(", ")}.`);return n}}return null}function R(e){return null!=e&&e.some(e=>S.test(e.value))}function V(e,t){const n=new Map;for(const r of e.body){const e="ExportNamedDeclaration"===r.type&&r.declaration?r.declaration:r,o=(r.leadingComments||[]).concat(e===r?[]:e.leadingComments||[]),s=R(o),i=k(o,t);if("FunctionDeclaration"!==e.type&&"TSDeclareFunction"!==e.type||!e.id){if("VariableDeclaration"===e.type)for(const r of e.declarations){const e=(r.leadingComments||[]).concat(r.init&&r.init.leadingComments||[]);"Identifier"===r.id.type&&(s||R(e))&&n.set(r.id.name,i||k(e,t))}}else s&&n.set(e.id.name,i)}return n}function T(e,t){const o=r.resolve(r.dirname(e),t),s=P.get(o);if(void 0!==s)return s;const i=[],a=r.extname(o);if(""!==a&&(i.push(o),/^\.[cm]?js$/.test(a))){const e=o.slice(0,-a.length);i.push(e+a.replace("js","ts"),e+".tsx")}for(const e of v)i.push(o+e);for(const e of v)i.push(r.join(o,`index${e}`));let c=null;for(const e of i)try{if(n.statSync(e).isFile()){c=e;break}}catch(e){}return P.set(o,c),c}function L(e,t){return N(e,t).functions}function N(t,r){const s=F.get(t);if(void 0!==s)return s;let i=null;try{i=e(n.readFileSync(t,"utf8"),{plugins:o(t,r),sourceType:"module"}).program}catch(e){}const a=null===i?{functions:new Map,namespaces:new Set}:{functions:V(i,t),namespaces:W(i)};return F.set(t,a),a}function B(e,t,n){const r=K(n.sources,g),o=new Map;function s(e,t,n,r){const s=i.get(t),a=void 0!==s?r||n===s.form?s:void 0:n?{form:n}:void 0;o.set(e,{inline:a,name:t})}for(const[t,r]of V(e,n.filename))s(t,t,r,!1);for(const o of e.body){if("ImportDeclaration"!==o.type)continue;const e=o.source.value,i=U(r,e),a=!i&&!1!==n.relativeImports&&"string"==typeof n.filename&&e.startsWith(".");if(i||a)for(const r of o.specifiers){if("ImportDefaultSpecifier"===r.type){const n=e.slice(e.lastIndexOf("/")+1);i&&t.has(n)&&s(r.local.name,n,null,!0);continue}if("ImportSpecifier"!==r.type||"Identifier"!==r.imported.type)continue;if(i){t.has(r.imported.name)&&s(r.local.name,r.imported.name,null,!0);continue}const o=T(n.filename,e),a=null===o?void 0:L(o,n.parserPlugins).get(r.imported.name);void 0!==a&&s(r.local.name,r.imported.name,a,!1)}}return o}function W(e){const t=new Set,n=e=>(e||[]).some(e=>l.test(e.value));for(const r of e.body){const e="ExportNamedDeclaration"===r.type&&r.declaration?r.declaration:r,o=n(r.leadingComments);if("VariableDeclaration"===e.type)for(const r of e.declarations)"Identifier"===r.id.type&&(o||n(e.leadingComments)||n(r.leadingComments)||r.init&&n(r.init.leadingComments))&&t.add(r.id.name)}return t}function z(e,t){return N(e,t).namespaces}function J(e,t,n){const r=new Set,o=K(n.sources,g);for(const t of W(e))r.add(t);for(const s of e.body){if("ImportDeclaration"!==s.type)continue;const e=s.source.value,i=U(o,e),a=!i&&!1!==n.relativeImports&&"string"==typeof n.filename&&e.startsWith(".");if(!i&&!a)continue;let c;for(const o of s.specifiers)if("ImportSpecifier"===o.type&&"Identifier"===o.imported.type)if(i)t.has(o.imported.name)&&r.add(o.local.name);else{if(void 0===c){const t=T(n.filename,e);c=null===t?new Set:z(t,n.parserPlugins)}c.has(o.imported.name)&&r.add(o.local.name)}}for(const t of e.body)if("VariableDeclaration"===t.type)for(const e of t.declarations)"Identifier"===e.id.type&&e.init&&"Identifier"===e.init.type&&r.has(e.init.name)&&r.add(e.id.name);return r}function X(e,t){const n=e=>{if(Array.isArray(e))e.forEach(n);else if(e&&"object"==typeof e&&"string"==typeof e.type&&!I.has(e.type)){"CallExpression"!==e.type&&"NewExpression"!==e.type&&"TaggedTemplateExpression"!==e.type||t(e);for(const t of Object.keys(e))_.has(t)||n(e[t])}};e.arguments.forEach(n)}function q(n,r){const i=r||{},a=new Set(i.functions||s),l=new Set(i.namespaces||u);if(!function(e,t,n){if(e.includes("__NO_SIDE_EFFECTS__")||e.includes("@lexical-pure-namespace")||w.test(e))return!0;for(const n of t)if(e.includes(n))return!0;for(const t of n)if(e.includes(t))return!0;return!1}(n,a,l))return null;const f=e(n,{errorRecovery:!1,plugins:o(i.filename,i.parserPlugins),sourceFilename:i.filename,sourceType:"module"}),p=B(f.program,a,i),d=J(f.program,l,i);if(0===p.size&&0===d.size)return null;const m=new Set,y=[],h=[];D(f.program,{functions:p,namespaces:d},(e,t)=>{if(!0===t.namespace)return void(M(n,e.start)||m.add(e.start));h.push(e);const r=p.get(e.callee.name),o=!0===i.inline?r.inline:void 0;void 0!==o&&!t.discarded&&function(e,t){const n=e.arguments;return"identity"!==t.form||1===n.length&&"SpreadElement"!==n[0].type&&"SequenceExpression"!==n[0].type}(e,o)?y.push([e,o,t.statementStart]):M(n,e.start)||m.add(e.start)});const g=Array.from(m);if(!0===i.strict){const e=new Set(m);for(const[t]of y)e.add(t.start);!function(e,t,n,r){const o=[],s=new Set;for(const t of n)X(t,n=>{const i=n.callee||n.tag;if(r.has(n.start)||M(e,n.start)||s.has(n.start)||"Identifier"===i.type&&E.has(i.name))return;s.add(n.start);const a=e.slice(i.start,i.end).replace(/\s+/g," "),c=n.loc?`${n.loc.start.line}:${n.loc.start.column+1}`:"?";o.push(` ${c} ${a}(...) inside ${e.slice(t.callee.start,t.callee.end)}(...)`)});if(o.length>0)throw new c(`@lexical/compiler: ${o.length} call(s) evaluated inside a definition in ${t||"<unknown>"} are not known to be side-effect free, so the definition cannot be tree-shaken:\n${o.join("\n")}\nMake the call lazy, or declare the function side-effect free (@__NO_SIDE_EFFECTS__ plus the transform's \`functions\`/\`namespaces\` options) so that its calls are annotated too.`)}(n,i.filename,h,e)}if(0===g.length&&0===y.length)return null;const S=new t(n);for(const e of g)S.appendLeft(e,"/* @__PURE__ */");for(const[e,t,r]of y)A(S,n,e,t,r);return O(S,n,f.program,y.map(([e])=>e)),{code:S.toString(),count:g.length,inlined:y.length,map:!1===i.sourceMap?void 0:S.generateMap({hires:"boundary",source:i.filename})}}function K(e,t){return void 0===e?t:Array.isArray(e)?e:[e]}function U(e,t){return e.some(e=>e.test(t))}const G=s,H=u,Q=p;function Y(e,t){return q(e,t)}function Z(t,n){return function(t,n){const r=n||{},i=new Set(r.functions||s),a=e(t,{errorRecovery:!1,plugins:o(r.filename,r.parserPlugins),sourceFilename:r.filename,sourceType:"module"});return{functions:new Set(B(a.program,i,r).keys()),namespaces:J(a.program,new Set(r.namespaces||u),r)}}(t,n)}function ee(e){return function(e){const t=e||{},n=new Set(t.functions||s),r=K(t.include,[h]),o=K(t.exclude,[]);return{enforce:void 0===t.enforce?"post":t.enforce,name:"@lexical/compiler",transform(e,s){const i=function(e){return e.replace(/[?#][\s\S]*$/,"")}(s);if(!U(r,i)||U(o,i))return null;const a=b.test(i);let l=null;try{l=q(e,{filename:i,functions:n,inline:t.inline,namespaces:t.namespaces,parserPlugins:t.parserPlugins,relativeImports:t.relativeImports,sourceMap:t.sourceMap,sources:t.sources,strict:!0===t.strict&&!a})}catch(e){if(e instanceof c&&!a)throw e;return this&&"function"==typeof this.warn&&this.warn(`@lexical/compiler could not parse ${s}, no annotations were added: ${e instanceof Error?e.message:String(e)}`),null}return null===l?null:{code:l.code,map:void 0===l.map?null:l.map}}}}(e)}export{Q as INLINE_FACTORY_FORMS,G as PURE_FACTORY_FUNCTIONS,H as PURE_NAMESPACES,ee as pureAnnotations,Z as pureCallNames,Y as transformPureAnnotations};
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"rollup-plugin",
|
|
10
10
|
"tree-shaking"
|
|
11
11
|
],
|
|
12
|
-
"version": "0.51.0",
|
|
12
|
+
"version": "0.51.1-nightly.20260921.0",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"repository": {
|
|
15
15
|
"type": "git",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"homepage": "https://lexical.dev/docs/packages/lexical-compiler",
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@babel/parser": "^8.0.4",
|
|
25
|
-
"lexical": "0.51.0",
|
|
25
|
+
"lexical": "0.51.1-nightly.20260921.0",
|
|
26
26
|
"magic-string": "^0.30.21"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|