wrec 0.24.0 → 0.24.1

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 (2) hide show
  1. package/package.json +1 -1
  2. package/scripts/used-by.js +45 -12
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "wrec",
3
3
  "description": "a small library that greatly simplifies building web components",
4
4
  "author": "R. Mark Volkmann",
5
- "version": "0.24.0",
5
+ "version": "0.24.1",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",
@@ -97,8 +97,9 @@ function hasStaticModifier(node) {
97
97
  );
98
98
  }
99
99
 
100
- function getWrecNames(sourceFile) {
100
+ function getWrecImportInfo(sourceFile) {
101
101
  const names = new Set(['Wrec']);
102
+ let quote = "'";
102
103
 
103
104
  for (const statement of sourceFile.statements) {
104
105
  if (
@@ -122,11 +123,18 @@ function getWrecNames(sourceFile) {
122
123
 
123
124
  for (const element of namedBindings.elements) {
124
125
  const importedName = element.propertyName?.text ?? element.name.text;
125
- if (importedName === 'Wrec') names.add(element.name.text);
126
+ if (importedName === 'Wrec') {
127
+ names.add(element.name.text);
128
+
129
+ const moduleText = statement.moduleSpecifier.getText(sourceFile);
130
+ if (moduleText.startsWith('"') || moduleText.startsWith("'")) {
131
+ quote = moduleText[0];
132
+ }
133
+ }
126
134
  }
127
135
  }
128
136
 
129
- return names;
137
+ return {names, quote};
130
138
  }
131
139
 
132
140
  function extendsWrec(node, wrecNames) {
@@ -389,8 +397,8 @@ function getMethodUsages(classNode, propertyNames) {
389
397
  return propToMethods;
390
398
  }
391
399
 
392
- function createUsedByProperty(methodNames) {
393
- return `usedBy: [${methodNames.map(name => `'${name}'`).join(', ')}]`;
400
+ function createUsedByProperty(methodNames, quote) {
401
+ return `usedBy: [${methodNames.map(name => `${quote}${name}${quote}`).join(', ')}]`;
394
402
  }
395
403
 
396
404
  function getIndent(text, pos) {
@@ -399,7 +407,7 @@ function getIndent(text, pos) {
399
407
  return match ? match[0] : '';
400
408
  }
401
409
 
402
- function buildConfigText(sourceFile, member, methodNames) {
410
+ function buildConfigText(sourceFile, member, methodNames, quote) {
403
411
  const {text} = sourceFile;
404
412
  const configObject = member.initializer;
405
413
  const existingMembers = configObject.properties.filter(
@@ -413,7 +421,7 @@ function buildConfigText(sourceFile, member, methodNames) {
413
421
  text.slice(property.getStart(sourceFile), property.end).trim()
414
422
  );
415
423
  if (methodNames.length > 0)
416
- existingTexts.push(createUsedByProperty(methodNames));
424
+ existingTexts.push(createUsedByProperty(methodNames, quote));
417
425
 
418
426
  const original = text.slice(
419
427
  configObject.getStart(sourceFile),
@@ -432,10 +440,12 @@ function buildConfigText(sourceFile, member, methodNames) {
432
440
 
433
441
  function transformSourceFile(sourceFile) {
434
442
  const edits = [];
435
- const wrecNames = getWrecNames(sourceFile);
443
+ const {names: wrecNames, quote} = getWrecImportInfo(sourceFile);
444
+ let foundWrecSubclass = false;
436
445
 
437
446
  for (const node of sourceFile.statements) {
438
447
  if (!ts.isClassDeclaration(node) || !extendsWrec(node, wrecNames)) continue;
448
+ foundWrecSubclass = true;
439
449
 
440
450
  let propertiesObject = null;
441
451
  for (const member of node.members) {
@@ -484,7 +494,7 @@ function transformSourceFile(sourceFile) {
484
494
  const needsUsedBy = methodNames.length > 0;
485
495
  if (!hadUsedBy && !needsUsedBy) continue;
486
496
 
487
- const nextText = buildConfigText(sourceFile, member, methodNames);
497
+ const nextText = buildConfigText(sourceFile, member, methodNames, quote);
488
498
  const currentText = sourceFile.text.slice(
489
499
  configObject.getStart(sourceFile),
490
500
  configObject.end
@@ -501,8 +511,22 @@ function transformSourceFile(sourceFile) {
501
511
  }
502
512
  }
503
513
 
514
+ if (!foundWrecSubclass) {
515
+ return {
516
+ changed: false,
517
+ edits: [],
518
+ foundWrecSubclass: false,
519
+ text: sourceFile.text
520
+ };
521
+ }
522
+
504
523
  if (edits.length === 0) {
505
- return {changed: false, edits: [], text: sourceFile.text};
524
+ return {
525
+ changed: false,
526
+ edits: [],
527
+ foundWrecSubclass: true,
528
+ text: sourceFile.text
529
+ };
506
530
  }
507
531
 
508
532
  let nextSource = sourceFile.text;
@@ -512,7 +536,7 @@ function transformSourceFile(sourceFile) {
512
536
  nextSource.slice(0, edit.start) + edit.text + nextSource.slice(edit.end);
513
537
  }
514
538
 
515
- return {changed: true, edits, text: nextSource};
539
+ return {changed: true, edits, foundWrecSubclass: true, text: nextSource};
516
540
  }
517
541
 
518
542
  let changedCount = 0;
@@ -528,7 +552,16 @@ for (const file of files) {
528
552
  scriptKind
529
553
  );
530
554
 
531
- const {changed, edits, text: nextText} = transformSourceFile(sourceFile);
555
+ const {
556
+ changed,
557
+ edits,
558
+ foundWrecSubclass,
559
+ text: nextText
560
+ } = transformSourceFile(sourceFile);
561
+ if (!foundWrecSubclass) {
562
+ console.error('No class extending Wrec was found.');
563
+ process.exit(1);
564
+ }
532
565
  if (!changed) continue;
533
566
 
534
567
  changedCount++;