@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.mjs CHANGED
@@ -1702,7 +1702,11 @@ function patternAsValue(pattern) {
1702
1702
  }
1703
1703
  case "Identifier":
1704
1704
  case "BindingProperty": {
1705
- const children = [pattern.name, pattern.delim];
1705
+ const children = [
1706
+ // { name: value } = ... declares value, not name
1707
+ pattern.value ?? pattern.name,
1708
+ pattern.delim
1709
+ ];
1706
1710
  if (isWhitespaceOrEmpty(pattern.children[0])) {
1707
1711
  children.unshift(pattern.children[0]);
1708
1712
  }
@@ -2310,6 +2314,111 @@ function processDeclarationConditionStatement(s, getRef) {
2310
2314
  }
2311
2315
  }
2312
2316
  }
2317
+ function dynamizeFromClause(from) {
2318
+ from = from.slice(1);
2319
+ from = insertTrimmingSpace(from, "");
2320
+ if (from.at(-1)?.type === "ImportAssertion") {
2321
+ const assert2 = from.pop();
2322
+ from.push(", {", assert2.keyword, ":", assert2.object, "}");
2323
+ }
2324
+ return ["(", ...from, ")"];
2325
+ }
2326
+ function dynamizeImportDeclaration(decl) {
2327
+ const { imports } = decl;
2328
+ let { star, binding, specifiers } = imports;
2329
+ const justDefault = binding && !specifiers && !star;
2330
+ const pattern = (() => {
2331
+ {
2332
+ if (binding) {
2333
+ if (specifiers) {
2334
+ return makeRef();
2335
+ } else {
2336
+ return binding;
2337
+ }
2338
+ } else {
2339
+ return convertNamedImportsToObject(imports, true);
2340
+ }
2341
+ }
2342
+ })();
2343
+ const c = "const";
2344
+ const initializer = [
2345
+ " = ",
2346
+ justDefault ? "(" : void 0,
2347
+ { type: "Await", children: ["await"] },
2348
+ " ",
2349
+ decl.children[0],
2350
+ // import
2351
+ dynamizeFromClause(decl.from),
2352
+ justDefault ? ").default" : void 0
2353
+ ];
2354
+ const bindings = [{
2355
+ type: "Binding",
2356
+ names: pattern.names,
2357
+ pattern,
2358
+ initializer,
2359
+ children: [pattern, initializer]
2360
+ }];
2361
+ if (binding && specifiers) {
2362
+ const pattern2 = binding;
2363
+ const initializer2 = [
2364
+ " = ",
2365
+ pattern,
2366
+ ".default"
2367
+ ];
2368
+ bindings.push({
2369
+ type: "Binding",
2370
+ names: binding.names,
2371
+ pattern: pattern2,
2372
+ initializer: initializer2,
2373
+ children: [pattern2, initializer2]
2374
+ });
2375
+ const pattern3 = convertNamedImportsToObject(imports.children.at(-1), true);
2376
+ const initializer3 = [
2377
+ " = ",
2378
+ pattern
2379
+ ];
2380
+ bindings.push({
2381
+ type: "Binding",
2382
+ names: specifiers.names,
2383
+ pattern: pattern3,
2384
+ initializer: initializer3,
2385
+ children: [pattern3, initializer3]
2386
+ });
2387
+ }
2388
+ return {
2389
+ type: "Declaration",
2390
+ names: imports.names,
2391
+ bindings,
2392
+ decl: c,
2393
+ children: [
2394
+ c,
2395
+ " ",
2396
+ bindings.flatMap((binding2, i) => i > 0 ? [", ", binding2] : [binding2])
2397
+ ]
2398
+ };
2399
+ }
2400
+ function dynamizeImportDeclarationExpression($0) {
2401
+ const [imp, ws1, named, ws2, from] = $0;
2402
+ const object = convertNamedImportsToObject(named);
2403
+ const dot = ".";
2404
+ return processCallMemberExpression({
2405
+ type: "CallExpression",
2406
+ children: [
2407
+ { type: "Await", children: "await" },
2408
+ " ",
2409
+ imp,
2410
+ insertTrimmingSpace(ws2, ""),
2411
+ dynamizeFromClause(from),
2412
+ {
2413
+ type: "PropertyGlob",
2414
+ dot,
2415
+ object,
2416
+ children: [ws1, dot, object],
2417
+ reversed: true
2418
+ }
2419
+ ]
2420
+ });
2421
+ }
2313
2422
  var init_declaration = __esm({
2314
2423
  "source/parser/declaration.civet"() {
2315
2424
  "use strict";
@@ -2319,6 +2428,7 @@ var init_declaration = __esm({
2319
2428
  init_util();
2320
2429
  init_function();
2321
2430
  init_binding();
2431
+ init_lib();
2322
2432
  }
2323
2433
  });
2324
2434
 
@@ -3135,10 +3245,13 @@ __export(lib_exports, {
3135
3245
  adjustIndexAccess: () => adjustIndexAccess,
3136
3246
  attachPostfixStatementAsExpression: () => attachPostfixStatementAsExpression,
3137
3247
  blockWithPrefix: () => blockWithPrefix,
3248
+ convertNamedImportsToObject: () => convertNamedImportsToObject,
3138
3249
  convertObjectToJSXAttributes: () => convertObjectToJSXAttributes,
3139
3250
  dedentBlockString: () => dedentBlockString,
3140
3251
  dedentBlockSubstitutions: () => dedentBlockSubstitutions,
3141
3252
  deepCopy: () => deepCopy,
3253
+ dynamizeImportDeclaration: () => dynamizeImportDeclaration,
3254
+ dynamizeImportDeclarationExpression: () => dynamizeImportDeclarationExpression,
3142
3255
  expressionizeTypeIf: () => expressionizeTypeIf,
3143
3256
  forRange: () => forRange,
3144
3257
  gatherBindingCode: () => gatherBindingCode,
@@ -3410,9 +3523,13 @@ function processCallMemberExpression(node) {
3410
3523
  throw new Error(`Glob pattern must have call or member expression value, found ${JSON.stringify(part.value)}`);
3411
3524
  }
3412
3525
  let suppressPrefix = false;
3413
- let value = part.value ?? part.name;
3526
+ let name = part.name;
3527
+ let value = part.value ?? name;
3414
3528
  const wValue = getTrimmingSpace(part.value);
3415
3529
  [value, suppressPrefix] = handleThisPrivateShorthands(value);
3530
+ if (glob.reversed) {
3531
+ [name, value] = [value, name];
3532
+ }
3416
3533
  if (!suppressPrefix) {
3417
3534
  value = prefix.concat(insertTrimmingSpace(value, ""));
3418
3535
  }
@@ -3430,13 +3547,13 @@ function processCallMemberExpression(node) {
3430
3547
  } else {
3431
3548
  parts.push({
3432
3549
  type: part.type === "Identifier" ? "Property" : part.type,
3433
- name: part.name,
3550
+ name,
3434
3551
  value,
3435
3552
  delim: part.delim,
3436
3553
  names: part.names,
3437
3554
  children: [
3438
3555
  isWhitespaceOrEmpty(part.children[0]) && part.children[0],
3439
- part.name,
3556
+ name,
3440
3557
  isWhitespaceOrEmpty(part.children[2]) && part.children[2],
3441
3558
  part.children[3]?.token === ":" ? part.children[3] : ":",
3442
3559
  value,
@@ -3581,6 +3698,35 @@ function convertMethodToFunction(method) {
3581
3698
  block
3582
3699
  };
3583
3700
  }
3701
+ function convertNamedImportsToObject(node, pattern) {
3702
+ const properties = node.specifiers.map((specifier) => {
3703
+ if (specifier.ts) {
3704
+ return { type: "Error", message: "cannot use `type` in dynamic import" };
3705
+ } else {
3706
+ const { source, binding } = specifier;
3707
+ const delim = specifier.children.at(-1);
3708
+ return {
3709
+ type: pattern ? "BindingProperty" : "Property",
3710
+ name: source,
3711
+ value: !(source === binding) ? binding : void 0,
3712
+ delim,
3713
+ children: source === binding ? [source, delim] : [source, ":", binding, delim]
3714
+ };
3715
+ }
3716
+ });
3717
+ return {
3718
+ type: pattern ? "ObjectBindingPattern" : "ObjectExpression",
3719
+ names: node.names,
3720
+ properties,
3721
+ children: [
3722
+ node.children[0],
3723
+ // {
3724
+ properties,
3725
+ node.children.at(-1)
3726
+ // }
3727
+ ]
3728
+ };
3729
+ }
3584
3730
  function convertObjectToJSXAttributes(obj) {
3585
3731
  const { properties } = obj;
3586
3732
  const parts = [];
@@ -7056,14 +7202,17 @@ var require_parser = __commonJS({
7056
7202
  children: [$1, ...$2, ...rest.flat()]
7057
7203
  });
7058
7204
  });
7059
- var CallExpression$1 = $TS($S($EXPECT($L20, 'CallExpression "import"'), ArgumentsWithTrailingMemberExpressions, $Q(CallExpressionRest)), function($skip, $loc, $0, $1, $2, $3) {
7205
+ var CallExpression$1 = $TS($S(Import, _, NamedImports, __, FromClause), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
7206
+ return dynamizeImportDeclarationExpression($0);
7207
+ });
7208
+ var CallExpression$2 = $TS($S($EXPECT($L20, 'CallExpression "import"'), ArgumentsWithTrailingMemberExpressions, $Q(CallExpressionRest)), function($skip, $loc, $0, $1, $2, $3) {
7060
7209
  var rest = $3;
7061
7210
  return processCallMemberExpression({
7062
7211
  type: "CallExpression",
7063
7212
  children: [$1, ...$2, ...rest.flat()]
7064
7213
  });
7065
7214
  });
7066
- var CallExpression$2 = $TS($S(MemberExpression, AllowedTrailingMemberExpressions, $Q(CallExpressionRest)), function($skip, $loc, $0, $1, $2, $3) {
7215
+ var CallExpression$3 = $TS($S(MemberExpression, AllowedTrailingMemberExpressions, $Q(CallExpressionRest)), function($skip, $loc, $0, $1, $2, $3) {
7067
7216
  var member = $1;
7068
7217
  var trailing = $2;
7069
7218
  var rest = $3;
@@ -7076,7 +7225,7 @@ var require_parser = __commonJS({
7076
7225
  }
7077
7226
  return member;
7078
7227
  });
7079
- var CallExpression$$ = [CallExpression$0, CallExpression$1, CallExpression$2];
7228
+ var CallExpression$$ = [CallExpression$0, CallExpression$1, CallExpression$2, CallExpression$3];
7080
7229
  function CallExpression(ctx, state) {
7081
7230
  return $EVENT_C(ctx, state, "CallExpression", CallExpression$$);
7082
7231
  }
@@ -11340,28 +11489,31 @@ var require_parser = __commonJS({
11340
11489
  };
11341
11490
  });
11342
11491
  var ImportDeclaration$1 = $T($S(Import, __, TypeKeyword, __, ImportClause, __, FromClause), function(value) {
11343
- return { "type": "ImportDeclaration", "ts": true, "children": value };
11492
+ var imports = value[4];
11493
+ var from = value[6];
11494
+ return { "type": "ImportDeclaration", "ts": true, "children": value, "imports": imports, "from": from };
11344
11495
  });
11345
11496
  var ImportDeclaration$2 = $T($S(Import, __, ImportClause, __, FromClause), function(value) {
11346
- return { "type": "ImportDeclaration", "children": value };
11497
+ var imports = value[2];
11498
+ var from = value[4];
11499
+ return { "type": "ImportDeclaration", "children": value, "imports": imports, "from": from };
11347
11500
  });
11348
11501
  var ImportDeclaration$3 = $T($S(Import, __, ModuleSpecifier), function(value) {
11349
- return { "type": "ImportDeclaration", "children": value };
11502
+ var module3 = value[2];
11503
+ return { "type": "ImportDeclaration", "children": value, "module": module3 };
11350
11504
  });
11351
11505
  var ImportDeclaration$4 = $TS($S(ImpliedImport, $E($S(TypeKeyword, __)), ImportClause, __, FromClause), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
11352
11506
  var i = $1;
11353
11507
  var t = $2;
11354
- var c = $3;
11508
+ var imports = $3;
11355
11509
  var w = $4;
11356
- var f = $5;
11510
+ var from = $5;
11357
11511
  i.$loc = {
11358
- pos: f[0].$loc.pos - 1,
11359
- length: f[0].$loc.length + 1
11512
+ pos: from[0].$loc.pos - 1,
11513
+ length: from[0].$loc.length + 1
11360
11514
  };
11361
- const children = [i, t, c, w, f];
11362
- if (!t)
11363
- return children;
11364
- return { type: "ImportDeclaration", ts: true, children };
11515
+ const children = [i, t, imports, w, from];
11516
+ return { type: "ImportDeclaration", ts: !!t, children, imports, from };
11365
11517
  });
11366
11518
  var ImportDeclaration$$ = [ImportDeclaration$0, ImportDeclaration$1, ImportDeclaration$2, ImportDeclaration$3, ImportDeclaration$4];
11367
11519
  function ImportDeclaration(ctx, state) {
@@ -11379,14 +11531,17 @@ var require_parser = __commonJS({
11379
11531
  if (rest) {
11380
11532
  return {
11381
11533
  type: "Declaration",
11382
- children: $0,
11383
- names: [...binding.names, ...rest[3].names]
11534
+ children: [binding, ...rest],
11535
+ names: [...binding.names, ...rest[3].names],
11536
+ binding,
11537
+ specifiers: rest[3].specifiers
11384
11538
  };
11385
11539
  }
11386
11540
  return {
11387
11541
  type: "Declaration",
11388
- children: $0,
11389
- names: binding.names
11542
+ children: [binding],
11543
+ names: binding.names,
11544
+ binding
11390
11545
  };
11391
11546
  });
11392
11547
  var ImportClause$1 = NameSpaceImport;
@@ -11396,11 +11551,14 @@ var require_parser = __commonJS({
11396
11551
  return $EVENT_C(ctx, state, "ImportClause", ImportClause$$);
11397
11552
  }
11398
11553
  var NameSpaceImport$0 = $TS($S(Star, ImportAsToken, __, ImportedBinding), function($skip, $loc, $0, $1, $2, $3, $4) {
11554
+ var star = $1;
11399
11555
  var binding = $4;
11400
11556
  return {
11401
11557
  type: "Declaration",
11402
11558
  children: $0,
11403
- names: binding.names
11559
+ names: binding.names,
11560
+ binding,
11561
+ star
11404
11562
  };
11405
11563
  });
11406
11564
  function NameSpaceImport(ctx, state) {
@@ -11412,17 +11570,32 @@ var require_parser = __commonJS({
11412
11570
  return {
11413
11571
  type: "Declaration",
11414
11572
  children: $0,
11415
- names
11573
+ names,
11574
+ specifiers
11416
11575
  };
11417
11576
  });
11418
11577
  function NamedImports(ctx, state) {
11419
11578
  return $EVENT(ctx, state, "NamedImports", NamedImports$0);
11420
11579
  }
11421
- var FromClause$0 = $S(From, __, ModuleSpecifier);
11580
+ var FromClause$0 = $TS($S(From, __, ModuleSpecifier), function($skip, $loc, $0, $1, $2, $3) {
11581
+ var module3 = $3;
11582
+ if (!Array.isArray(module3))
11583
+ return $0;
11584
+ return [$1, $2, ...module3];
11585
+ });
11422
11586
  function FromClause(ctx, state) {
11423
11587
  return $EVENT(ctx, state, "FromClause", FromClause$0);
11424
11588
  }
11425
- var ImportAssertion$0 = $S($E(_), $C($EXPECT($L117, 'ImportAssertion "with"'), $EXPECT($L118, 'ImportAssertion "assert"')), NonIdContinue, $E(_), ObjectLiteral);
11589
+ 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) {
11590
+ var keyword = $2;
11591
+ var object = $5;
11592
+ return {
11593
+ type: "ImportAssertion",
11594
+ keyword,
11595
+ object,
11596
+ children: $0
11597
+ };
11598
+ });
11426
11599
  function ImportAssertion(ctx, state) {
11427
11600
  return $EVENT(ctx, state, "ImportAssertion", ImportAssertion$0);
11428
11601
  }
@@ -11452,8 +11625,10 @@ var require_parser = __commonJS({
11452
11625
  return $EVENT_C(ctx, state, "TypeAndImportSpecifier", TypeAndImportSpecifier$$);
11453
11626
  }
11454
11627
  var ImportSpecifier$0 = $TS($S(__, ModuleExportName, ImportAsToken, __, ImportedBinding, ObjectPropertyDelimiter), function($skip, $loc, $0, $1, $2, $3, $4, $5, $6) {
11628
+ var source = $2;
11455
11629
  var binding = $5;
11456
11630
  return {
11631
+ source,
11457
11632
  binding,
11458
11633
  children: $0
11459
11634
  };
@@ -11461,6 +11636,7 @@ var require_parser = __commonJS({
11461
11636
  var ImportSpecifier$1 = $TS($S(__, ImportedBinding, ObjectPropertyDelimiter), function($skip, $loc, $0, $1, $2, $3) {
11462
11637
  var binding = $2;
11463
11638
  return {
11639
+ source: binding,
11464
11640
  binding,
11465
11641
  children: $0
11466
11642
  };
@@ -11652,9 +11828,18 @@ var require_parser = __commonJS({
11652
11828
  function ImplicitExportSpecifier(ctx, state) {
11653
11829
  return $EVENT(ctx, state, "ImplicitExportSpecifier", ImplicitExportSpecifier$0);
11654
11830
  }
11655
- var Declaration$0 = HoistableDeclaration;
11656
- var Declaration$1 = ClassDeclaration;
11657
- var Declaration$2 = $TV(LexicalDeclaration, function($skip, $loc, $0, $1) {
11831
+ var Declaration$0 = $TV(ImportDeclaration, function($skip, $loc, $0, $1) {
11832
+ var decl = $0;
11833
+ if (decl.ts || decl.module || !decl.imports || !decl.from)
11834
+ return $skip;
11835
+ const { imports } = decl;
11836
+ if (!imports.binding && !imports.specifiers)
11837
+ return $skip;
11838
+ return dynamizeImportDeclaration(decl);
11839
+ });
11840
+ var Declaration$1 = HoistableDeclaration;
11841
+ var Declaration$2 = ClassDeclaration;
11842
+ var Declaration$3 = $TV(LexicalDeclaration, function($skip, $loc, $0, $1) {
11658
11843
  var d = $0;
11659
11844
  if (d.thisAssignments?.length)
11660
11845
  return {
@@ -11668,11 +11853,11 @@ var require_parser = __commonJS({
11668
11853
  };
11669
11854
  return d;
11670
11855
  });
11671
- var Declaration$3 = TypeDeclaration;
11672
- var Declaration$4 = EnumDeclaration;
11673
- var Declaration$5 = OperatorDeclaration;
11674
- var Declaration$6 = UsingDeclaration;
11675
- var Declaration$$ = [Declaration$0, Declaration$1, Declaration$2, Declaration$3, Declaration$4, Declaration$5, Declaration$6];
11856
+ var Declaration$4 = TypeDeclaration;
11857
+ var Declaration$5 = EnumDeclaration;
11858
+ var Declaration$6 = OperatorDeclaration;
11859
+ var Declaration$7 = UsingDeclaration;
11860
+ var Declaration$$ = [Declaration$0, Declaration$1, Declaration$2, Declaration$3, Declaration$4, Declaration$5, Declaration$6, Declaration$7];
11676
11861
  function Declaration(ctx, state) {
11677
11862
  return $EVENT_C(ctx, state, "Declaration", Declaration$$);
11678
11863
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@danielx/civet",
3
3
  "type": "commonjs",
4
- "version": "0.6.87",
4
+ "version": "0.6.88",
5
5
  "description": "CoffeeScript style syntax for TypeScript",
6
6
  "main": "dist/main.js",
7
7
  "module": "dist/main.mjs",
@@ -75,7 +75,7 @@
75
75
  "@prettier/sync": "^0.3.0",
76
76
  "@types/assert": "^1.5.6",
77
77
  "@types/mocha": "^9.1.1",
78
- "@types/node": "^20.5.1",
78
+ "@types/node": "^20.12.2",
79
79
  "c8": "^7.12.0",
80
80
  "esbuild": "0.20.0",
81
81
  "marked": "^4.2.4",
package/register.js CHANGED
@@ -1,8 +1,16 @@
1
1
  /**
2
- @file Civet CJS registration
2
+ @file Civet CJS and ESM registration
3
3
 
4
- `require`ing this file will register the `.civet` extension with
5
- Node.js's `require`.
4
+ `import`ing this file in Node 20.6.0+ will register the `.civet` extension
5
+ for both ESM `import`s and CJS `require`s.
6
+
7
+ @example
8
+ ```bash
9
+ node --import @danielx/civet/register source.civet
10
+ ```
11
+
12
+ On older Node, `require`ing this file will register the `.civet` extension
13
+ for CJS `require`s.
6
14
 
7
15
  @example
8
16
  ```bash
@@ -10,17 +18,25 @@ node -r @danielx/civet/register source.civet
10
18
  ```
11
19
  */
12
20
 
21
+ try {
22
+ const { register } = require('node:module');
23
+ const { pathToFileURL } = require('node:url');
24
+
25
+ register('./dist/esm.mjs', pathToFileURL(__filename));
26
+ } catch (e) {}
27
+
28
+ // CJS registration
13
29
  if (require.extensions) {
14
30
  const fs = require("fs");
15
31
  const { compile } = require("./");
16
32
 
17
33
  require.extensions[".civet"] = function (module, filename) {
18
34
  const js = compile(fs.readFileSync(filename, 'utf8'), {
35
+ filename,
19
36
  js: true,
20
37
  inlineMap: true,
21
38
  });
22
39
  module._compile(js, filename);
23
- return;
24
40
  };
25
41
 
26
42
  try {