Ts-morph: Rename variable which used for build object

Created on 27 Sep 2018  路  8Comments  路  Source: dsherret/ts-morph

Describe the bug
Version: latest

Trying to rename variable which used to build object with shortand property acces
To Reproduce

const a = 2;
const b = {a};
// after rename a -> a_renamed
const a_renamed = 2;
const b = {a_renamed};
Expected behavior

// after rename a -> a_renamed
const a_renamed = 2;
const b = {a: a_renamed};

bug compiler api

All 8 comments

That makes sense, but it's what the TypeScript language service does. Same thing happens in my editor. See these two bugs: https://github.com/Microsoft/TypeScript/issues/15787 https://github.com/Microsoft/TypeScript/issues/16435

That said, I believe I could do some additional checks on the identifier and if it's a shorthand property access expression I can convert it to a property assignment.

Can I try to solve this problem?

I'm wondering if we should fix this or just wait for it to be fixed in the compiler api because it would impact the performance of findReferences since we'd have to traverse the tree for every result (it's already mistakenly doing that though and will be fixed in #435).

How do you feel about solving this problem in the compiler api rather than ts-simple-ast? That way more people will get the benefit of the fix.

This may be fixed in 3.4.1. I'll look into it again soon.

Great!

@maximelkin sorry, I just looked into this and it doesn't look like they actually fixed it :(

I've comment on the original TypeScript issue about it not being fixed in the language service.

And nevermind. So there's a new providePrefixAndSuffixTextForRename parameter on findRenameLocations that I have to use:

findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean, providePrefixAndSuffixTextForRename?: boolean): ReadonlyArray<RenameLocation> | undefined;

I'll fix this now by implementing #638.

@maximelkin so, one annoyance with this new providePrefixAndSuffixTextForRename parameter is that it also adds aliases to import and export specifiers. I would have hoped that one option would control this for shorthand property assignments -> property assignments and another for adding aliases to import/export specifiers.

This is going to be off (false) by default for these reasons:

  1. It's off by default in the compiler API.
  2. It's only supported in TS 3.4+ and this library currently supports down to 3.0.
  3. No way to disable aliases being added in import/export specifiers.

So overall, I wish they had made the control here more fine grained.

Enabling

In 3.0, to enable this, you will be able to specify that at a global level through manipulation settings:

const project = new Project({
    manipulationSettings: {
        usePrefixAndSuffixTextForRename: true
    }
});
// or
project.manipulationSettings.set({ usePrefixAndSuffixTextForRename: true });

Or by specifying it in the options when renaming:

identifier.rename("NewName", {
    usePrefixAndSuffixText: true
});
Was this page helpful?
0 / 5 - 0 ratings