question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling `replaceWithText` on private static property with no spaces to be non-private leads to error

See original GitHub issue

Describe the bug

Version: 15.1.0

I have a class with a private static property that I would like to make public by renaming it. The weird thing is, that the issue only happens if the file is in minified form.

To Reproduce

import { Project, SyntaxKind } from "ts-morph";

// This works
const content1 = `
class Test{
    static #t = 23;
}`;

// This fails
const content2 = "class Test{static#t=23;}";

const project = new Project();
const file = project.createSourceFile("test.ts", content2);

const classNode = file.getFirstChildByKindOrThrow(SyntaxKind.ClassDeclaration);
const property = classNode.getFirstChildByKindOrThrow(SyntaxKind.PropertyDeclaration);
const privateIdentifier = property.getFirstChildByKindOrThrow(SyntaxKind.PrivateIdentifier);

// remove # from the beginning of the name
const publicName = privateIdentifier.getText().substring(1);
privateIdentifier.replaceWithText(publicName);

console.log(file.getText());

If I use the minified input (content2), then the following error is thrown:

Stack: Error: Error replacing tree! Perhaps a syntax error was inserted (Current: SyntaxList -- New: Identifier).

Expected behavior

It should work with the minified version as well.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dsherretcommented, Jun 17, 2022

Yeah, I started working on it, but it’s a decent amount of additional code. I’m going to close this one as “wont do” and recommend adding some conditional logic like the following to tell if a space should be added:

const content2 = "class Test{static#t=23;}";

const project = new Project();
const file = project.createSourceFile("test.ts", content2);

const classNode = file.getClassOrThrow("Test");
const property = classNode.getPropertyOrThrow("#t");
const privateIdentifier = property.getNameNode();

// remove # from the beginning of the name
let publicName = privateIdentifier.getText().substring(1);
if (isAlphaNumeric(file.getFullText().charCodeAt(privateIdentifier.getStart() - 1))) {
  // add a space if the previous character is alphanumeric
  publicName = " " + publicName;
}
privateIdentifier.replaceWithText(publicName);

console.log(file.getText());

function isAlphaNumeric(charCode: number) {
  return charCode > 47 && charCode < 58 // 0-9
    || charCode > 64 && charCode < 91 // A-Z
    || charCode > 96 && charCode < 123; // a-z
}
0reactions
Wyctuscommented, Jun 17, 2022

This is clever, thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Private property changing it's value, without it's setter being ...
The problem is the fact that this variable is static and hence when one user logs in and the value is set, another...
Read more >
Strict mode static property is undefined (no compiler error)
For the type check on the getProps() side, this is somewhat a working-as-intended: the variable is not declared optional, so they "cannot" be ......
Read more >
Static properties and methods - The Modern JavaScript Tutorial
staticMethod () call is the class constructor User itself (the “object before dot” rule). Usually, static methods are used to implement functions ...
Read more >
java.util Class Properties
A property list that contains default values for any keys not found in this property list. private static char[], hexDigit. A table of...
Read more >
MapBasic v2021 User Guide - Precisely Help
Trademarked names are used editorially, to the benefit of the trademark owner, with no intent to infringe on the trademark. 4. MapBasic 2021....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found