@scriptc/compiler 0.4.3 → 0.4.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 (232) hide show
  1. package/ambient/scriptc-node-fallback.d.ts +73 -29
  2. package/dist/backend/llvm/bytes-loop-versions.js +6 -0
  3. package/dist/backend/llvm/bytes-loop-versions.js.map +1 -1
  4. package/dist/backend/llvm/bytes-loop-versions.test.js +7 -2
  5. package/dist/backend/llvm/bytes-loop-versions.test.js.map +1 -1
  6. package/dist/backend/llvm/debug-info.js +2 -5
  7. package/dist/backend/llvm/debug-info.js.map +1 -1
  8. package/dist/backend/llvm/dyn.d.ts +4 -0
  9. package/dist/backend/llvm/dyn.js +21 -9
  10. package/dist/backend/llvm/dyn.js.map +1 -1
  11. package/dist/backend/llvm/emit-function.js +2 -8
  12. package/dist/backend/llvm/emit-function.js.map +1 -1
  13. package/dist/backend/llvm/emit-module.js +16 -6
  14. package/dist/backend/llvm/emit-module.js.map +1 -1
  15. package/dist/backend/llvm/emitter.d.ts +10 -0
  16. package/dist/backend/llvm/emitter.js +9 -5
  17. package/dist/backend/llvm/emitter.js.map +1 -1
  18. package/dist/backend/llvm/emitter.test.js +11 -2
  19. package/dist/backend/llvm/emitter.test.js.map +1 -1
  20. package/dist/backend/llvm/expr-async.js +11 -13
  21. package/dist/backend/llvm/expr-async.js.map +1 -1
  22. package/dist/backend/llvm/expr-bytes.js +14 -8
  23. package/dist/backend/llvm/expr-bytes.js.map +1 -1
  24. package/dist/backend/llvm/expr-containers.js +14 -0
  25. package/dist/backend/llvm/expr-containers.js.map +1 -1
  26. package/dist/backend/llvm/expr-primitives.js +20 -11
  27. package/dist/backend/llvm/expr-primitives.js.map +1 -1
  28. package/dist/backend/llvm/expr-stream-bridges.js +16 -4
  29. package/dist/backend/llvm/expr-stream-bridges.js.map +1 -1
  30. package/dist/backend/llvm/integer-values.d.ts +5 -0
  31. package/dist/backend/llvm/integer-values.js +27 -3
  32. package/dist/backend/llvm/integer-values.js.map +1 -1
  33. package/dist/backend/llvm/lib-dispatch.js +133 -3
  34. package/dist/backend/llvm/lib-dispatch.js.map +1 -1
  35. package/dist/backend/llvm/lib-process.js +37 -1
  36. package/dist/backend/llvm/lib-process.js.map +1 -1
  37. package/dist/backend/llvm/lib-shared.js +21 -0
  38. package/dist/backend/llvm/lib-shared.js.map +1 -1
  39. package/dist/backend/llvm/publish.js +3 -3
  40. package/dist/backend/llvm/publish.js.map +1 -1
  41. package/dist/backend/llvm/reference-effects.js +3 -0
  42. package/dist/backend/llvm/reference-effects.js.map +1 -1
  43. package/dist/backend/llvm/shapes.js +2 -7
  44. package/dist/backend/llvm/shapes.js.map +1 -1
  45. package/dist/backend/llvm/stack-captures.js +1 -5
  46. package/dist/backend/llvm/stack-captures.js.map +1 -1
  47. package/dist/backend/llvm/union-repr.d.ts +4 -3
  48. package/dist/backend/llvm/union-repr.js +6 -5
  49. package/dist/backend/llvm/union-repr.js.map +1 -1
  50. package/dist/backend/llvm/walkers.js +13 -0
  51. package/dist/backend/llvm/walkers.js.map +1 -1
  52. package/dist/backend/native-codegen-core.d.ts +5 -3
  53. package/dist/backend/native-codegen-core.js +13 -6
  54. package/dist/backend/native-codegen-core.js.map +1 -1
  55. package/dist/backend/native-codegen.test.js +6 -4
  56. package/dist/backend/native-codegen.test.js.map +1 -1
  57. package/dist/backend/target-cpu.d.ts +26 -0
  58. package/dist/backend/target-cpu.js +98 -0
  59. package/dist/backend/target-cpu.js.map +1 -0
  60. package/dist/backend/target-cpu.test.d.ts +1 -0
  61. package/dist/backend/target-cpu.test.js +38 -0
  62. package/dist/backend/target-cpu.test.js.map +1 -0
  63. package/dist/cli/command.js +20 -1
  64. package/dist/cli/command.js.map +1 -1
  65. package/dist/cli/usage.d.ts +4 -1
  66. package/dist/cli/usage.js +7 -0
  67. package/dist/cli/usage.js.map +1 -1
  68. package/dist/compile-types.d.ts +7 -0
  69. package/dist/diagnostics/diagnostic.js +1 -1
  70. package/dist/diagnostics/diagnostic.js.map +1 -1
  71. package/dist/executable/executable-cache.d.ts +3 -0
  72. package/dist/executable/executable-cache.js +4 -0
  73. package/dist/executable/executable-cache.js.map +1 -1
  74. package/dist/executable/executable-cache.test.js +2 -0
  75. package/dist/executable/executable-cache.test.js.map +1 -1
  76. package/dist/frontend/decl-projection.js +105 -10
  77. package/dist/frontend/decl-projection.js.map +1 -1
  78. package/dist/frontend/lowering/builtins/async-context.d.ts +8 -2
  79. package/dist/frontend/lowering/builtins/async-context.js +62 -3
  80. package/dist/frontend/lowering/builtins/async-context.js.map +1 -1
  81. package/dist/frontend/lowering/builtins/crypto.js +3 -1
  82. package/dist/frontend/lowering/builtins/crypto.js.map +1 -1
  83. package/dist/frontend/lowering/builtins/dates.js +4 -1
  84. package/dist/frontend/lowering/builtins/dates.js.map +1 -1
  85. package/dist/frontend/lowering/builtins/filesystem-calls.js +3 -3
  86. package/dist/frontend/lowering/builtins/filesystem-calls.js.map +1 -1
  87. package/dist/frontend/lowering/builtins/fs-glob.js +6 -3
  88. package/dist/frontend/lowering/builtins/fs-glob.js.map +1 -1
  89. package/dist/frontend/lowering/builtins/fs-paths.d.ts +43 -5
  90. package/dist/frontend/lowering/builtins/fs-paths.js +245 -93
  91. package/dist/frontend/lowering/builtins/fs-paths.js.map +1 -1
  92. package/dist/frontend/lowering/builtins/module-calls.js +6 -3
  93. package/dist/frontend/lowering/builtins/module-calls.js.map +1 -1
  94. package/dist/frontend/lowering/builtins/promises.d.ts +1 -1
  95. package/dist/frontend/lowering/builtins/promises.js +149 -138
  96. package/dist/frontend/lowering/builtins/promises.js.map +1 -1
  97. package/dist/frontend/lowering/call-signatures.js +0 -1
  98. package/dist/frontend/lowering/call-signatures.js.map +1 -1
  99. package/dist/frontend/lowering/cjs-exports-object.js +1 -1
  100. package/dist/frontend/lowering/cjs-exports-object.js.map +1 -1
  101. package/dist/frontend/lowering/cjs-inline-require.d.ts +32 -0
  102. package/dist/frontend/lowering/cjs-inline-require.js +307 -4
  103. package/dist/frontend/lowering/cjs-inline-require.js.map +1 -1
  104. package/dist/frontend/lowering/class-dynamic-dispatch.d.ts +10 -0
  105. package/dist/frontend/lowering/class-dynamic-dispatch.js +114 -0
  106. package/dist/frontend/lowering/class-dynamic-dispatch.js.map +1 -1
  107. package/dist/frontend/lowering/class-method-mutations.js +54 -18
  108. package/dist/frontend/lowering/class-method-mutations.js.map +1 -1
  109. package/dist/frontend/lowering/class-protocols.d.ts +25 -0
  110. package/dist/frontend/lowering/class-protocols.js +183 -1
  111. package/dist/frontend/lowering/class-protocols.js.map +1 -1
  112. package/dist/frontend/lowering/completion-callbacks.d.ts +4 -0
  113. package/dist/frontend/lowering/completion-callbacks.js +146 -0
  114. package/dist/frontend/lowering/completion-callbacks.js.map +1 -0
  115. package/dist/frontend/lowering/containers/array-callbacks.d.ts +5 -4
  116. package/dist/frontend/lowering/containers/array-callbacks.js +7 -6
  117. package/dist/frontend/lowering/containers/array-callbacks.js.map +1 -1
  118. package/dist/frontend/lowering/containers/array-construction.js +5 -0
  119. package/dist/frontend/lowering/containers/array-construction.js.map +1 -1
  120. package/dist/frontend/lowering/containers/array-methods.js +11 -3
  121. package/dist/frontend/lowering/containers/array-methods.js.map +1 -1
  122. package/dist/frontend/lowering/containers/bytes.d.ts +25 -17
  123. package/dist/frontend/lowering/containers/bytes.js +453 -45
  124. package/dist/frontend/lowering/containers/bytes.js.map +1 -1
  125. package/dist/frontend/lowering/date-values.d.ts +11 -0
  126. package/dist/frontend/lowering/date-values.js +207 -0
  127. package/dist/frontend/lowering/date-values.js.map +1 -0
  128. package/dist/frontend/lowering/expressions/array-literals.js +6 -0
  129. package/dist/frontend/lowering/expressions/array-literals.js.map +1 -1
  130. package/dist/frontend/lowering/expressions/binary.js +64 -26
  131. package/dist/frontend/lowering/expressions/binary.js.map +1 -1
  132. package/dist/frontend/lowering/expressions/element-access.js +79 -0
  133. package/dist/frontend/lowering/expressions/element-access.js.map +1 -1
  134. package/dist/frontend/lowering/expressions/identifiers.js +31 -1
  135. package/dist/frontend/lowering/expressions/identifiers.js.map +1 -1
  136. package/dist/frontend/lowering/expressions/property-access.js +56 -0
  137. package/dist/frontend/lowering/expressions/property-access.js.map +1 -1
  138. package/dist/frontend/lowering/expressions/typeof.js +6 -0
  139. package/dist/frontend/lowering/expressions/typeof.js.map +1 -1
  140. package/dist/frontend/lowering/expressions/updates.js +17 -0
  141. package/dist/frontend/lowering/expressions/updates.js.map +1 -1
  142. package/dist/frontend/lowering/function-returns.js +15 -0
  143. package/dist/frontend/lowering/function-returns.js.map +1 -1
  144. package/dist/frontend/lowering/indexed-unions.d.ts +54 -0
  145. package/dist/frontend/lowering/indexed-unions.js +401 -0
  146. package/dist/frontend/lowering/indexed-unions.js.map +1 -0
  147. package/dist/frontend/lowering/key-order-witness.js +210 -32
  148. package/dist/frontend/lowering/key-order-witness.js.map +1 -1
  149. package/dist/frontend/lowering/key-order-witness.test.js +104 -4
  150. package/dist/frontend/lowering/key-order-witness.test.js.map +1 -1
  151. package/dist/frontend/lowering/lower-assert.js +15 -3
  152. package/dist/frontend/lowering/lower-assert.js.map +1 -1
  153. package/dist/frontend/lowering/lower-calls.js +19 -3
  154. package/dist/frontend/lowering/lower-calls.js.map +1 -1
  155. package/dist/frontend/lowering/lower-classes.js +22 -47
  156. package/dist/frontend/lowering/lower-classes.js.map +1 -1
  157. package/dist/frontend/lowering/lower-exprs.js +205 -1
  158. package/dist/frontend/lowering/lower-exprs.js.map +1 -1
  159. package/dist/frontend/lowering/lower-generators.js +1 -1
  160. package/dist/frontend/lowering/lower-generators.js.map +1 -1
  161. package/dist/frontend/lowering/lower-inspect.d.ts +1 -1
  162. package/dist/frontend/lowering/lower-inspect.js +35 -11
  163. package/dist/frontend/lowering/lower-inspect.js.map +1 -1
  164. package/dist/frontend/lowering/lower-modules.js +21 -5
  165. package/dist/frontend/lowering/lower-modules.js.map +1 -1
  166. package/dist/frontend/lowering/lower-stmts.js +100 -34
  167. package/dist/frontend/lowering/lower-stmts.js.map +1 -1
  168. package/dist/frontend/lowering/lowerer.d.ts +30 -2
  169. package/dist/frontend/lowering/lowerer.js +68 -7
  170. package/dist/frontend/lowering/lowerer.js.map +1 -1
  171. package/dist/frontend/lowering/object-factory-new.d.ts +5 -0
  172. package/dist/frontend/lowering/object-factory-new.js +20 -6
  173. package/dist/frontend/lowering/object-factory-new.js.map +1 -1
  174. package/dist/frontend/lowering/overload-choice.d.ts +33 -0
  175. package/dist/frontend/lowering/overload-choice.js +190 -0
  176. package/dist/frontend/lowering/overload-choice.js.map +1 -0
  177. package/dist/frontend/lowering/surfaces.js +55 -2
  178. package/dist/frontend/lowering/surfaces.js.map +1 -1
  179. package/dist/frontend/pipeline.js +1 -1
  180. package/dist/frontend/pipeline.js.map +1 -1
  181. package/dist/frontend/program-analysis.d.ts +5 -1
  182. package/dist/frontend/program-analysis.js +5 -0
  183. package/dist/frontend/program-analysis.js.map +1 -1
  184. package/dist/frontend/program.d.ts +13 -5
  185. package/dist/frontend/program.js +197 -14
  186. package/dist/frontend/program.js.map +1 -1
  187. package/dist/frontend/ts7/semantic-model.d.ts +3 -0
  188. package/dist/frontend/ts7/semantic-model.js +5 -0
  189. package/dist/frontend/ts7/semantic-model.js.map +1 -1
  190. package/dist/frontend/ts7/semantic-model.test.js +6 -1
  191. package/dist/frontend/ts7/semantic-model.test.js.map +1 -1
  192. package/dist/frontend/ts7/semantic-types.d.ts +1 -1
  193. package/dist/frontend/ts7/semantic-types.js +1 -1
  194. package/dist/frontend/ts7/semantic-types.js.map +1 -1
  195. package/dist/frontend/type-mapper.js +45 -16
  196. package/dist/frontend/type-mapper.js.map +1 -1
  197. package/dist/index.js +12 -0
  198. package/dist/index.js.map +1 -1
  199. package/dist/ir/builtin-effects.js +8 -0
  200. package/dist/ir/builtin-effects.js.map +1 -1
  201. package/dist/ir/builtin-signatures.d.ts +1 -1
  202. package/dist/ir/builtin-signatures.js +56 -0
  203. package/dist/ir/builtin-signatures.js.map +1 -1
  204. package/dist/ir/int32-slots.js +7 -0
  205. package/dist/ir/int32-slots.js.map +1 -1
  206. package/dist/ir/int32-slots.test.js +33 -0
  207. package/dist/ir/int32-slots.test.js.map +1 -1
  208. package/dist/ir/integer-loops.js +14 -4
  209. package/dist/ir/integer-loops.js.map +1 -1
  210. package/dist/ir/ir.d.ts +13 -3
  211. package/dist/ir/ir.js +19 -5
  212. package/dist/ir/ir.js.map +1 -1
  213. package/dist/ir/ir.test.js +2 -1
  214. package/dist/ir/ir.test.js.map +1 -1
  215. package/dist/ir/js-ic.d.ts +2 -15
  216. package/dist/ir/js-ic.js +11 -79
  217. package/dist/ir/js-ic.js.map +1 -1
  218. package/dist/ir/js-ic.test.js +0 -142
  219. package/dist/ir/js-ic.test.js.map +1 -1
  220. package/dist/ir/runtime-features.js +25 -1
  221. package/dist/ir/runtime-features.js.map +1 -1
  222. package/dist/ir/validate.js +46 -5
  223. package/dist/ir/validate.js.map +1 -1
  224. package/dist/library/ir-policy.js +1 -0
  225. package/dist/library/ir-policy.js.map +1 -1
  226. package/dist/native/prepare.d.ts +1 -1
  227. package/dist/native/prepare.js +11 -1
  228. package/dist/native/prepare.js.map +1 -1
  229. package/dist/native/prepare.test.js +18 -2
  230. package/dist/native/prepare.test.js.map +1 -1
  231. package/package.json +20 -20
  232. package/surface-manifest.json +2 -4
@@ -130,13 +130,21 @@ declare function requireSpecOf7(node: ts.Node): string | null;
130
130
  * These lower to NOTHING — the bindings are alias plumbing (tsc models
131
131
  * them as import aliases) and the module edge lives in the order walk. */
132
132
  declare function isRequireStatement7(stmt: ts.Statement): boolean;
133
- /** The require() occurrences of one top-level statement: the declaration
134
- * forms and the bare side-effect call. Empty for everything else. */
135
- declare function requiresOf7(stmt: ts.Statement): {
133
+ /** A require() occurrence of a top-level statement. `split` marks the
134
+ * assignment of a split require binding (splitRequireBindings7): `node`
135
+ * is its require call and `decl` the binding's declaration. */
136
+ interface RequireOccurrence7 {
136
137
  spec: string;
137
138
  node: ts.Node;
138
139
  decl: ts.VariableDeclaration | null;
139
- }[];
140
+ split?: true;
141
+ }
142
+ /** The require() occurrences of one top-level statement: the declaration
143
+ * forms, the bare side-effect call, and the assignment of a split require
144
+ * binding. Empty for everything else. */
145
+ declare function requiresOf7(stmt: ts.Statement, program: ts.Program): RequireOccurrence7[];
146
+ /** The require call of a split require binding's declaration, or null. */
147
+ declare function splitRequireCallOf7(program: ts.Program, decl: ts.VariableDeclaration): ts.CallExpression | null;
140
148
  /** The exact `Object.defineProperty(exports|module.exports, "__esModule",
141
149
  * { value: true })` interop stamp (shared by preflight and the no-op lowering);
142
150
  * `{ value: !0 }` is the same stamp. */
@@ -301,7 +309,7 @@ export declare function esmNamedImportLinkCrash(program: ts.Program, entry: ts.S
301
309
  export { ambientDtsPath, fallbackDtsPath, isNodeTypesPath, overridesDtsPath } from "./dts-paths.js";
302
310
  export { builtinDefaultImportModule, canonicalBuiltinModule, npmBuiltinAliasOf, SUPPORTED_BUILTIN_MODULES, } from "./builtin-modules.js";
303
311
  export { npmPackageNameOf, workspacePackageOfPath } from "./workspace-registry.js";
304
- export { requireSpecOf7 as requireSpecOf, isRequireStatement7 as isRequireStatement, requiresOf7 as requiresOf, isNodeEsmFile7 as isNodeEsmFile, resolveImport7 as resolveImport, resolveNpmImport7 as resolveNpmImport, locOf7 as locOf, requireEarlyReference7 as requireEarlyReference, inDeferredPosition7 as inDeferredPosition, };
312
+ export { requireSpecOf7 as requireSpecOf, isRequireStatement7 as isRequireStatement, requiresOf7 as requiresOf, splitRequireCallOf7 as splitRequireCallOf, isNodeEsmFile7 as isNodeEsmFile, resolveImport7 as resolveImport, resolveNpmImport7 as resolveNpmImport, locOf7 as locOf, requireEarlyReference7 as requireEarlyReference, inDeferredPosition7 as inDeferredPosition, };
305
313
  /** True for JavaScript source files (.js/.mjs/.cjs) — the files whose
306
314
  * types come entirely from inference (checkJs + JSDoc) and whose classes
307
315
  * declare fields by constructor assignment. */
@@ -1087,6 +1087,11 @@ function suppressedJsStrictness7(d) {
1087
1087
  if (JS_ANY_OPERATOR_CODES.has(d.code)) {
1088
1088
  return ts.flattenDiagnosticMessageText(d, "\n").includes("'any'");
1089
1089
  }
1090
+ // The deprecated Buffer(...) call without `new`, which Node's Buffer
1091
+ // function accepts (containers/bytes.ts lowers it).
1092
+ if (d.code === 2348) {
1093
+ return ts.flattenDiagnosticMessageText(d, "\n").includes("'BufferConstructor'");
1094
+ }
1090
1095
  return false;
1091
1096
  }
1092
1097
  /** The specifier of a `require("...")` call: the callee is the bare
@@ -1126,9 +1131,15 @@ function isRequireStatement7(stmt) {
1126
1131
  decls.every((d) => d.initializer !== undefined && requireSpecOf7(d.initializer) !== null));
1127
1132
  }
1128
1133
  /** The require() occurrences of one top-level statement: the declaration
1129
- * forms and the bare side-effect call. Empty for everything else. */
1130
- function requiresOf7(stmt) {
1134
+ * forms, the bare side-effect call, and the assignment of a split require
1135
+ * binding. Empty for everything else. */
1136
+ function requiresOf7(stmt, program) {
1131
1137
  if (ts.isExpressionStatement(stmt)) {
1138
+ const split = splitRequireOfStatement7(program, stmt);
1139
+ if (split !== null)
1140
+ return [
1141
+ { spec: requireSpecOf7(split.call), node: split.call, decl: split.decl, split: true },
1142
+ ];
1132
1143
  const spec = requireSpecOf7(stmt.expression);
1133
1144
  return spec !== null ? [{ spec, node: stmt.expression, decl: null }] : [];
1134
1145
  }
@@ -1142,6 +1153,158 @@ function requiresOf7(stmt) {
1142
1153
  }
1143
1154
  return out;
1144
1155
  }
1156
+ /** CommonJS require bindings split into a declaration and one later
1157
+ * assignment (`var m; m = require("./x")`): a top-level `var` or `let`
1158
+ * declaration without initializer in a CommonJS file, assigned by exactly one
1159
+ * top-level `m = require("<literal>")` statement of the require global over
1160
+ * a compiled program module, and written nowhere else. The binding holds the
1161
+ * module's exports from the assignment on, as `const m = require("./x")`
1162
+ * does, so preflight treats the assignment as that require: its module edge,
1163
+ * its cycle admission and its early-read fence. The lowering's recognizer
1164
+ * (splitRequireBindingOf in cjs-inline-require.ts) accepts only these
1165
+ * bindings. Keyed by declaration, with the assignment's require call. */
1166
+ function splitRequireBindings7(program, sf) {
1167
+ const memo = program.analysis.splitRequireBindings;
1168
+ const cached = memo.get(sf);
1169
+ if (cached !== undefined)
1170
+ return cached;
1171
+ const out = new Map();
1172
+ memo.set(sf, out);
1173
+ if (sf.isDeclarationFile || !isCjsJsFile7(sf, program) || !sf.text.includes("require"))
1174
+ return out;
1175
+ const checker = program.getTypeChecker();
1176
+ const stmts = sf.statements.nodes;
1177
+ const candidates = new Map();
1178
+ for (const stmt of stmts) {
1179
+ if (!ts.isVariableStatement(stmt))
1180
+ continue;
1181
+ if ((stmt.declarationList.flags & (ts.NodeFlags.Const | ts.NodeFlags.Using)) !== 0)
1182
+ continue;
1183
+ for (const decl of stmt.declarationList.declarations.nodes) {
1184
+ if (!ts.isIdentifier(decl.name) || decl.initializer !== undefined)
1185
+ continue;
1186
+ const symbol = checker.getSymbolAtLocation(decl.name);
1187
+ if (symbol === undefined || symbol.declarations.length !== 1)
1188
+ continue;
1189
+ candidates.set(symbol, { decl, call: null, assigns: 0 });
1190
+ }
1191
+ }
1192
+ if (candidates.size === 0)
1193
+ return out;
1194
+ // The one top-level `m = ...` statement, whose right side is the require.
1195
+ for (const stmt of stmts) {
1196
+ if (!ts.isExpressionStatement(stmt) || !ts.isBinaryExpression(stmt.expression))
1197
+ continue;
1198
+ const assign = stmt.expression;
1199
+ if (assign.operatorToken.kind !== ts.SyntaxKind.EqualsToken || !ts.isIdentifier(assign.left))
1200
+ continue;
1201
+ const symbol = checker.getSymbolAtLocation(assign.left);
1202
+ const candidate = symbol !== undefined ? candidates.get(symbol) : undefined;
1203
+ if (candidate === undefined)
1204
+ continue;
1205
+ candidate.assigns++;
1206
+ let right = assign.right;
1207
+ while (ts.isParenthesizedExpression(right))
1208
+ right = right.expression;
1209
+ if (ts.isCallExpression(right) && splitRequireTarget7(program, sf, right))
1210
+ candidate.call = right;
1211
+ }
1212
+ const names = new Set();
1213
+ for (const candidate of candidates.values()) {
1214
+ if (candidate.assigns === 1 && candidate.call !== null)
1215
+ names.add(candidate.decl.name.text);
1216
+ }
1217
+ if (names.size === 0)
1218
+ return out;
1219
+ // Exactly one write in the whole file: the assignment itself.
1220
+ const writes = new Map();
1221
+ ts.walkPreorder(sf, (node) => {
1222
+ if (!ts.isIdentifier(node) || !names.has(node.text))
1223
+ return undefined;
1224
+ const parent = node.parent;
1225
+ const symbol = parent !== undefined && ts.isShorthandPropertyAssignment(parent) && parent.name === node
1226
+ ? (checker.getShorthandAssignmentValueSymbol(parent) ?? checker.getSymbolAtLocation(node))
1227
+ : checker.getSymbolAtLocation(node);
1228
+ if (symbol === undefined || !candidates.has(symbol) || !isWriteReference7(node))
1229
+ return undefined;
1230
+ writes.set(symbol, (writes.get(symbol) ?? 0) + 1);
1231
+ return undefined;
1232
+ });
1233
+ for (const [symbol, candidate] of candidates) {
1234
+ if (candidate.assigns === 1 && candidate.call !== null && writes.get(symbol) === 1)
1235
+ out.set(candidate.decl, candidate.call);
1236
+ }
1237
+ return out;
1238
+ }
1239
+ /** True when `call` is a require of the CommonJS global over a compiled
1240
+ * program module: not a builtin, a JSON document or a declaration file. */
1241
+ function splitRequireTarget7(program, sf, call) {
1242
+ const spec = requireSpecOf7(call);
1243
+ if (spec === null || canonicalBuiltinModule(spec) !== null)
1244
+ return false;
1245
+ if (requireCallBindingKind7(program, call) !== "ambient")
1246
+ return false;
1247
+ const dep = resolveImport7(program, sf, spec, "require") ?? npmStaticDepSf7(program, sf, spec, "require");
1248
+ return dep !== null && !dep.isDeclarationFile && !dep.fileName.endsWith(".json");
1249
+ }
1250
+ /** The split require binding a top-level statement assigns, or null. */
1251
+ function splitRequireOfStatement7(program, stmt) {
1252
+ if (!ts.isExpressionStatement(stmt) ||
1253
+ !ts.isBinaryExpression(stmt.expression) ||
1254
+ !ts.isSourceFile(stmt.parent))
1255
+ return null;
1256
+ let right = stmt.expression.right;
1257
+ while (ts.isParenthesizedExpression(right))
1258
+ right = right.expression;
1259
+ for (const [decl, call] of splitRequireBindings7(program, stmt.parent))
1260
+ if (call === right)
1261
+ return { decl, call };
1262
+ return null;
1263
+ }
1264
+ /** The top-level statement of a split require binding's assignment, from
1265
+ * its require call (`m = require("./x")` or `m = (require("./x"))`). */
1266
+ function splitRequireStatementOf7(call) {
1267
+ let node = call;
1268
+ while (node.parent !== undefined && ts.isParenthesizedExpression(node.parent))
1269
+ node = node.parent;
1270
+ return node.parent?.parent;
1271
+ }
1272
+ /** The require call of a split require binding's declaration, or null. */
1273
+ function splitRequireCallOf7(program, decl) {
1274
+ return splitRequireBindings7(program, ts.getSourceFileOfNode(decl)).get(decl) ?? null;
1275
+ }
1276
+ /** True when `id` is written: an assignment target (compound and
1277
+ * destructuring included), an update operand, or a for-in/of target. */
1278
+ function isWriteReference7(id) {
1279
+ let n = id;
1280
+ for (;;) {
1281
+ const p = n.parent;
1282
+ if (p === undefined)
1283
+ return false;
1284
+ if (ts.isBinaryExpression(p)) {
1285
+ const k = p.operatorToken.kind;
1286
+ return (p.left === n && k >= ts.SyntaxKind.FirstAssignment && k <= ts.SyntaxKind.LastAssignment);
1287
+ }
1288
+ if (ts.isPostfixUnaryExpression(p) || ts.isPrefixUnaryExpression(p)) {
1289
+ return ((p.operator === ts.SyntaxKind.PlusPlusToken ||
1290
+ p.operator === ts.SyntaxKind.MinusMinusToken) &&
1291
+ p.operand === n);
1292
+ }
1293
+ if (ts.isForOfStatement(p) || ts.isForInStatement(p))
1294
+ return p.initializer === n;
1295
+ if (ts.isParenthesizedExpression(p) ||
1296
+ ts.isArrayLiteralExpression(p) ||
1297
+ ts.isSpreadElement(p) ||
1298
+ ts.isSpreadAssignment(p) ||
1299
+ ts.isShorthandPropertyAssignment(p) ||
1300
+ ts.isObjectLiteralExpression(p) ||
1301
+ (ts.isPropertyAssignment(p) && p.initializer === n)) {
1302
+ n = p;
1303
+ continue;
1304
+ }
1305
+ return false;
1306
+ }
1307
+ }
1145
1308
  /** `true`, or `!0`, its minified spelling. */
1146
1309
  function isTrueLiteral(expr) {
1147
1310
  return (expr.kind === ts.SyntaxKind.TrueKeyword ||
@@ -1269,7 +1432,7 @@ function purePrefixStmt7(program, s) {
1269
1432
  return true;
1270
1433
  if (ts.isExpressionStatement(s) && ts.isStringLiteral(s.expression))
1271
1434
  return true; // directive
1272
- if (isRequireStatement7(s))
1435
+ if (isRequireStatement7(s) || splitRequireOfStatement7(program, s) !== null)
1273
1436
  return true;
1274
1437
  if (isCjsJsFile7(ts.getSourceFileOfNode(s), program)) {
1275
1438
  if (ts.isExpressionStatement(s) && isEsModuleStamp(s.expression)) {
@@ -1393,12 +1556,18 @@ function requireEarlyReference7(program, decl) {
1393
1556
  if (cached !== undefined)
1394
1557
  return cached;
1395
1558
  let found = null;
1396
- const stmt = decl.parent?.parent;
1397
1559
  const sf = ts.getSourceFileOfNode(decl);
1398
- if (stmt !== undefined && ts.isVariableStatement(stmt) && ts.isSourceFile(stmt.parent)) {
1560
+ // A split require binding is bound at its assignment statement.
1561
+ const splitCall = splitRequireCallOf7(program, decl);
1562
+ const stmt = splitCall !== null ? splitRequireStatementOf7(splitCall) : decl.parent?.parent;
1563
+ if (stmt !== undefined &&
1564
+ (ts.isVariableStatement(stmt) || splitCall !== null) &&
1565
+ ts.isSourceFile(stmt.parent)) {
1399
1566
  const stmts = sf.statements.nodes;
1400
1567
  const k = stmts.indexOf(stmt);
1401
- const declarations = stmt.declarationList.declarations.nodes;
1568
+ const declarations = ts.isVariableStatement(stmt)
1569
+ ? stmt.declarationList.declarations.nodes
1570
+ : [];
1402
1571
  // Earlier declarators run too: `const x = read(), dep = require('./dep')`.
1403
1572
  const precedingDecls = declarations.slice(0, declarations.indexOf(decl));
1404
1573
  let firstRunnable = program.analysis.requireFirstRunnable.get(sf);
@@ -1446,6 +1615,12 @@ exactEarlyRead) {
1446
1615
  }
1447
1616
  if (bindings.size === 0)
1448
1617
  return null;
1618
+ // A split require binding's assignment writes the binding; it reads nothing.
1619
+ let splitWrite = splitRequireCallOf7(program, decl);
1620
+ while (splitWrite !== null && ts.isParenthesizedExpression(splitWrite.parent))
1621
+ splitWrite = splitWrite.parent;
1622
+ splitWrite =
1623
+ splitWrite !== null && ts.isBinaryExpression(splitWrite.parent) ? splitWrite.parent.left : null;
1449
1624
  const stmts = sf.statements.nodes;
1450
1625
  const cjs = isCjsJsFile7(sf, program);
1451
1626
  const symbolOf = (id) => {
@@ -1690,7 +1865,7 @@ exactEarlyRead) {
1690
1865
  if (sym === undefined)
1691
1866
  return undefined;
1692
1867
  if (bindings.has(sym)) {
1693
- if (exactEarlyRead?.(n) === true)
1868
+ if (n === splitWrite || exactEarlyRead?.(n) === true)
1694
1869
  return undefined;
1695
1870
  hit = n;
1696
1871
  return "stop";
@@ -3091,7 +3266,7 @@ function cjsCycleRefusal7(program, comp, lineOf) {
3091
3266
  }
3092
3267
  // Bindings of other members must stay unreachable from top-level code.
3093
3268
  for (const st of stmts) {
3094
- for (const req of requiresOf7(st)) {
3269
+ for (const req of requiresOf7(st, program)) {
3095
3270
  if (req.decl === null)
3096
3271
  continue;
3097
3272
  const dep = resolveImport7(program, m, req.spec, "require");
@@ -5221,17 +5396,20 @@ function preflight7(load) {
5221
5396
  const early = req.decl !== null ? requireEarlyReference7(program, req.decl) : null;
5222
5397
  if (early === null ||
5223
5398
  (bindingKind !== "createRequire" &&
5399
+ req.split === undefined &&
5224
5400
  ((req.decl.parent?.flags ?? 0) & (ts.NodeFlags.Const | ts.NodeFlags.Let)) !== 0)) {
5225
5401
  return false;
5226
5402
  }
5227
5403
  diags.push(unsupportedDiag("SC1013", loc, bindingKind === "createRequire"
5228
5404
  ? `createRequire() binding '${early.text}' below code that can reach it (Node throws ReferenceError on any earlier access, which is not modeled — move the require above the code that can run first)`
5229
- : `var require() binding '${early.text}' below code that can reach it (Node reads undefined before the require runs, which is not modeled — declare it with const or let, or move the require above the code that can run first)`));
5405
+ : req.split !== undefined
5406
+ ? `require() binding '${early.text}' assigned below code that can reach it (Node reads undefined before the assignment runs, which is not modeled — move the assignment above the code that can run first)`
5407
+ : `var require() binding '${early.text}' below code that can reach it (Node reads undefined before the require runs, which is not modeled — declare it with const or let, or move the require above the code that can run first)`));
5230
5408
  return true;
5231
5409
  };
5232
5410
  for (let k = 0; k < stmts.length; k++) {
5233
5411
  const stmt = stmts[k];
5234
- for (const req of requiresOf7(stmt)) {
5412
+ for (const req of requiresOf7(stmt, program)) {
5235
5413
  const loc = {
5236
5414
  file: sf.fileName,
5237
5415
  start: ts.tokenStartOfNode(req.node),
@@ -5260,8 +5438,8 @@ function preflight7(load) {
5260
5438
  if (jsonDep !== null && jsonDep.fileName.endsWith(".json")) {
5261
5439
  if (bindingKind === "createRequire")
5262
5440
  continue;
5263
- if (ts.isVariableStatement(stmt) && requiresOf7(stmt).length > 1) {
5264
- const others = requiresOf7(stmt).some((r) => {
5441
+ if (ts.isVariableStatement(stmt) && requiresOf7(stmt, program).length > 1) {
5442
+ const others = requiresOf7(stmt, program).some((r) => {
5265
5443
  const d = resolveImport7(program, sf, r.spec, "require");
5266
5444
  return d === null || !d.fileName.endsWith(".json");
5267
5445
  });
@@ -5423,9 +5601,14 @@ function preflight7(load) {
5423
5601
  // counts, and targets outside the compiled graph add nothing here:
5424
5602
  // their use sites keep their existing lowering fences.
5425
5603
  if (jsRequireFile && !isNodeEsmFile7(sf, program)) {
5604
+ // A split require binding's assignment is a statement-level require
5605
+ // edge (requiresOf7 above), parenthesized or not.
5606
+ const splitCalls = new Set(splitRequireBindings7(program, sf).values());
5426
5607
  for (const call of inlineRequiresOf7(sf)) {
5427
5608
  if (requireCallBindingKind7(program, call) !== "ambient")
5428
5609
  continue;
5610
+ if (splitCalls.has(call))
5611
+ continue;
5429
5612
  const spec = requireSpecOf7(call);
5430
5613
  if (load.externalTypes.has(spec) || canonicalBuiltinModule(spec) !== null)
5431
5614
  continue;
@@ -5798,7 +5981,7 @@ function analyzeEsmNamedImportLinks(program, entry) {
5798
5981
  const nextSeen = new Set(seenModules);
5799
5982
  nextSeen.add(dep);
5800
5983
  const module = checker.getSymbolAtLocation(dep);
5801
- const exported = module?.getExports().get(name);
5984
+ const exported = module?.getExports().get(ts.escapedSymbolName(name));
5802
5985
  if (exported !== undefined) {
5803
5986
  let resolved = exported;
5804
5987
  const seenSymbols = new Set();
@@ -6076,7 +6259,7 @@ const nodeBuiltinNames = new Set(builtinModules);
6076
6259
  export { ambientDtsPath, fallbackDtsPath, isNodeTypesPath, overridesDtsPath } from "./dts-paths.js";
6077
6260
  export { builtinDefaultImportModule, canonicalBuiltinModule, npmBuiltinAliasOf, SUPPORTED_BUILTIN_MODULES, } from "./builtin-modules.js";
6078
6261
  export { npmPackageNameOf, workspacePackageOfPath } from "./workspace-registry.js";
6079
- export { requireSpecOf7 as requireSpecOf, isRequireStatement7 as isRequireStatement, requiresOf7 as requiresOf, isNodeEsmFile7 as isNodeEsmFile, resolveImport7 as resolveImport, resolveNpmImport7 as resolveNpmImport, locOf7 as locOf, requireEarlyReference7 as requireEarlyReference, inDeferredPosition7 as inDeferredPosition, };
6262
+ export { requireSpecOf7 as requireSpecOf, isRequireStatement7 as isRequireStatement, requiresOf7 as requiresOf, splitRequireCallOf7 as splitRequireCallOf, isNodeEsmFile7 as isNodeEsmFile, resolveImport7 as resolveImport, resolveNpmImport7 as resolveNpmImport, locOf7 as locOf, requireEarlyReference7 as requireEarlyReference, inDeferredPosition7 as inDeferredPosition, };
6080
6263
  /** True for JavaScript source files (.js/.mjs/.cjs) — the files whose
6081
6264
  * types come entirely from inference (checkJs + JSDoc) and whose classes
6082
6265
  * declare fields by constructor assignment. */