@lexical/compiler 0.50.1-nightly.20260916.0 → 0.51.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.
- package/README.md +36 -0
- package/dist/LexicalCompiler.dev.js +1 -0
- package/dist/LexicalCompiler.js +16 -1
- package/dist/LexicalCompiler.js.flow +10 -22
- package/dist/LexicalCompiler.prod.js +1 -1
- package/dist/LexicalCompilerPureAnnotations.dev.js +20 -0
- package/dist/LexicalCompilerPureAnnotations.prod.js +1 -1
- package/dist/LexicalCompilerSchemaJsonCodegen.dev.js +797 -0
- package/dist/LexicalCompilerSchemaJsonCodegen.js +26 -0
- package/dist/LexicalCompilerSchemaJsonCodegen.js.flow +99 -0
- package/dist/LexicalCompilerSchemaJsonCodegen.prod.js +9 -0
- package/dist/LexicalCompilerSchemaJsonCodegenUsage.js.flow +86 -0
- package/dist/SchemaJsonCodegen.d.ts +277 -0
- package/dist/index.d.ts +1 -0
- package/package.json +19 -2
- package/src/SchemaJsonCodegen.ts +1094 -0
- package/src/index.ts +1 -0
- package/src/passes/pureAnnotations.mjs +20 -0
package/README.md
CHANGED
|
@@ -319,3 +319,39 @@ including the barrel's own re-exports. ESLint enforces this convention;
|
|
|
319
319
|
the compiler handles the published subpath layout. Direct subpath imports
|
|
320
320
|
remain supported for downstream consumers, including applications that need
|
|
321
321
|
small bundles without tree-shaking or the compiler plugin.
|
|
322
|
+
|
|
323
|
+
## SchemaJsonCodegen
|
|
324
|
+
|
|
325
|
+
The package's second entry point, `@lexical/compiler/SchemaJsonCodegen`, is
|
|
326
|
+
not part of the plugin pipeline: it is the library a code generator uses to
|
|
327
|
+
turn a node's declarative serialization schema (the `json` property of
|
|
328
|
+
`$config`) into straight-line JavaScript. Lexical's own
|
|
329
|
+
`scripts/generate-node-json.mjs` uses it to generate the specialized
|
|
330
|
+
`exportJSON`/`updateFromJSON` implementations the built-in nodes ship with.
|
|
331
|
+
|
|
332
|
+
- `compileParse(meta, defaultValue, tableBaseName)` compiles a schema's
|
|
333
|
+
introspectable `meta` into a JavaScript expression over `v` that parses
|
|
334
|
+
exactly as the schema does, plus any lookup tables the expression refers
|
|
335
|
+
to. Only the kinds whose meta fully determines the parse are compiled
|
|
336
|
+
(strings, numbers, booleans, enums, and `aliasedValue` tables); the rest
|
|
337
|
+
throw `NotCompilable` rather than emit a guess.
|
|
338
|
+
- Compiling is not trusting: `verifyCompiledParse` runs the compiled
|
|
339
|
+
expression against the real schema over `verificationCorpus(meta)` — every
|
|
340
|
+
value the schema names plus a fixed set of hostile inputs,
|
|
341
|
+
`Object.prototype` member names included — and throws naming the first
|
|
342
|
+
value they disagree on. `verifyTableCoversDomain` proves an emitted lookup
|
|
343
|
+
table total over a schema's domain, so its miss-fallback is dead code.
|
|
344
|
+
- The compact form's comparisons are compiled the same way.
|
|
345
|
+
`compileDiffersFromDefault(schema, name)` states the test that a value is
|
|
346
|
+
not the schema's default — a literal for a primitive default, and the
|
|
347
|
+
length test `arrayValue`'s equality reduces to for an empty-array default —
|
|
348
|
+
and `verifyDiffersFromDefault` runs it against the schema's own `isEqual`
|
|
349
|
+
over the corpus.
|
|
350
|
+
- `NUM_HELPER_SOURCE` and `JSON_NUMBER_SOURCE` are the number-parsing helper
|
|
351
|
+
as source text, so an emitted module and the verification share one
|
|
352
|
+
definition rather than two copies that could drift; `NUM_RANGE_HELPER_SOURCE`
|
|
353
|
+
is the constrained-domain variant.
|
|
354
|
+
|
|
355
|
+
The corpus is fixed rather than sampled, so a generator built on this
|
|
356
|
+
produces byte-reproducible output — which is what lets generated files be
|
|
357
|
+
checked in and drift-tested against regeneration.
|
|
@@ -7,4 +7,5 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
export { INLINE_FACTORY_FORMS, PURE_FACTORY_FUNCTIONS, PURE_NAMESPACES, pureAnnotations, pureCallNames, transformPureAnnotations } from '@lexical/compiler/PureAnnotations';
|
|
10
|
+
export { JSON_NUMBER_SOURCE, NUM_BODY, NUM_CLAMP_BODY, NUM_CLAMP_HELPER_SOURCE, NUM_HELPER_SOURCE, NUM_RANGE_BODY, NUM_RANGE_HELPER_SOURCE, NotCompilable, compileDiffersFromDefault, compileParse, literal, verificationCorpus, verifyCompiledParse, verifyDiffersFromDefault, verifyTableCoversDomain } from '@lexical/compiler/SchemaJsonCodegen';
|
|
10
11
|
export { subpathImports } from '@lexical/compiler/SubpathImports';
|
package/dist/LexicalCompiler.js
CHANGED
|
@@ -10,9 +10,24 @@ import * as modDev from './LexicalCompiler.dev.js';
|
|
|
10
10
|
import * as modProd from './LexicalCompiler.prod.js';
|
|
11
11
|
const mod = process.env.NODE_ENV !== 'production' ? modDev : modProd;
|
|
12
12
|
export const INLINE_FACTORY_FORMS = mod.INLINE_FACTORY_FORMS;
|
|
13
|
+
export const JSON_NUMBER_SOURCE = mod.JSON_NUMBER_SOURCE;
|
|
14
|
+
export const NUM_BODY = mod.NUM_BODY;
|
|
15
|
+
export const NUM_CLAMP_BODY = mod.NUM_CLAMP_BODY;
|
|
16
|
+
export const NUM_CLAMP_HELPER_SOURCE = mod.NUM_CLAMP_HELPER_SOURCE;
|
|
17
|
+
export const NUM_HELPER_SOURCE = mod.NUM_HELPER_SOURCE;
|
|
18
|
+
export const NUM_RANGE_BODY = mod.NUM_RANGE_BODY;
|
|
19
|
+
export const NUM_RANGE_HELPER_SOURCE = mod.NUM_RANGE_HELPER_SOURCE;
|
|
20
|
+
export const NotCompilable = mod.NotCompilable;
|
|
13
21
|
export const PURE_FACTORY_FUNCTIONS = mod.PURE_FACTORY_FUNCTIONS;
|
|
14
22
|
export const PURE_NAMESPACES = mod.PURE_NAMESPACES;
|
|
23
|
+
export const compileDiffersFromDefault = mod.compileDiffersFromDefault;
|
|
24
|
+
export const compileParse = mod.compileParse;
|
|
25
|
+
export const literal = mod.literal;
|
|
15
26
|
export const pureAnnotations = mod.pureAnnotations;
|
|
16
27
|
export const pureCallNames = mod.pureCallNames;
|
|
17
28
|
export const subpathImports = mod.subpathImports;
|
|
18
|
-
export const transformPureAnnotations = mod.transformPureAnnotations;
|
|
29
|
+
export const transformPureAnnotations = mod.transformPureAnnotations;
|
|
30
|
+
export const verificationCorpus = mod.verificationCorpus;
|
|
31
|
+
export const verifyCompiledParse = mod.verifyCompiledParse;
|
|
32
|
+
export const verifyDiffersFromDefault = mod.verifyDiffersFromDefault;
|
|
33
|
+
export const verifyTableCoversDomain = mod.verifyTableCoversDomain;
|
|
@@ -12,27 +12,15 @@
|
|
|
12
12
|
*
|
|
13
13
|
* Every pass, re-exported. Import a single pass from its own entry point
|
|
14
14
|
* (`@lexical/compiler/PureAnnotations`) to leave the rest unloaded.
|
|
15
|
+
*
|
|
16
|
+
* `export *`, as `src/index.ts` does, rather than one list per entry. Naming
|
|
17
|
+
* them here meant naming each one twice, and a name added to a pass was a name
|
|
18
|
+
* this file went on not having: `CompiledExpression`, `TableNaming`,
|
|
19
|
+
* `BindingNaming` and four helper-source constants were all missing from it,
|
|
20
|
+
* and the parity lint compares this file against the barrel rather than
|
|
21
|
+
* against each pass, so it saw a barrel that had simply stopped keeping up.
|
|
15
22
|
*/
|
|
16
23
|
|
|
17
|
-
export
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
PureAnnotationsResult,
|
|
21
|
-
PureAnnotationsSourceMap,
|
|
22
|
-
TransformPureAnnotationsOptions,
|
|
23
|
-
} from '@lexical/compiler/PureAnnotations';
|
|
24
|
-
|
|
25
|
-
export {
|
|
26
|
-
INLINE_FACTORY_FORMS,
|
|
27
|
-
PURE_FACTORY_FUNCTIONS,
|
|
28
|
-
PURE_NAMESPACES,
|
|
29
|
-
pureAnnotations,
|
|
30
|
-
pureCallNames,
|
|
31
|
-
transformPureAnnotations,
|
|
32
|
-
} from '@lexical/compiler/PureAnnotations';
|
|
33
|
-
|
|
34
|
-
export type {
|
|
35
|
-
SubpathImportsOptions,
|
|
36
|
-
SubpathImportsPlugin,
|
|
37
|
-
} from '@lexical/compiler/SubpathImports';
|
|
38
|
-
export {subpathImports} from '@lexical/compiler/SubpathImports';
|
|
24
|
+
export * from '@lexical/compiler/PureAnnotations';
|
|
25
|
+
export * from '@lexical/compiler/SchemaJsonCodegen';
|
|
26
|
+
export * from '@lexical/compiler/SubpathImports';
|
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
export{INLINE_FACTORY_FORMS,PURE_FACTORY_FUNCTIONS,PURE_NAMESPACES,pureAnnotations,pureCallNames,transformPureAnnotations}from"@lexical/compiler/PureAnnotations";export{subpathImports}from"@lexical/compiler/SubpathImports";
|
|
9
|
+
export{INLINE_FACTORY_FORMS,PURE_FACTORY_FUNCTIONS,PURE_NAMESPACES,pureAnnotations,pureCallNames,transformPureAnnotations}from"@lexical/compiler/PureAnnotations";export{JSON_NUMBER_SOURCE,NUM_BODY,NUM_CLAMP_BODY,NUM_CLAMP_HELPER_SOURCE,NUM_HELPER_SOURCE,NUM_RANGE_BODY,NUM_RANGE_HELPER_SOURCE,NotCompilable,compileDiffersFromDefault,compileParse,literal,verificationCorpus,verifyCompiledParse,verifyDiffersFromDefault,verifyTableCoversDomain}from"@lexical/compiler/SchemaJsonCodegen";export{subpathImports}from"@lexical/compiler/SubpathImports";
|
|
@@ -82,6 +82,11 @@ function parserPluginsFor(
|
|
|
82
82
|
* @type {ReadonlyArray<string>}
|
|
83
83
|
*/
|
|
84
84
|
const PURE_FACTORY_FUNCTIONS$1 = [
|
|
85
|
+
// The combinators that build a node's serialization schema at module
|
|
86
|
+
// scope, nested inside each other and inside createState.
|
|
87
|
+
'aliasedValue',
|
|
88
|
+
'arrayValue',
|
|
89
|
+
'booleanValue',
|
|
85
90
|
'configExtension',
|
|
86
91
|
'createCommand',
|
|
87
92
|
'createContextState',
|
|
@@ -95,8 +100,23 @@ const PURE_FACTORY_FUNCTIONS$1 = [
|
|
|
95
100
|
'defineImportRule',
|
|
96
101
|
'defineOverlayRules',
|
|
97
102
|
'domOverride',
|
|
103
|
+
'enumValue',
|
|
104
|
+
// @lexical/react publishes each module as its own entry, so this is public
|
|
105
|
+
// and callable by name from another package even though it is @internal.
|
|
106
|
+
'newContext',
|
|
107
|
+
'nodeSchema',
|
|
108
|
+
'nullable',
|
|
109
|
+
'numberValue',
|
|
110
|
+
'objectValue',
|
|
111
|
+
'optional',
|
|
112
|
+
'rawValue',
|
|
98
113
|
'safeCast',
|
|
114
|
+
'stringValue',
|
|
115
|
+
'transformValue',
|
|
116
|
+
'unionValue',
|
|
99
117
|
'warnOnlyOnce',
|
|
118
|
+
'withAccessors',
|
|
119
|
+
'withField',
|
|
100
120
|
];
|
|
101
121
|
|
|
102
122
|
/**
|
|
@@ -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=["configExtension","createCommand","createContextState","createImportState","createLinkMatcherWithRegExp","createRefCountedRegistry","createRenderState","createState","declarePeerDependency","defineExtension","defineImportRule","defineOverlayRules","domOverride","safeCast","warnOnlyOnce"],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 M(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 j(e,t){const n=M(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 P(e,t,n,r,o){const s=M(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 A=new Map,k=new Map;function F(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 T(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=F(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||F(e,t))}}else s&&n.set(e.id.name,i)}return n}function L(e,t){const o=r.resolve(r.dirname(e),t),s=A.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 A.set(o,c),c}function N(e,t){return B(e,t).functions}function B(t,r){const s=k.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:T(i,t),namespaces:z(i)};return k.set(t,a),a}function W(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 T(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=L(n.filename,e),a=null===o?void 0:N(o,n.parserPlugins).get(r.imported.name);void 0!==a&&s(r.local.name,r.imported.name,a,!1)}}return o}function z(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 V(e,t){return B(e,t).namespaces}function J(e,t,n){const r=new Set,o=K(n.sources,g);for(const t of z(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=L(n.filename,e);c=null===t?new Set:V(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=W(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(j(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]):j(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)||j(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)P(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(W(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.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};
|