-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(typescript-estree): use simpler absolutify behavior for project service client file paths #8520
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
Merged
JoshuaKGoldberg
merged 11 commits into
typescript-eslint:main
from
JoshuaKGoldberg:use-program-from-project-service-absolutify
Feb 24, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f35daa7
fix(typescript-estree): use simpler absolutify behavior for project s…
JoshuaKGoldberg 75884ca
A bit of internal cleanup, and added unit tests
JoshuaKGoldberg ddb17c7
Lol Windows
JoshuaKGoldberg c4bfec0
Standardize type specifier style
JoshuaKGoldberg cd12fbe
Merge branch 'main'
JoshuaKGoldberg 8caf0bd
Merge branch 'main'
JoshuaKGoldberg 3626af5
oopity
JoshuaKGoldberg 58be38c
Merge branch 'main' into use-program-from-project-service-absolutify
JoshuaKGoldberg 5dabf62
Correct unit test for undefined
JoshuaKGoldberg 65685ce
Update packages/typescript-estree/src/useProgramFromProjectService.ts
JoshuaKGoldberg 898b1df
cspell
JoshuaKGoldberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,7 @@ | |
"blurple", | ||
"bradzacher", | ||
"camelcase", | ||
"canonicalize", | ||
"Cena", | ||
"codebases", | ||
"Codecov", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
packages/typescript-estree/tests/lib/useProgramFromProjectService.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* eslint-disable @typescript-eslint/explicit-function-return-type -- Fancy mocks */ | ||
import path from 'path'; | ||
|
||
import type { TypeScriptProjectService } from '../../src/create-program/createProjectService'; | ||
import type { ParseSettings } from '../../src/parseSettings'; | ||
import { useProgramFromProjectService } from '../../src/useProgramFromProjectService'; | ||
|
||
const mockCreateProjectProgram = jest.fn(); | ||
|
||
jest.mock('../../src/create-program/createProjectProgram', () => ({ | ||
get createProjectProgram() { | ||
return mockCreateProjectProgram; | ||
}, | ||
})); | ||
|
||
const mockGetProgram = jest.fn(); | ||
|
||
const currentDirectory = '/repos/repo'; | ||
|
||
function createMockProjectService() { | ||
const openClientFile = jest.fn(); | ||
const service = { | ||
getDefaultProjectForFile: () => ({ | ||
getLanguageService: () => ({ | ||
getProgram: mockGetProgram, | ||
}), | ||
}), | ||
getScriptInfo: () => ({}), | ||
host: { | ||
getCurrentDirectory: () => currentDirectory, | ||
}, | ||
openClientFile, | ||
}; | ||
|
||
return { | ||
service: service as typeof service & TypeScriptProjectService, | ||
openClientFile, | ||
}; | ||
} | ||
|
||
const mockParseSettings = { | ||
filePath: 'path/PascalCaseDirectory/camelCaseFile.ts', | ||
} as ParseSettings; | ||
|
||
describe('useProgramFromProjectService', () => { | ||
it('passes an absolute, case-matching file path to service.openClientFile', () => { | ||
const { service } = createMockProjectService(); | ||
|
||
useProgramFromProjectService( | ||
{ allowDefaultProjectForFiles: undefined, service }, | ||
mockParseSettings, | ||
false, | ||
); | ||
|
||
expect(service.openClientFile).toHaveBeenCalledWith( | ||
path.normalize('/repos/repo/path/PascalCaseDirectory/camelCaseFile.ts'), | ||
undefined, | ||
undefined, | ||
undefined, | ||
); | ||
}); | ||
|
||
it('throws an error when hasFullTypeInformation is enabled and the file is both in the project service and allowDefaultProjectForFiles', () => { | ||
const { service } = createMockProjectService(); | ||
|
||
service.openClientFile.mockReturnValueOnce({ | ||
configFileName: 'tsconfig.json', | ||
}); | ||
|
||
expect(() => | ||
useProgramFromProjectService( | ||
{ allowDefaultProjectForFiles: [mockParseSettings.filePath], service }, | ||
mockParseSettings, | ||
true, | ||
), | ||
).toThrow( | ||
`${mockParseSettings.filePath} was included by allowDefaultProjectForFiles but also was found in the project service. Consider removing it from allowDefaultProjectForFiles.`, | ||
); | ||
}); | ||
|
||
it('throws an error when hasFullTypeInformation is enabled and the file is neither in the project service nor allowDefaultProjectForFiles', () => { | ||
const { service } = createMockProjectService(); | ||
|
||
service.openClientFile.mockReturnValueOnce({}); | ||
|
||
expect(() => | ||
useProgramFromProjectService( | ||
{ allowDefaultProjectForFiles: [], service }, | ||
mockParseSettings, | ||
true, | ||
), | ||
).toThrow( | ||
`${mockParseSettings.filePath} was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProjectForFiles.`, | ||
); | ||
}); | ||
|
||
it('returns undefined when hasFullTypeInformation is disabled, the file is both in the project service and allowDefaultProjectForFiles, and the service does not have a matching program', () => { | ||
const { service } = createMockProjectService(); | ||
|
||
mockGetProgram.mockReturnValue(undefined); | ||
|
||
service.openClientFile.mockReturnValueOnce({ | ||
configFileName: 'tsconfig.json', | ||
}); | ||
|
||
const actual = useProgramFromProjectService( | ||
{ allowDefaultProjectForFiles: [mockParseSettings.filePath], service }, | ||
mockParseSettings, | ||
false, | ||
); | ||
|
||
expect(actual).toBeUndefined(); | ||
}); | ||
|
||
it('returns a created program when hasFullTypeInformation is disabled, the file is both in the project service and allowDefaultProjectForFiles, and the service has a matching program', () => { | ||
const { service } = createMockProjectService(); | ||
const program = { getSourceFile: jest.fn() }; | ||
|
||
mockGetProgram.mockReturnValue(program); | ||
|
||
service.openClientFile.mockReturnValueOnce({ | ||
configFileName: 'tsconfig.json', | ||
}); | ||
mockCreateProjectProgram.mockReturnValueOnce(program); | ||
|
||
const actual = useProgramFromProjectService( | ||
{ allowDefaultProjectForFiles: [mockParseSettings.filePath], service }, | ||
mockParseSettings, | ||
false, | ||
); | ||
|
||
expect(actual).toBe(program); | ||
}); | ||
|
||
it('returns a created program when hasFullTypeInformation is disabled, the file is neither in the project service nor allowDefaultProjectForFiles, and the service has a matching program', () => { | ||
const { service } = createMockProjectService(); | ||
const program = { getSourceFile: jest.fn() }; | ||
|
||
mockGetProgram.mockReturnValue(program); | ||
|
||
service.openClientFile.mockReturnValueOnce({}); | ||
mockCreateProjectProgram.mockReturnValueOnce(program); | ||
|
||
const actual = useProgramFromProjectService( | ||
{ allowDefaultProjectForFiles: [], service }, | ||
mockParseSettings, | ||
false, | ||
); | ||
|
||
expect(actual).toBe(program); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.