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

feat(typescript-estree): add EXPERIMENTAL_useProjectService option to use TypeScript project service #6754

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
550465b
WIP: createProjectService
JoshuaKGoldberg Mar 24, 2023
6ea9f07
Merge branch 'v6' into create-project-service
JoshuaKGoldberg Apr 16, 2023
05f4d99
Collected updates: reuse program; lean more into tsserver
JoshuaKGoldberg Apr 16, 2023
1fed323
chore: fix website config.ts type checking
JoshuaKGoldberg Apr 16, 2023
ecd5d3d
Spell checking
JoshuaKGoldberg Apr 16, 2023
78f8c4a
remove .only
JoshuaKGoldberg Apr 17, 2023
76acaeb
Cleaned up todo comments in code and properly restrict new option
JoshuaKGoldberg Apr 18, 2023
a68dece
Added separate test run in CI for experimental option
JoshuaKGoldberg Apr 18, 2023
6f6f02a
fix: no node-version
JoshuaKGoldberg Apr 18, 2023
e43145f
Remove process.env manual set
JoshuaKGoldberg Apr 18, 2023
214fc8d
Fix linter config.ts
JoshuaKGoldberg Apr 18, 2023
a2f71a7
Tweaked project creation to try to explicitly set cwd
JoshuaKGoldberg Apr 18, 2023
4ffaffa
Progress on updating unit tests for absolute paths
JoshuaKGoldberg Apr 24, 2023
20d9f82
fix: add missing clearTSServerProjectService
JoshuaKGoldberg Apr 24, 2023
6ef8498
Add more path relativity fixes
JoshuaKGoldberg Apr 25, 2023
195462d
Lint fixes and watch program relativity
JoshuaKGoldberg Apr 25, 2023
0db7485
No, not always true
JoshuaKGoldberg Apr 25, 2023
b583bb3
Fix around semanticInfo.test.ts
JoshuaKGoldberg Apr 25, 2023
5b9ca22
Switch snapshot to inline
JoshuaKGoldberg Apr 25, 2023
a9aec01
perf: only openExternalProject once per project
JoshuaKGoldberg Apr 27, 2023
4ab1b3d
Merge branch 'v6' into create-project-service
JoshuaKGoldberg Apr 27, 2023
be6cc5d
Revert "perf: only openExternalProject once per project"
JoshuaKGoldberg Apr 27, 2023
80110c1
Reverted changes to allow alternate TSConfig names
JoshuaKGoldberg Apr 27, 2023
c326886
Remove project existence checking
JoshuaKGoldberg Apr 28, 2023
a5772e2
Add linting from root
JoshuaKGoldberg Apr 28, 2023
128837a
Refactor CI naming a bit, and bumping to MacOS image...
JoshuaKGoldberg Apr 28, 2023
4f52573
Alas, linting from root style runs out of memory
JoshuaKGoldberg Apr 28, 2023
b1da422
Added a test, why not
JoshuaKGoldberg Apr 28, 2023
814a8ad
fix: don't fall back to default program/project creation
JoshuaKGoldberg May 3, 2023
a5180c7
Fixed up more test exclusions
JoshuaKGoldberg May 3, 2023
37d3548
Move tsserver import to a require
JoshuaKGoldberg May 3, 2023
00ddecd
rename: tsserverType -> ts
JoshuaKGoldberg May 3, 2023
b5d76d4
Merge branch 'v6' into create-project-service
JoshuaKGoldberg May 8, 2023
da45669
Use path.resolve, and then simplify parserSettings usage
JoshuaKGoldberg Jun 13, 2023
69be30f
Merge branch 'main'
JoshuaKGoldberg Jul 12, 2023
93d369c
Remove unneeded typingsInstaller
JoshuaKGoldberg Jul 12, 2023
7e74202
Merge branch 'main' into create-project-service
JoshuaKGoldberg Jul 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ const DEFAULT_EXTRA_FILE_EXTENSIONS = [
*/
function createProjectProgram(
parseSettings: ParseSettings,
programsForProjects: readonly ts.Program[],
): ASTAndDefiniteProgram | undefined {
log('Creating project program for: %s', parseSettings.filePath);

const programsForProjects = getWatchProgramsForProjects(parseSettings);
// const programsForProjects = getWatchProgramsForProjects(parseSettings);
// const programsForProjects = createPrograms(parseSettings);
const astAndProgram = firstDefined(programsForProjects, currentProgram =>
getAstFromProgram(currentProgram, parseSettings),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as ts from 'typescript';
import * as tsserver from 'typescript/lib/tsserverlibrary';
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

export function createProjectService() {
const compilerOptions = { /* todo */ strict: true };
const compilerHost = tsserver.createCompilerHost(compilerOptions, true);

// TODO: see getWatchProgramsForProjects
// We don't watch the disk, we just refer to these when ESLint calls us
// there's a whole separate update pass in maybeInvalidateProgram at the bottom of getWatchProgramsForProjects
// (this "goes nuclear on TypeScript")
const watchFile = (
path: string,
callback: ts.FileWatcherCallback,
): ts.FileWatcher => {
// todo (or just ... stub out?)
return { close() {} };
};

const watchDirectory = (
path: string,
callback: ts.DirectoryWatcherCallback,
) => {
// todo (or just ... stub out?)
return { close() {} };
};

const system = {
...ts.sys,
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
watchFile,
watchDirectory,
setTimeout,
clearTimeout,
setImmediate,
clearImmediate,
};

const projectService = new tsserver.server.ProjectService({
host: system,

// todo: look into these
cancellationToken: { isCancellationRequested: () => false },
useSingleInferredProject: false, // ???
useInferredProjectPerProjectRoot: false, // ???
typingsInstaller: {
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
attach: () => {},
enqueueInstallTypingsRequest: () => {},
installPackage: async (): Promise<never> => {
throw new Error('pls no');
},
isKnownTypesPackageName: () => false,
onProjectClosed: () => {},
globalTypingsCacheLocation: '',
},
logger: {
Copy link
Member

Choose a reason for hiding this comment

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

probably be good to wire this up to our debug output so we can toggle it on for verbose debugging purposes?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ooh good point. I'll leave that as a followup issue 👍 it'd be a good fodder for getting someone other than me involved in this area of code.

close() {},
hasLevel: () => false,
loggingEnabled: () => false,
perftrc() {},
info() {},
startGroup() {},
endGroup() {},
msg() {},
getLogFileName: () => undefined,
},
session: undefined,
});
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

return projectService;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import debug from 'debug';
import type * as ts from 'typescript';
import { createProjectService } from '../create-program/createProjectService';

import { ensureAbsolutePath } from '../create-program/shared';
import type { TSESTreeOptions } from '../parser-options';
Expand Down Expand Up @@ -31,6 +32,10 @@ export function createParseSettings(
? options.tsconfigRootDir
: process.cwd();
const parseSettings: MutableParseSettings = {
// todo: sometimes
projectService: createProjectService(),
EXPERIMENTAL_useProjectService: true,

allowInvalidAST: options.allowInvalidAST === true,
code,
codeFullText,
Expand Down
7 changes: 7 additions & 0 deletions packages/typescript-estree/src/parseSettings/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type * as ts from 'typescript';
import type * as tsserverlibrary from 'typescript/lib/tsserverlibrary';

import type { CanonicalPath } from '../create-program/shared';
import type { TSESTree } from '../ts-estree';
Expand All @@ -10,6 +11,9 @@ type DebugModule = 'typescript-eslint' | 'eslint' | 'typescript';
* Internal settings used by the parser to run on a file.
*/
export interface MutableParseSettings {
// todo
projectService: tsserverlibrary.server.ProjectService | undefined;

/**
* Prevents the parser from throwing an error if it receives an invalid AST from TypeScript.
*/
Expand Down Expand Up @@ -57,6 +61,9 @@ export interface MutableParseSettings {
*/
errorOnUnknownASTType: boolean;

// todo
EXPERIMENTAL_useProjectService: boolean;

/**
* Whether TS should use the source files for referenced projects instead of the compiled .d.ts files.
*
Expand Down
52 changes: 51 additions & 1 deletion packages/typescript-estree/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { convertError } from './convert';
import { createDefaultProgram } from './create-program/createDefaultProgram';
import { createIsolatedProgram } from './create-program/createIsolatedProgram';
import { createProjectProgram } from './create-program/createProjectProgram';
import { createProjectService } from './create-program/createProjectService';
import {
createNoProgram,
createSourceFile,
} from './create-program/createSourceFile';
import { getWatchProgramsForProjects } from './create-program/getWatchProgramsForProjects';
import type { ASTAndProgram, CanonicalPath } from './create-program/shared';
import {
createProgramFromConfigFile,
Expand Down Expand Up @@ -57,8 +59,43 @@ function getProgramAndAST(
}
}

// todo; wrap in function or some such
if (parseSettings.projectService) {
parseSettings.projectService.openClientFile(
parseSettings.filePath,
parseSettings.codeFullText,
);

// todo: maybe we'll slap this into the parseSEttings as a property?
// const projectService = createProjectServiceProgram(/* parseSettings */);
const program = parseSettings.projectService
.getScriptInfo(parseSettings.filePath)!
.getDefaultProject()
.getLanguageService(/*ensureSynchronized*/ true)
.getProgram();

// TODO "Eventually, close the file"
// what does this actually do?
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
//

if (program) {
const projectServiceProgram = createProjectProgram(parseSettings, [
// LOL TYPES
// This'll hoepfully be resolved once they fully move to modules
// right now it's two separate files that get dumped out
program as ts.Program,
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
]);
if (projectServiceProgram) {
return projectServiceProgram;
}
}
}

if (hasFullTypeInformation) {
const fromProjectProgram = createProjectProgram(parseSettings);
const fromProjectProgram = createProjectProgram(
parseSettings,
getWatchProgramsForProjects(parseSettings),
);
if (fromProjectProgram) {
return fromProjectProgram;
}
Expand Down Expand Up @@ -284,3 +321,16 @@ export {
clearProgramCache,
clearParseAndGenerateServicesCalls,
};

// next steps
// 0. clean up comments
// 1. ping jake with questions
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
// * what does closing a file do
// 2. create versions of test suite tailored to this new fancy form
// 3. testing the bujeezus out of it

// aside:
// * performance testing - need to work on that
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
// (see TS folks - they do both memory usage and runtime)
// memory usage: makes bigger people happy (OOMs)
// runtime: more people can use it
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function baseTests(
}

describe('persistent parse', () => {
describe('includes not ending in a slash', () => {
describe.only('includes not ending in a slash', () => {
const tsConfigExcludeBar = {
include: ['src'],
exclude: ['./src/bar.ts'],
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/tests/lib/semanticInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ describe('semanticInfo', () => {
expect(tsComputedPropertyString.kind).toEqual(ts.SyntaxKind.StringLiteral);
});

it('imported-file tests', () => {
it.only('imported-file tests', () => {
const fileName = path.resolve(FIXTURES_DIR, 'import-file.src.ts');
const parseResult = parseCodeAndGenerateServices(
fs.readFileSync(fileName, 'utf8'),
Expand Down