Rename mock and require file references#62834
Rename mock and require file references#62834hjonasson wants to merge 7 commits intomicrosoft:mainfrom
Conversation
|
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
1 similar comment
|
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
|
Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document them on our wiki's API Breaking Changes page. Also, please make sure @DanielRosenwasser and @RyanCavanaugh are aware of the changes, just as a heads up. |
@microsoft-github-policy-service agree |
…hjonasson/TypeScript into feature/rename-mock-and-require-imports
|
Hey @hjonasson, thanks for the PR but
We're doing most of our work over at typescript-go which we plan to eventually move over here - but we're also trying to avoid new scope, instead focusing on parity. Once we have that, I'd say we could consider new work in this direction. |
|
I appreciate the feedback @DanielRosenwasser . Let me brush up on my Go 😆 Could I get your thoughts on where code like this should live. I am hesitant to hardcode tool specifics into the language server itself but I am not sure where else to put it? Jest plugins seem wrong and that would probably require some tsserver support anyway 🤔 |
|
After having a go at it in Typescript-Go it looks like the file renaming hasn't been implemented yet 🤔 I might give up on this for now. |
Hi 👋
I am both forgetful and lazy. That has caused me to get a broken pipeline too many times when I move files, relying on auto-updates of file paths. That breaks other file references like
jest.mock("my-moved-file")orrequire("my-moved-file")I know these are not imports, but function calls. However, both are file references for practical purposes, and I would love for this to be covered. Did I use AI to help me with this? Yes. However, I also tested it extensively with a local build of tsserver in various projects, in addition the added unit tests. I also searched open and closed issues for this feature but couldn't find it. My apologies if this has been rejected before.
Before:
After:
Implementation Details
Changes Made
Added
updateImportsInTestFrameworkCallspreference (src/compiler/types.ts)UserPreferencesinterfaceExtended
getEditsForFileRenamefunctionality (src/services/getEditsForFileRename.ts)getModulePathArgument()function to detect module loading patternsupdateModuleLoadingCalls()function to traverse AST and update pathsforEachChildresultSupported Patterns
jest.mock(),jest.unmock(),jest.requireActual(),jest.requireMock(),jest.doMock(),jest.dontMock()vitest.mock(),vi.mock()import(),require()(already supported, now with improved coverage)Design Decisions
testFrameworkPatternsarrayjest.mock()), not nested property accessesSetto track already-processed imports, avoiding redundant updatesTesting
Files Changed
src/compiler/types.ts- Added new preference interfacesrc/services/getEditsForFileRename.ts- Implemented feature logic and bug fixtests/baselines/reference/api/typescript.d.ts- Updated API baselinetests/cases/fourslash/getEditsForFileRename_jestMock.ts- Added test coveragetests/cases/fourslash/getEditsForFileRename_requireInTs.ts- Added test coveragetests/cases/fourslash/getEditsForFileRename_dynamicImport.ts- Added test coveragePlease verify that:
Backlogmilestone (required) (sorry 🙏 Happy to create one if you like the idea)mainbranchhereby runtestslocally✅ Refer to CONTRIBUTING.MD for more details.
https://github.com/Microsoft/TypeScript/blob/main/CONTRIBUTING.md
#62835