Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
JoshuaKGoldberg merged 11 commits intotypescript-eslint:mainfromJoshuaKGoldberg:use-program-from-project-service-absolutifyFeb 24, 2024
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
11 commits Select commitHold shift + click to select a range
f35daa7
fix(typescript-estree): use simpler absolutify behavior for project s…
JoshuaKGoldberg75884ca
A bit of internal cleanup, and added unit tests
JoshuaKGoldbergddb17c7
Lol Windows
JoshuaKGoldbergc4bfec0
Standardize type specifier style
JoshuaKGoldbergcd12fbe
Merge branch 'main'
JoshuaKGoldberg8caf0bd
Merge branch 'main'
JoshuaKGoldberg3626af5
oopity
JoshuaKGoldberg58be38c
Merge branch 'main' into use-program-from-project-service-absolutify
JoshuaKGoldberg5dabf62
Correct unit test for undefined
JoshuaKGoldberg65685ce
Update packages/typescript-estree/src/useProgramFromProjectService.ts
JoshuaKGoldberg898b1df
cspell
JoshuaKGoldbergFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions.cspell.json
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", | ||
50 changes: 34 additions & 16 deletionspackages/typescript-estree/src/useProgramFromProjectService.ts
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. |
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 |
---|---|---|
@@ -1,13 +1,10 @@ | ||
import debug from 'debug'; | ||
import { minimatch } from 'minimatch'; | ||
import path from 'path'; | ||
import { createProjectProgram } from './create-program/createProjectProgram'; | ||
import type { ProjectServiceSettings } from './create-program/createProjectService'; | ||
import type { ASTAndDefiniteProgram } from './create-program/shared'; | ||
import type { MutableParseSettings } from './parseSettings'; | ||
const log = debug( | ||
@@ -19,11 +16,17 @@ export function useProgramFromProjectService( | ||
parseSettings: Readonly<MutableParseSettings>, | ||
hasFullTypeInformation: boolean, | ||
): ASTAndDefiniteProgram | undefined { | ||
// We don't canonicalize the filename because it caused a performance regression. | ||
// See https://github.com/typescript-eslint/typescript-eslint/issues/8519 | ||
const filePathAbsolute = absolutify(parseSettings.filePath); | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
log( | ||
'Opening project service file for: %s at absolute path %s', | ||
parseSettings.filePath, | ||
filePathAbsolute, | ||
); | ||
const opened = service.openClientFile( | ||
filePathAbsolute, | ||
parseSettings.codeFullText, | ||
/* scriptKind */ undefined, | ||
parseSettings.tsconfigRootDir, | ||
@@ -36,36 +39,51 @@ export function useProgramFromProjectService( | ||
'Project service type information enabled; checking for file path match on: %o', | ||
allowDefaultProjectForFiles, | ||
); | ||
const isDefaultProjectAllowedPath = filePathMatchedBy( | ||
parseSettings.filePath, | ||
allowDefaultProjectForFiles, | ||
); | ||
log( | ||
'Default project allowed path: %s, based on config file: %s', | ||
isDefaultProjectAllowedPath, | ||
opened.configFileName, | ||
); | ||
if (opened.configFileName) { | ||
if (isDefaultProjectAllowedPath) { | ||
throw new Error( | ||
`${parseSettings.filePath} was included by allowDefaultProjectForFiles but also was found in the project service. Consider removing it from allowDefaultProjectForFiles.`, | ||
); | ||
} | ||
} else if (!isDefaultProjectAllowedPath) { | ||
throw new Error( | ||
`${parseSettings.filePath} was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProjectForFiles.`, | ||
); | ||
} | ||
} | ||
log('Retrieving script info and then program for: %s', filePathAbsolute); | ||
const scriptInfo = service.getScriptInfo(filePathAbsolute); | ||
const program = service | ||
.getDefaultProjectForFile(scriptInfo!.fileName, true)! | ||
.getLanguageService(/*ensureSynchronized*/ true) | ||
.getProgram(); | ||
if (!program) { | ||
log('Could not find project service program for: %s',filePathAbsolute); | ||
return undefined; | ||
} | ||
log('Found project service program for: %s',filePathAbsolute); | ||
return createProjectProgram(parseSettings, [program]); | ||
function absolutify(filePath: string): string { | ||
return path.isAbsolute(filePath) | ||
? filePath | ||
: path.join(service.host.getCurrentDirectory(), filePath); | ||
} | ||
} | ||
function filePathMatchedBy( | ||
152 changes: 152 additions & 0 deletionspackages/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.