@danielx/civet 0.6.87 → 0.6.88

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/dist/main.js CHANGED
@@ -1704,7 +1704,11 @@ function patternAsValue(pattern) {
1704
1704
  }
1705
1705
  case "Identifier":
1706
1706
  case "BindingProperty": {
1707
- const children = [pattern.name, pattern.delim];
1707
+ const children = [
1708
+ // { name: value } = ... declares value, not name
1709
+ pattern.value ?? pattern.name,
1710
+ pattern.delim
1711
+ ];
1708
1712
  if (isWhitespaceOrEmpty(pattern.children[0])) {
1709
1713
  children.unshift(pattern.children[0]);
1710
1714
  }
@@ -2312,6 +2316,111 @@ function processDeclarationConditionStatement(s, getRef) {
2312
2316
  }
2313
2317
  }
2314
2318
  }
2319
+ function dynamizeFromClause(from) {
2320
+ from = from.slice(1);
2321
+ from = insertTrimmingSpace(from, "");
2322
+ if (from.at(-1)?.type === "ImportAssertion") {
2323
+ const assert2 = from.pop();
2324
+ from.push(", {", assert2.keyword, ":", assert2.object, "}");
2325
+ }
2326
+ return ["(", ...from, ")"];
2327
+ }
2328
+ function dynamizeImportDeclaration(decl) {
2329
+ const { imports } = decl;
2330
+ let { star, binding, specifiers } = imports;
2331
+ const justDefault = binding && !specifiers && !star;
2332
+ const pattern = (() => {
2333
+ {
2334
+ if (binding) {
2335
+ if (specifiers) {
2336
+ return makeRef();
2337
+ } else {
2338
+ return binding;
2339
+ }
2340
+ } else {
2341
+ return convertNamedImportsToObject(imports, true);
2342
+ }
2343
+ }
2344
+ })();
2345
+ const c = "const";
2346
+ const initializer = [
2347
+ " = ",
2348
+ justDefault ? "(" : void 0,
2349
+ { type: "Await", children: ["await"] },
2350
+ " ",
2351
+ decl.children[0],
2352
+ // import
2353
+ dynamizeFromClause(decl.from),
2354
+ justDefault ? ").default" : void 0
2355
+ ];
2356
+ const bindings = [{
2357
+ type: "Binding",
2358
+ names: pattern.names,
2359
+ pattern,
2360
+ initializer,
2361
+ children: [pattern, initializer]
2362
+ }];
2363
+ if (binding && specifiers) {
2364
+ const pattern2 = binding;
2365
+ const initializer2 = [
2366
+ " = ",
2367
+ pattern,
2368
+ ".default"
2369
+ ];
2370
+ bindings.push({
2371
+ type: "Binding",
2372
+ names: binding.names,
2373
+ pattern: pattern2,
2374
+ initializer: initializer2,
2375
+ children: [pattern2, initializer2]
2376
+ });
2377
+ const pattern3 = convertNamedImportsToObject(imports.children.at(-1), true);
2378
+ const initializer3 = [
2379
+ " = ",
2380
+ pattern
2381
+ ];
2382
+ bindings.push({
2383
+ type: "Binding",
2384
+ names: specifiers.names,
2385
+ pattern: pattern3,
2386
+ initializer: initializer3,
2387
+ children: [pattern3, initializer3]
2388
+ });
2389
+ }
2390
+ return {
2391
+ type: "Declaration",
2392
+ names: imports.names,
2393
+ bindings,
2394
+ decl: c,
2395
+ children: [
2396
+ c,
2397
+ " ",
2398
+ bindings.flatMap((binding2, i) => i > 0 ? [", ", binding2] : [binding2])
2399
+ ]
2400
+ };
2401
+ }
2402
+ function dynamizeImportDeclarationExpression($0) {
2403
+ const [imp, ws1, named, ws2, from] = $0;
2404
+ const object = convertNamedImportsToObject(named);
2405
+ const dot = ".";
2406
+ return processCallMemberExpression({
2407
+ type: "CallExpression",
2408
+ children: [
2409
+ { type: "Await", children: "await" },
2410
+ " ",
2411
+ imp,
2412
+ insertTrimmingSpace(ws2, ""),
2413
+ dynamizeFromClause(from),
2414
+ {
2415
+ type: "PropertyGlob",
2416
+ dot,
2417
+ object,
2418
+ children: [ws1, dot, object],
2419
+ reversed: true
2420
+ }
2421
+ ]
2422
+ });
2423
+ }
2315
2424
  var init_declaration = __esm({
2316
2425
  "source/parser/declaration.civet"() {
2317
2426
  "use strict";
@@ -2321,6 +2430,7 @@ var init_declaration = __esm({
2321
2430
  init_util();
2322
2431
  init_function();
2323
2432
  init_binding();
2433
+ init_lib();
2324
2434
  }
2325
2435
  });
2326
2436
 
@@ -3137,10 +3247,13 @@ __export(lib_exports, {
3137
3247
  adjustIndexAccess: () => adjustIndexAccess,
3138
3248
  attachPostfixStatementAsExpression: () => attachPostfixStatementAsExpression,
3139
3249
  blockWithPrefix: () => blockWithPrefix,
3250
+ convertNamedImportsToObject: () => convertNamedImportsToObject,
3140
3251
  convertObjectToJSXAttributes: () => convertObjectToJSXAttributes,
3141
3252
  dedentBlockString: () => dedentBlockString,
3142
3253
  dedentBlockSubstitutions: () => dedentBlockSubstitutions,
3143
3254
  deepCopy: () => deepCopy,
3255
+ dynamizeImportDeclaration: () => dynamizeImportDeclaration,
3256
+ dynamizeImportDeclarationExpression: () => dynamizeImportDeclarationExpression,
3144
3257
  expressionizeTypeIf: () => expressionizeTypeIf,
3145
3258
  forRange: () => forRange,
3146
3259
  gatherBindingCode: () => gatherBindingCode,
@@ -3412,9 +3525,13 @@ function processCallMemberExpression(node) {
3412
3525
  throw new Error(`Glob pattern must have call or member expression value, found ${JSON.stringify(part.value)}`);
3413
3526
  }
3414
3527
  let suppressPrefix = false;
3415
- let value = part.value ?? part.name;
3528
+ let name = part.name;
3529
+ let value = part.value ?? name;
3416
3530
  const wValue = getTrimmingSpace(part.value);
3417
3531
  [value, suppressPrefix] = handleThisPrivateShorthands(value);
3532
+ if (glob.reversed) {
3533
+ [name, value] = [value, name];
3534
+ }
3418
3535
  if (!suppressPrefix) {
3419
3536
  value = prefix.concat(insertTrimmingSpace(value, ""));
3420
3537
  }
@@ -3432,13 +3549,13 @@ function processCallMemberExpression(node) {
3432
3549
  } else {
3433
3550
  parts.push({
3434
3551
  type: part.type === "Identifier" ? "Property" : part.type,
3435
- name: part.name,
3552
+ name,
3436
3553
  value,
3437
3554
  delim: part.delim,
3438
3555
  names: part.names,
3439
3556
  children: [
3440
3557
  isWhitespaceOrEmpty(part.children[0]) && part.children[0],
3441
- part.name,
3558
+ name,
3442
3559
  isWhitespaceOrEmpty(part.children[2]) && part.children[2],
3443
3560
  part.children[3]?.token === ":" ? part.children[3] : ":",
3444
3561
  value,
@@ -3583,6 +3700,35 @@ function convertMethodToFunction(method) {
3583
3700
  block
3584
3701
  };
3585
3702
  }
3703
+ function convertNamedImportsToObject(node, pattern) {
3704
+ const properties = node.specifiers.map((specifier) => {
3705
+ if (specifier.ts) {
3706
+ return { type: "Error", message: "cannot use `type` in dynamic import" };
3707
+ } else {
3708
+ const { source, binding } = specifier;
3709
+ const delim = specifier.children.at(-1);
3710
+ return {
3711
+ type: pattern ? "BindingProperty" : "Property",
3712
+ name: source,
3713
+ value: !(source === binding) ? binding : void 0,
3714
+ delim,
3715
+ children: source === binding ? [source, delim] : [source, ":", binding, delim]
3716
+ };
3717
+ }
3718
+ });
3719
+ return {
3720
+ type: pattern ? "ObjectBindingPattern" : "ObjectExpression",
3721
+ names: node.names,
3722
+ properties,
3723
+ children: [
3724
+ node.children[0],
3725
+ // {
3726
+ properties,
3727
+ node.children.at(-1)
3728
+ // }
3729
+ ]
3730
+ };
3731
+ }
3586
3732
  function convertObjectToJSXAttributes(obj) {
3587
3733
  const { properties } = obj;
3588
3734
  const parts = [];
@@ -7058,14 +7204,17 @@ var require_parser = __commonJS({
7058
7204
  children: [$1, ...$2, ...rest.flat()]
7059
7205
  });
7060
7206
  });
7061
- var CallExpression$1 = $TS($S($EXPECT($L20, 'CallExpression "import"'), ArgumentsWithTrailingMemberExpressions, $Q(CallExpressionRest)), function($skip, $loc, $0, $1, $2, $3) {
7207
+ var CallExpression$1 = $TS($S(Import, _, NamedImports, __, FromClause), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
7208
+ return dynamizeImportDeclarationExpression($0);
7209
+ });
7210
+ var CallExpression$2 = $TS($S($EXPECT($L20, 'CallExpression "import"'), ArgumentsWithTrailingMemberExpressions, $Q(CallExpressionRest)), function($skip, $loc, $0, $1, $2, $3) {
7062
7211
  var rest = $3;
7063
7212
  return processCallMemberExpression({
7064
7213
  type: "CallExpression",
7065
7214
  children: [$1, ...$2, ...rest.flat()]
7066
7215
  });
7067
7216
  });
7068
- var CallExpression$2 = $TS($S(MemberExpression, AllowedTrailingMemberExpressions, $Q(CallExpressionRest)), function($skip, $loc, $0, $1, $2, $3) {
7217
+ var CallExpression$3 = $TS($S(MemberExpression, AllowedTrailingMemberExpressions, $Q(CallExpressionRest)), function($skip, $loc, $0, $1, $2, $3) {
7069
7218
  var member = $1;
7070
7219
  var trailing = $2;
7071
7220
  var rest = $3;
@@ -7078,7 +7227,7 @@ var require_parser = __commonJS({
7078
7227
  }
7079
7228
  return member;
7080
7229
  });
7081
- var CallExpression$$ = [CallExpression$0, CallExpression$1, CallExpression$2];
7230
+ var CallExpression$$ = [CallExpression$0, CallExpression$1, CallExpression$2, CallExpression$3];
7082
7231
  function CallExpression(ctx, state) {
7083
7232
  return $EVENT_C(ctx, state, "CallExpression", CallExpression$$);
7084
7233
  }
@@ -11342,28 +11491,31 @@ var require_parser = __commonJS({
11342
11491
  };
11343
11492
  });
11344
11493
  var ImportDeclaration$1 = $T($S(Import, __, TypeKeyword, __, ImportClause, __, FromClause), function(value) {
11345
- return { "type": "ImportDeclaration", "ts": true, "children": value };
11494
+ var imports = value[4];
11495
+ var from = value[6];
11496
+ return { "type": "ImportDeclaration", "ts": true, "children": value, "imports": imports, "from": from };
11346
11497
  });
11347
11498
  var ImportDeclaration$2 = $T($S(Import, __, ImportClause, __, FromClause), function(value) {
11348
- return { "type": "ImportDeclaration", "children": value };
11499
+ var imports = value[2];
11500
+ var from = value[4];
11501
+ return { "type": "ImportDeclaration", "children": value, "imports": imports, "from": from };
11349
11502
  });
11350
11503
  var ImportDeclaration$3 = $T($S(Import, __, ModuleSpecifier), function(value) {
11351
- return { "type": "ImportDeclaration", "children": value };
11504
+ var module4 = value[2];
11505
+ return { "type": "ImportDeclaration", "children": value, "module": module4 };
11352
11506
  });
11353
11507
  var ImportDeclaration$4 = $TS($S(ImpliedImport, $E($S(TypeKeyword, __)), ImportClause, __, FromClause), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
11354
11508
  var i = $1;
11355
11509
  var t = $2;
11356
- var c = $3;
11510
+ var imports = $3;
11357
11511
  var w = $4;
11358
- var f = $5;
11512
+ var from = $5;
11359
11513
  i.$loc = {
11360
- pos: f[0].$loc.pos - 1,
11361
- length: f[0].$loc.length + 1
11514
+ pos: from[0].$loc.pos - 1,
11515
+ length: from[0].$loc.length + 1
11362
11516
  };
11363
- const children = [i, t, c, w, f];
11364
- if (!t)
11365
- return children;
11366
- return { type: "ImportDeclaration", ts: true, children };
11517
+ const children = [i, t, imports, w, from];
11518
+ return { type: "ImportDeclaration", ts: !!t, children, imports, from };
11367
11519
  });
11368
11520
  var ImportDeclaration$$ = [ImportDeclaration$0, ImportDeclaration$1, ImportDeclaration$2, ImportDeclaration$3, ImportDeclaration$4];
11369
11521
  function ImportDeclaration(ctx, state) {
@@ -11381,14 +11533,17 @@ var require_parser = __commonJS({
11381
11533
  if (rest) {
11382
11534
  return {
11383
11535
  type: "Declaration",
11384
- children: $0,
11385
- names: [...binding.names, ...rest[3].names]
11536
+ children: [binding, ...rest],
11537
+ names: [...binding.names, ...rest[3].names],
11538
+ binding,
11539
+ specifiers: rest[3].specifiers
11386
11540
  };
11387
11541
  }
11388
11542
  return {
11389
11543
  type: "Declaration",
11390
- children: $0,
11391
- names: binding.names
11544
+ children: [binding],
11545
+ names: binding.names,
11546
+ binding
11392
11547
  };
11393
11548
  });
11394
11549
  var ImportClause$1 = NameSpaceImport;
@@ -11398,11 +11553,14 @@ var require_parser = __commonJS({
11398
11553
  return $EVENT_C(ctx, state, "ImportClause", ImportClause$$);
11399
11554
  }
11400
11555
  var NameSpaceImport$0 = $TS($S(Star, ImportAsToken, __, ImportedBinding), function($skip, $loc, $0, $1, $2, $3, $4) {
11556
+ var star = $1;
11401
11557
  var binding = $4;
11402
11558
  return {
11403
11559
  type: "Declaration",
11404
11560
  children: $0,
11405
- names: binding.names
11561
+ names: binding.names,
11562
+ binding,
11563
+ star
11406
11564
  };
11407
11565
  });
11408
11566
  function NameSpaceImport(ctx, state) {
@@ -11414,17 +11572,32 @@ var require_parser = __commonJS({
11414
11572
  return {
11415
11573
  type: "Declaration",
11416
11574
  children: $0,
11417
- names
11575
+ names,
11576
+ specifiers
11418
11577
  };
11419
11578
  });
11420
11579
  function NamedImports(ctx, state) {
11421
11580
  return $EVENT(ctx, state, "NamedImports", NamedImports$0);
11422
11581
  }
11423
- var FromClause$0 = $S(From, __, ModuleSpecifier);
11582
+ var FromClause$0 = $TS($S(From, __, ModuleSpecifier), function($skip, $loc, $0, $1, $2, $3) {
11583
+ var module4 = $3;
11584
+ if (!Array.isArray(module4))
11585
+ return $0;
11586
+ return [$1, $2, ...module4];
11587
+ });
11424
11588
  function FromClause(ctx, state) {
11425
11589
  return $EVENT(ctx, state, "FromClause", FromClause$0);
11426
11590
  }
11427
- var ImportAssertion$0 = $S($E(_), $C($EXPECT($L117, 'ImportAssertion "with"'), $EXPECT($L118, 'ImportAssertion "assert"')), NonIdContinue, $E(_), ObjectLiteral);
11591
+ var ImportAssertion$0 = $TS($S($E(_), $C($EXPECT($L117, 'ImportAssertion "with"'), $EXPECT($L118, 'ImportAssertion "assert"')), NonIdContinue, $E(_), ObjectLiteral), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
11592
+ var keyword = $2;
11593
+ var object = $5;
11594
+ return {
11595
+ type: "ImportAssertion",
11596
+ keyword,
11597
+ object,
11598
+ children: $0
11599
+ };
11600
+ });
11428
11601
  function ImportAssertion(ctx, state) {
11429
11602
  return $EVENT(ctx, state, "ImportAssertion", ImportAssertion$0);
11430
11603
  }
@@ -11454,8 +11627,10 @@ var require_parser = __commonJS({
11454
11627
  return $EVENT_C(ctx, state, "TypeAndImportSpecifier", TypeAndImportSpecifier$$);
11455
11628
  }
11456
11629
  var ImportSpecifier$0 = $TS($S(__, ModuleExportName, ImportAsToken, __, ImportedBinding, ObjectPropertyDelimiter), function($skip, $loc, $0, $1, $2, $3, $4, $5, $6) {
11630
+ var source = $2;
11457
11631
  var binding = $5;
11458
11632
  return {
11633
+ source,
11459
11634
  binding,
11460
11635
  children: $0
11461
11636
  };
@@ -11463,6 +11638,7 @@ var require_parser = __commonJS({
11463
11638
  var ImportSpecifier$1 = $TS($S(__, ImportedBinding, ObjectPropertyDelimiter), function($skip, $loc, $0, $1, $2, $3) {
11464
11639
  var binding = $2;
11465
11640
  return {
11641
+ source: binding,
11466
11642
  binding,
11467
11643
  children: $0
11468
11644
  };
@@ -11654,9 +11830,18 @@ var require_parser = __commonJS({
11654
11830
  function ImplicitExportSpecifier(ctx, state) {
11655
11831
  return $EVENT(ctx, state, "ImplicitExportSpecifier", ImplicitExportSpecifier$0);
11656
11832
  }
11657
- var Declaration$0 = HoistableDeclaration;
11658
- var Declaration$1 = ClassDeclaration;
11659
- var Declaration$2 = $TV(LexicalDeclaration, function($skip, $loc, $0, $1) {
11833
+ var Declaration$0 = $TV(ImportDeclaration, function($skip, $loc, $0, $1) {
11834
+ var decl = $0;
11835
+ if (decl.ts || decl.module || !decl.imports || !decl.from)
11836
+ return $skip;
11837
+ const { imports } = decl;
11838
+ if (!imports.binding && !imports.specifiers)
11839
+ return $skip;
11840
+ return dynamizeImportDeclaration(decl);
11841
+ });
11842
+ var Declaration$1 = HoistableDeclaration;
11843
+ var Declaration$2 = ClassDeclaration;
11844
+ var Declaration$3 = $TV(LexicalDeclaration, function($skip, $loc, $0, $1) {
11660
11845
  var d = $0;
11661
11846
  if (d.thisAssignments?.length)
11662
11847
  return {
@@ -11670,11 +11855,11 @@ var require_parser = __commonJS({
11670
11855
  };
11671
11856
  return d;
11672
11857
  });
11673
- var Declaration$3 = TypeDeclaration;
11674
- var Declaration$4 = EnumDeclaration;
11675
- var Declaration$5 = OperatorDeclaration;
11676
- var Declaration$6 = UsingDeclaration;
11677
- var Declaration$$ = [Declaration$0, Declaration$1, Declaration$2, Declaration$3, Declaration$4, Declaration$5, Declaration$6];
11858
+ var Declaration$4 = TypeDeclaration;
11859
+ var Declaration$5 = EnumDeclaration;
11860
+ var Declaration$6 = OperatorDeclaration;
11861
+ var Declaration$7 = UsingDeclaration;
11862
+ var Declaration$$ = [Declaration$0, Declaration$1, Declaration$2, Declaration$3, Declaration$4, Declaration$5, Declaration$6, Declaration$7];
11678
11863
  function Declaration(ctx, state) {
11679
11864
  return $EVENT_C(ctx, state, "Declaration", Declaration$$);
11680
11865
  }