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 36 commits
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
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -193,6 +193,38 @@ jobs:
# Sadly 1 day is the minimum
retention-days: 1

unit_tests_tsserver:
name: Run Unit Tests with Experimental TSServer
needs: [build]
runs-on: ubuntu-latest
strategy:
matrix:
package:
[
'eslint-plugin',
'eslint-plugin-internal',
'eslint-plugin-tslint',
'typescript-estree',
]
env:
COLLECT_COVERAGE: false
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 2
- name: Install
uses: ./.github/actions/prepare-install
with:
node-version: 18
- name: Build
uses: ./.github/actions/prepare-build
- name: Run unit tests for ${{ matrix.package }}
run: npx nx test ${{ matrix.package }} --coverage=false
env:
CI: true
TYPESCRIPT_ESLINT_EXPERIMENTAL_TSSERVER: true
Comment on lines +196 to +226
Copy link
Member

Choose a reason for hiding this comment

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

This was a great thing to add!


website_tests:
# The NETLIFY_TOKEN secret will not be available on forks
if: github.repository_owner == 'typescript-eslint'
Expand Down
34 changes: 34 additions & 0 deletions .vscode/launch.json
Expand Up @@ -141,6 +141,40 @@
"${workspaceFolder}/packages/scope-manager/dist/index.js",
],
},
{
"type": "node",
"request": "launch",
"name": "Jest Test Current eslint-plugin-tslint Rule",
"cwd": "${workspaceFolder}/packages/eslint-plugin-tslint/",
"program": "${workspaceFolder}/node_modules/jest/bin/jest.js",
"args": [
"--runInBand",
"--no-cache",
"--no-coverage",
"${fileBasenameNoExtension}"
],
"sourceMaps": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"${workspaceFolder}/packages/utils/src/index.ts",
"${workspaceFolder}/packages/utils/dist/index.js",
"${workspaceFolder}/packages/utils/src/ts-estree.ts",
"${workspaceFolder}/packages/utils/dist/ts-estree.js",
"${workspaceFolder}/packages/type-utils/src/index.ts",
"${workspaceFolder}/packages/type-utils/dist/index.js",
"${workspaceFolder}/packages/parser/src/index.ts",
"${workspaceFolder}/packages/parser/dist/index.js",
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
"${workspaceFolder}/packages/typescript-estree/dist/index.js",
"${workspaceFolder}/packages/types/src/index.ts",
"${workspaceFolder}/packages/types/dist/index.js",
"${workspaceFolder}/packages/visitor-keys/src/index.ts",
"${workspaceFolder}/packages/visitor-keys/dist/index.js",
"${workspaceFolder}/packages/scope-manager/dist/index.js",
"${workspaceFolder}/packages/scope-manager/dist/index.js",
],
},
{
"type": "node",
"request": "launch",
Expand Down
11 changes: 10 additions & 1 deletion docs/packages/TypeScript_ESTree.mdx
Expand Up @@ -147,6 +147,15 @@ interface ParseAndGenerateServicesOptions extends ParseOptions {
*/
errorOnTypeScriptSyntacticAndSemanticIssues?: boolean;

/**
* ***EXPERIMENTAL FLAG*** - Use this at your own risk.
*
* Whether to create a shared TypeScript server to power program creation.
*
* @see https://github.com/typescript-eslint/typescript-eslint/issues/6575
*/
EXPERIMENTAL_useProjectService?: boolean;

/**
* ***EXPERIMENTAL FLAG*** - Use this at your own risk.
*
Expand All @@ -155,7 +164,7 @@ interface ParseAndGenerateServicesOptions extends ParseOptions {
*
* This flag REQUIRES at least TS v3.9, otherwise it does nothing.
*
* See: https://github.com/typescript-eslint/typescript-eslint/issues/2094
* @see https://github.com/typescript-eslint/typescript-eslint/issues/2094
*/
EXPERIMENTAL_useSourceOfProjectReferenceRedirect?: boolean;

Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin-tslint/src/rules/config.ts
@@ -1,4 +1,5 @@
import { ESLintUtils } from '@typescript-eslint/utils';
import path from 'path';
import type { RuleSeverity } from 'tslint';
import { Configuration } from 'tslint';

Expand Down Expand Up @@ -118,7 +119,7 @@ export default createRule<Options, MessageIds>({
context,
[{ rules: tslintRules, rulesDirectory: tslintRulesDirectory, lintFile }],
) {
const fileName = context.getFilename();
const fileName = path.resolve(context.getCwd(), context.getFilename());
Copy link
Member Author

Choose a reason for hiding this comment

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

Explaining: with EXPERIMENTAL_ProjectService, relative paths might no longer match up with how the project views its root file. So switching to absolute paths is more reliable.

const sourceCode = context.getSourceCode().text;
const services = ESLintUtils.getParserServices(context);
const program = services.program;
Expand Down
Expand Up @@ -16,6 +16,7 @@ const ruleTester = new RuleTester({
});

const withMetaParserOptions = {
EXPERIMENTAL_useProjectService: false,
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
tsconfigRootDir: getFixturesRootDir(),
project: './tsconfig-withmeta.json',
};
Expand Down
Expand Up @@ -595,6 +595,7 @@ function getElem(dict: Record<string, { foo: string }>, key: string) {
}
`,
parserOptions: {
EXPERIMENTAL_useProjectService: false,
tsconfigRootDir: getFixturesRootDir(),
project: './tsconfig.noUncheckedIndexedAccess.json',
},
Expand Down
Expand Up @@ -65,6 +65,7 @@ function assignmentTest(
const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
parserOptions: {
EXPERIMENTAL_useProjectService: false,
project: './tsconfig.noImplicitThis.json',
tsconfigRootDir: getFixturesRootDir(),
},
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/tests/rules/no-unsafe-call.test.ts
Expand Up @@ -6,6 +6,7 @@ import { getFixturesRootDir } from '../RuleTester';
const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
parserOptions: {
EXPERIMENTAL_useProjectService: false,
project: './tsconfig.noImplicitThis.json',
tsconfigRootDir: getFixturesRootDir(),
},
Expand Down
Expand Up @@ -6,6 +6,7 @@ import { getFixturesRootDir } from '../RuleTester';
const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
parserOptions: {
EXPERIMENTAL_useProjectService: false,
project: './tsconfig.noImplicitThis.json',
tsconfigRootDir: getFixturesRootDir(),
},
Expand Down
Expand Up @@ -6,6 +6,7 @@ import { getFixturesRootDir } from '../RuleTester';
const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
parserOptions: {
EXPERIMENTAL_useProjectService: false,
project: './tsconfig.noImplicitThis.json',
tsconfigRootDir: getFixturesRootDir(),
},
Expand Down
Expand Up @@ -14,6 +14,7 @@ const ruleTester = new RuleTester({
});

const withMetaParserOptions = {
EXPERIMENTAL_useProjectService: false,
tsconfigRootDir: getFixturesRootDir(),
project: './tsconfig-withmeta.json',
};
Expand Down
Expand Up @@ -204,6 +204,7 @@ const y = x!;

const ruleTesterWithNoUncheckedIndexAccess = new RuleTester({
parserOptions: {
EXPERIMENTAL_useProjectService: false,
sourceType: 'module',
tsconfigRootDir: getFixturesRootDir(),
project: './tsconfig.noUncheckedIndexedAccess.json',
Expand Down
7 changes: 4 additions & 3 deletions packages/parser/tests/lib/parser.ts
@@ -1,6 +1,7 @@
import * as scopeManager from '@typescript-eslint/scope-manager';
import type { ParserOptions } from '@typescript-eslint/types';
import * as typescriptESTree from '@typescript-eslint/typescript-estree';
import path from 'path';

import { parse, parseForESLint } from '../../src/parser';

Expand Down Expand Up @@ -33,10 +34,10 @@ describe('parser', () => {
jsx: false,
},
// ts-estree specific
filePath: 'isolated-file.src.ts',
filePath: './isolated-file.src.ts',
project: 'tsconfig.json',
errorOnTypeScriptSyntacticAndSemanticIssues: false,
tsconfigRootDir: 'tests/fixtures/services',
tsconfigRootDir: path.resolve(__dirname, '../fixtures/services'),
extraFileExtensions: ['.foo'],
};
parseForESLint(code, config);
Expand Down Expand Up @@ -89,7 +90,7 @@ describe('parser', () => {
filePath: 'isolated-file.src.ts',
project: 'tsconfig.json',
errorOnTypeScriptSyntacticAndSemanticIssues: false,
tsconfigRootDir: 'tests/fixtures/services',
tsconfigRootDir: path.join(__dirname, '../fixtures/services'),
extraFileExtensions: ['.foo'],
};
parseForESLint(code, config);
Expand Down
2 changes: 2 additions & 0 deletions packages/rule-tester/tests/RuleTester.test.ts
Expand Up @@ -169,6 +169,7 @@ describe('RuleTester', () => {
{
code: 'type-aware parser options should override the constructor config',
parserOptions: {
EXPERIMENTAL_useProjectService: false,
project: 'tsconfig.test-specific.json',
tsconfigRootDir: '/set/in/the/test/',
},
Expand Down Expand Up @@ -209,6 +210,7 @@ describe('RuleTester', () => {
"code": "type-aware parser options should override the constructor config",
"filename": "/set/in/the/test/file.ts",
"parserOptions": {
"EXPERIMENTAL_useProjectService": false,
"project": "tsconfig.test-specific.json",
"tsconfigRootDir": "/set/in/the/test/",
},
Expand Down
20 changes: 10 additions & 10 deletions packages/type-utils/tests/TypeOrValueSpecifier.test.ts
Expand Up @@ -196,42 +196,42 @@ describe('TypeOrValueSpecifier', () => {
],
[
'interface Foo {prop: string}; type Test = Foo;',
{ from: 'file', name: 'Foo', path: 'tests/fixtures/file.ts' },
{ from: 'file', name: 'Foo', path: 'file.ts' },
],
[
'type Foo = {prop: string}; type Test = Foo;',
{ from: 'file', name: 'Foo', path: 'tests/fixtures/file.ts' },
{ from: 'file', name: 'Foo', path: 'file.ts' },
],
[
'interface Foo {prop: string}; type Test = Foo;',
{
from: 'file',
name: 'Foo',
path: 'tests/../tests/fixtures/////file.ts',
path: './////file.ts',
},
],
[
'type Foo = {prop: string}; type Test = Foo;',
{
from: 'file',
name: 'Foo',
path: 'tests/../tests/fixtures/////file.ts',
path: './////file.ts',
},
],
[
'interface Foo {prop: string}; type Test = Foo;',
{
from: 'file',
name: ['Foo', 'Bar'],
path: 'tests/fixtures/file.ts',
path: 'file.ts',
},
],
[
'type Foo = {prop: string}; type Test = Foo;',
{
from: 'file',
name: ['Foo', 'Bar'],
path: 'tests/fixtures/file.ts',
path: 'file.ts',
},
],
])('matches a matching file specifier: %s', runTestPositive);
Expand All @@ -247,14 +247,14 @@ describe('TypeOrValueSpecifier', () => {
],
[
'interface Foo {prop: string}; type Test = Foo;',
{ from: 'file', name: 'Foo', path: 'tests/fixtures/wrong-file.ts' },
{ from: 'file', name: 'Foo', path: 'wrong-file.ts' },
],
[
'interface Foo {prop: string}; type Test = Foo;',
{
from: 'file',
name: ['Foo', 'Bar'],
path: 'tests/fixtures/wrong-file.ts',
path: 'wrong-file.ts',
},
],
])("doesn't match a mismatched file specifier: %s", runTestNegative);
Expand Down Expand Up @@ -399,14 +399,14 @@ describe('TypeOrValueSpecifier', () => {
['type Test = RegExp;', { from: 'file', name: ['RegExp', 'BigInt'] }],
[
'type Test = RegExp;',
{ from: 'file', name: 'RegExp', path: 'tests/fixtures/file.ts' },
{ from: 'file', name: 'RegExp', path: 'file.ts' },
],
[
'type Test = RegExp;',
{
from: 'file',
name: ['RegExp', 'BigInt'],
path: 'tests/fixtures/file.ts',
path: 'file.ts',
},
],
[
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/parser-options.ts
Expand Up @@ -47,6 +47,7 @@ interface ParserOptions {
debugLevel?: DebugLevel;
errorOnTypeScriptSyntacticAndSemanticIssues?: boolean;
errorOnUnknownASTType?: boolean;
EXPERIMENTAL_useProjectService?: boolean; // purposely undocumented for now
EXPERIMENTAL_useSourceOfProjectReferenceRedirect?: boolean; // purposely undocumented for now
extraFileExtensions?: string[];
filePath?: string;
Expand Down
6 changes: 5 additions & 1 deletion packages/typescript-estree/src/clear-caches.ts
@@ -1,6 +1,9 @@
import { clearWatchCaches } from './create-program/getWatchProgramsForProjects';
import { clearProgramCache as clearProgramCacheOriginal } from './parser';
import { clearTSConfigMatchCache } from './parseSettings/createParseSettings';
import {
clearTSConfigMatchCache,
clearTSServerProjectService,
} from './parseSettings/createParseSettings';
import { clearGlobCache } from './parseSettings/resolveProjectList';

/**
Expand All @@ -14,6 +17,7 @@ export function clearCaches(): void {
clearProgramCacheOriginal();
clearWatchCaches();
clearTSConfigMatchCache();
clearTSServerProjectService();
clearGlobCache();
}

Expand Down
Expand Up @@ -5,7 +5,6 @@ import * as ts from 'typescript';
import { firstDefined } from '../node-utils';
import type { ParseSettings } from '../parseSettings';
import { describeFilePath } from './describeFilePath';
import { getWatchProgramsForProjects } from './getWatchProgramsForProjects';
import type { ASTAndDefiniteProgram } from './shared';
import { getAstFromProgram } from './shared';

Expand All @@ -28,12 +27,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 astAndProgram = firstDefined(programsForProjects, currentProgram =>
getAstFromProgram(currentProgram, parseSettings),
getAstFromProgram(currentProgram, parseSettings.filePath),
);

// The file was either matched within the tsconfig, or we allow creating a default program
Expand Down