Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add opt-in user preference for prefix and suffix text on renames #29314

Merged
merged 21 commits into from
Jan 16, 2019

Conversation

uniqueiniquity
Copy link
Contributor

Fixes #28679
This PR adds a user preference which, if not set, returns the rename behavior on shorthand properties and import/export specifiers to its state in 3.1.

@@ -1231,7 +1232,8 @@ namespace ts.server {
this.getDefaultProject(args),
{ fileName: args.file, pos: position },
!!args.findInStrings,
!!args.findInComments
!!args.findInComments,
this.getHostPreferences().usePrefixAndSuffixTextForRename || false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.getHostPreferences().usePrefixAndSuffixTextForRename || false [](start = 16, length = 66)

Since there may be multiple preferences affecting rename behavior (e.g. #28677), would it make more sense to pass in the entire preferences object (e.g. as in getEditsForFileRename)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to passing entire preferences object.
However I only passed it as deep as this call; any deeper would require an API change, so I didn't want to do that unless absolutely necessary.

src/services/utilities.ts Outdated Show resolved Hide resolved
src/services/utilities.ts Outdated Show resolved Hide resolved
src/compiler/types.ts Outdated Show resolved Hide resolved
@sandersn
Copy link
Member

sandersn commented Jan 9, 2019

Do you have a plan for when and how to remove this code? It's pretty ugly and I think it will only be needed for another 6 months or so. (Maybe more, depending on how long we ship new versions of TS to old versions of VS).

@amcasey
Copy link
Member

amcasey commented Jan 9, 2019

@sandersn Don't we have to keep this until the newest version of VS without the opt-in is deprecated? I'm guessing that will be longer than 6 months.

@sandersn
Copy link
Member

sandersn commented Jan 9, 2019

@amcasey Per our in-person discussion, we'll want to ship new TS servers to VS versions that shipped at least 2 years ago. (Plus we haven't done the work to make VS work with the new API anyway.)

Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I take it you figured out how to make this work without changing getTypeAtLocation at all? If so, then sounds good. :)

@uniqueiniquity
Copy link
Contributor Author

@sandersn yep, it was a red herring.

@uniqueiniquity
Copy link
Contributor Author

@amcasey Can you take another look? I attempted to consolidate conditions like we discussed.

@amcasey
Copy link
Member

amcasey commented Jan 15, 2019

        if (!(state.options.isForRename && (name.escapedText === InternalSymbolName.Default))) {

This is unaffected by the user preference?


Refers to: src/services/findAllReferences.ts:1092 in ad75ce7. [](commit_id = ad75ce7, deletion_comment = False)

@amcasey
Copy link
Member

amcasey commented Jan 15, 2019

        if (addReferencesHere && !state.options.isForRename && state.markSeenReExportRHS(name)) {

This is unaffected by the user preference?


Refers to: src/services/findAllReferences.ts:1103 in ad75ce7. [](commit_id = ad75ce7, deletion_comment = False)

Copy link
Member

@amcasey amcasey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still a few questions but generally LGTM.

@weswigham
Copy link
Member

Does this being a config setting mean it'll count as fixing #29238?

@amcasey
Copy link
Member

amcasey commented Jan 15, 2019

@weswigham It means editors will have the option of exposing that as a setting but, arguably, it won't be fixed until they do.

@weswigham
Copy link
Member

Since we've had a number of asks for #29238, we should try to make sure it's exposed as a setting in VS and VSCode, at least.

@uniqueiniquity
Copy link
Contributor Author

@weswigham we can certainly plan to make it optional in VS. AFAIK @mjbvz has already made it the default in VS Code

@uniqueiniquity
Copy link
Contributor Author

@amcasey Correct on your first comment, in no case do we want to rename the identifier default there. Also correct on your second comment, that behavior should only happen during a non-rename operation.

@uniqueiniquity uniqueiniquity merged commit 5fc8f1d into microsoft:master Jan 16, 2019
@uniqueiniquity uniqueiniquity mentioned this pull request Jan 17, 2019
uniqueiniquity added a commit that referenced this pull request Jan 17, 2019
Porting #29314 to release-3.3
errendir added a commit to errendir/TypeScript that referenced this pull request Jan 20, 2019
* origin/master: (64 commits)
  Fix resolution of properties from prototype assignment in JS (microsoft#29302)
  Include all flow nodes made within `try` blocks as antecedents for `catch` or `finally` blocks (microsoft#29466)
  Don't treat interfaces as implementations
  Make the relationship between partial mapped types and the empty object not apply for subtype relationship (microsoft#29384)
  Add missing arity check on second inference pass (microsoft#29386)
  renames
  add missing type annotation
  PR feedback
  Illustrate a case that isn't handled correctly
  Add fourslash tests
  Consider JSX namespace imports when moving statements between files
  Fix gulp builds not building some targets
  Update user baselines (microsoft#29444)
  Add opt-in user preference for prefix and suffix text on renames (microsoft#29314)
  Fake up value declaration for synthetic jsx children symbol so they get excess property checked (microsoft#29359)
  Add regression test. (microsoft#29433)
  Elaborate jsx children elementwise (microsoft#29264)
  Add regression test
  PR feedback
  Fix trailing whitespace
  ...
errendir added a commit to errendir/TypeScript that referenced this pull request Jan 20, 2019
* origin/master: (64 commits)
  Fix resolution of properties from prototype assignment in JS (microsoft#29302)
  Include all flow nodes made within `try` blocks as antecedents for `catch` or `finally` blocks (microsoft#29466)
  Don't treat interfaces as implementations
  Make the relationship between partial mapped types and the empty object not apply for subtype relationship (microsoft#29384)
  Add missing arity check on second inference pass (microsoft#29386)
  renames
  add missing type annotation
  PR feedback
  Illustrate a case that isn't handled correctly
  Add fourslash tests
  Consider JSX namespace imports when moving statements between files
  Fix gulp builds not building some targets
  Update user baselines (microsoft#29444)
  Add opt-in user preference for prefix and suffix text on renames (microsoft#29314)
  Fake up value declaration for synthetic jsx children symbol so they get excess property checked (microsoft#29359)
  Add regression test. (microsoft#29433)
  Elaborate jsx children elementwise (microsoft#29264)
  Add regression test
  PR feedback
  Fix trailing whitespace
  ...
uniqueiniquity added a commit that referenced this pull request Jan 31, 2019
<!--
Thank you for submitting a pull request!

Here's a checklist you might find useful.
* [ ] There is an associated issue that is labeled
  'Bug' or 'help wanted' or is in the Community milestone
* [ ] Code is up-to-date with the `master` branch
* [ ] You've successfully run `jake runtests` locally
* [ ] You've signed the CLA
* [ ] There are new or updated unit tests validating the change

Refer to CONTRIBUTING.MD for more details.
  https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md
-->

Fixes #29585.
#29314 and #29385 made it so their respective settings are only recognized when provided to the host as a whole.
This PR makes it so that the relevant settings for the preferences on the file override those of the preferences on the host.
@justingrant
Copy link
Contributor

@uniqueiniquity - I just tried this new feature for the first time (thank you so much for building it!) and I was surprised that renaming a property of a named, destructured export wouldn't rename the symbol when it's imported in another file. Is this expected? I'll file a separate issue if not expected. Here's the repro:

  1. Uncheck the "Rename Shorthand Properties" options for TS and JS
  2. Create a new code file env.ts:
type ReadonlyAndNotUndefined<T> = {  readonly [P in keyof T]: string; }
export const {
  APP_NAME,
  MONGODB_CONNECTION_URI, 
  SENDGRID_API_KEY,
  NOTIFICATION_URL_BASE,
  NOTIFICATION_FROM_EMAIL,
} = process.env as ReadonlyAndNotUndefined<typeof process.env>;
  1. Create another file, use_env.ts :
import { NOTIFICATION_URL_BASE, SENDGRID_API_KEY, NOTIFICATION_FROM_EMAIL } from './env';
  1. In env.ts, use rename symbol (F2) to change the name of NOTIFICATION_FROM_EMAIL

Expected: name is also changed in use_env.ts
Actual: no change to use_env.ts, and a compiler error :

Module '"src/libs/env"' has no exported member 'NOTIFICATION_FROM_EMAIL'. Did you mean 'NOTIFICATION_FROM_EMAIL_2'?

Note that if you export individual variables (not via destructuring export) the rename works fine, e.g.
export const foo = 'bar'; can be renamed to foo2 and the rename will propagate to imports in other files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants