-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(typescript-estree): use simpler absolutify behavior for project s…
…ervice client file paths (#8520) * fix(typescript-estree): use simpler absolutify behavior for project service client file paths * A bit of internal cleanup, and added unit tests * Lol Windows * Standardize type specifier style * oopity * Correct unit test for undefined * Update packages/typescript-estree/src/useProgramFromProjectService.ts Co-authored-by: Brad Zacher <brad.zacher@gmail.com> * cspell --------- Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
- Loading branch information
1 parent
1807d55
commit 5e7ec8f
Showing
3 changed files
with
187 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 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); | ||
}); | ||
}); |