Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(rule-tester): check type-aware rules test cases for TS type errors#8351

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

Closed
auvred wants to merge11 commits intotypescript-eslint:mainfromauvred:feat/8298
Closed
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions.cspell.json
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -135,6 +135,7 @@
"necroing",
"Nicolò",
"nocheck",
"nodenext",
"noninteractive",
"Nrwl",
"nullish",
Expand Down
6 changes: 4 additions & 2 deletionspackages/eslint-plugin/tests/fixtures/tsconfig.json
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
{
"compilerOptions": {
"jsx": "preserve",
"target": "es5",
"target": "es2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"lib": ["es2015", "es2017", "esnext"],
"experimentalDecorators": true
"types": [],
"experimentalDecorators": true,
"skipLibCheck": true
},
"include": [
"file.ts",
Expand Down
5 changes: 5 additions & 0 deletionspackages/rule-tester/package.json
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -72,6 +72,11 @@
"source-map-support": "^0.5.21",
"typescript": "*"
},
"peerDependenciesMeta": {
"typescript": {
"optional": true
}
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/typescript-eslint"
Expand Down
63 changes: 60 additions & 3 deletionspackages/rule-tester/src/RuleTester.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,16 +7,21 @@ import type {
AnyRuleCreateFunction,
AnyRuleModule,
ParserOptions,
RuleContext,
RuleListener,
RuleModule,
} from '@typescript-eslint/utils/ts-eslint';

import * as parser from '@typescript-eslint/parser';
import { deepMerge } from '@typescript-eslint/utils/eslint-utils';
import {
deepMerge,
getParserServices,
} from '@typescript-eslint/utils/eslint-utils';
import { Linter } from '@typescript-eslint/utils/ts-eslint';
import assert from 'node:assert';
import path from 'node:path';
import util from 'node:util';
import * as ts from 'typescript';
// we intentionally import from eslint here because we need to use the same class
// that ESLint uses, not our custom override typed version
import { SourceCode } from 'eslint';
Expand DownExpand Up@@ -181,7 +186,10 @@ export class RuleTester extends TestFramework {
* configuration and the default configuration.
*/
this.#testerConfig = merge({}, defaultConfig, testerConfig, {
rules: { [`${RULE_TESTER_PLUGIN_PREFIX}validate-ast`]: 'error' },
rules: {
[`${RULE_TESTER_PLUGIN_PREFIX}collect-ts-diagnostics`]: 'error',
[`${RULE_TESTER_PLUGIN_PREFIX}validate-ast`]: 'error',
},
});

this.#lintersByBasePath = new Map();
Expand DownExpand Up@@ -629,11 +637,37 @@ export class RuleTester extends TestFramework {
} {
this.defineRule(ruleName, rule);

const shouldCollectTSDiagnostics =
item.runTSC === true ||
(item.runTSC !== false &&
rule.meta.docs &&
'requiresTypeChecking' in rule.meta.docs &&
!!rule.meta.docs.requiresTypeChecking);
let tsDiagnostics = null as readonly ts.Diagnostic[] | null;
let config: TesterConfigWithDefaults = merge({}, this.#testerConfig, {
files: ['**'],
plugins: {
[RULE_TESTER_PLUGIN]: {
rules: {
'collect-ts-diagnostics': {
create(
context: Readonly<RuleContext<string, unknown[]>>,
): RuleListener {
if (!shouldCollectTSDiagnostics) {
return {};
}
const services = getParserServices(context);
return {
Program(): void {
const diagnostics =
services.program.getSemanticDiagnostics();
if (diagnostics.length) {
tsDiagnostics ??= diagnostics;
}
},
};
},
},
/**
* Setup AST getters.
* The goal is to check whether or not AST was modified when
Expand All@@ -655,7 +689,7 @@ export class RuleTester extends TestFramework {
},
},
},
});
} satisfies RuleTesterConfig);

// Unlike other properties, we don't want to spread props between different parsers.
config.languageOptions.parser =
Expand DownExpand Up@@ -836,6 +870,29 @@ export class RuleTester extends TestFramework {
].join('\n'),
);
} while (fixedResult.fixed && passNumber < 10);
if (tsDiagnostics) {
const codesToIgnore = [
1375 /* 'await' expressions are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module. */,
1378 /* Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher. */,
6133 /* '{0}' is declared but its value is never read. */,
6138 /* Property '{0}' is declared but its value is never read. */,
];
for (const diagnostic of tsDiagnostics) {
if (
diagnostic.category !== ts.DiagnosticCategory.Error ||
codesToIgnore.includes(diagnostic.code)
) {
continue;
}

throw new Error(
`error TS${diagnostic.code}: ${ts.flattenDiagnosticMessageText(
diagnostic.messageText,
ts.sys.newLine,
)}`,
);
}
}

return {
config,
Expand Down
4 changes: 4 additions & 0 deletionspackages/rule-tester/src/types/ValidTestCase.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,6 +63,10 @@ export interface ValidTestCase<Options extends readonly unknown[]> {
* Options for the test case.
*/
readonly options?: Readonly<Options>;
/**
* TODO: add description (or maybe even give a better name?)
*/
readonly runTSC?: boolean;
/**
* Settings for the test case.
*/
Expand Down
1 change: 1 addition & 0 deletionspackages/rule-tester/src/utils/validationHelpers.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,6 +20,7 @@ export const RULE_TESTER_PARAMETERS = [
'options',
'output',
'skip',
'runTSC',
] as const;

/*
Expand Down
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp