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

fix(rule-tester): use cwd option to set base path for tests with file name#10201

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
bradzacher merged 5 commits intotypescript-eslint:mainfromreduckted:bugfix/10191
Oct 28, 2024
Merged
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
96 changes: 59 additions & 37 deletionspackages/rule-tester/src/RuleTester.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -166,7 +166,7 @@
}

export class RuleTester extends TestFramework {
readonly #linter:Linter;
readonly #lintersByBasePath: Map<string | undefined,Linter>;
readonly #rules: Record<string, AnyRuleCreateFunction | AnyRuleModule> = {};
readonly #testerConfig: TesterConfigWithDefaults;

Expand All@@ -184,31 +184,7 @@
rules: { [`${RULE_TESTER_PLUGIN_PREFIX}validate-ast`]: 'error' },
});

this.#linter = (() => {
const linter = new Linter({
configType: 'flat',
cwd: this.#testerConfig.languageOptions.parserOptions?.tsconfigRootDir,
});

// This nonsense is a workaround for https://github.com/jestjs/jest/issues/14840
// see also https://github.com/typescript-eslint/typescript-eslint/issues/8942
//
// For some reason rethrowing exceptions skirts around the circular JSON error.
const oldVerify = linter.verify.bind(linter);
linter.verify = (
...args: Parameters<Linter['verify']>
): ReturnType<Linter['verify']> => {
try {
return oldVerify(...args);
} catch (error) {
throw new Error('Caught an error while linting', {
cause: error,
});
}
};

return linter;
})();
this.#lintersByBasePath = new Map();

// make sure that the parser doesn't hold onto file handles between tests
// on linux (i.e. our CI env), there can be very a limited number of watch handles available
Expand All@@ -222,6 +198,57 @@
});
}

#getLinterForFilename(filename: string | undefined): Linter {
let basePath: string | undefined =
this.#testerConfig.languageOptions.parserOptions?.tsconfigRootDir;
// For an absolute path (`/foo.ts`), or a path that steps
// up (`../foo.ts`), resolve the path relative to the base
// path (using the current working directory if the parser
// options did not specify a base path) and use the file's
// root as the base path so that the file is under the base
// path. For any other path, which would just be a plain
// file name (`foo.ts`), don't change the base path.
if (
filename !== undefined &&
(filename.startsWith('/') || filename.startsWith('..'))
) {
basePath = path.parse(
path.resolve(basePath ?? process.cwd(), filename),
).root;
}

let linterForBasePath = this.#lintersByBasePath.get(basePath);
if (!linterForBasePath) {
linterForBasePath = (() => {
const linter = new Linter({
configType: 'flat',
cwd: basePath,
});

// This nonsense is a workaround for https://github.com/jestjs/jest/issues/14840
// see also https://github.com/typescript-eslint/typescript-eslint/issues/8942
//
// For some reason rethrowing exceptions skirts around the circular JSON error.
const oldVerify = linter.verify.bind(linter);
linter.verify = (
...args: Parameters<Linter['verify']>
): ReturnType<Linter['verify']> => {
try {
return oldVerify(...args);
} catch (error) {
throw new Error('Caught an error while linting', {

Check warning on line 239 in packages/rule-tester/src/RuleTester.ts

View check run for this annotation

Codecov/ codecov/patch

packages/rule-tester/src/RuleTester.ts#L239

Added line #L239 was not covered by tests
cause: error,
});
}
};

return linter;
})();
this.#lintersByBasePath.set(basePath, linterForBasePath);
}
return linterForBasePath;
}

/**
* Set the configuration to use for all future tests
*/
Expand DownExpand Up@@ -738,6 +765,7 @@
let passNumber = 0;
const outputs: string[] = [];
const configWithoutCustomKeys = omitCustomConfigProperties(config);
const linter = this.#getLinterForFilename(filename);

do {
passNumber++;
Expand DownExpand Up@@ -767,15 +795,7 @@
...configWithoutCustomKeys.linterOptions,
},
});
messages = this.#linter.verify(
code,
// ESLint uses an internal FlatConfigArray that extends @humanwhocodes/config-array.
Object.assign([], {
basePath: filename ? path.parse(filename).root : '',
getConfig: () => actualConfig,
}),
filename,
);
messages = linter.verify(code, actualConfig, filename);
} finally {
SourceCode.prototype.applyInlineConfig = applyInlineConfig;
SourceCode.prototype.applyLanguageOptions = applyLanguageOptions;
Expand All@@ -802,7 +822,7 @@
outputs.push(code);

// Verify if autofix makes a syntax error or not.
const errorMessageInFix =this.#linter
const errorMessageInFix = linter
.verify(fixedResult.output, configWithoutCustomKeys, filename)
.find(m => m.fatal);

Expand DownExpand Up@@ -1255,7 +1275,9 @@
]).output;

// Verify if suggestion fix makes a syntax error or not.
const errorMessageInSuggestion = this.#linter
const errorMessageInSuggestion = this.#getLinterForFilename(
item.filename,
)
.verify(
codeWithAppliedSuggestion,
omitCustomConfigProperties(result.config),
Expand Down
97 changes: 91 additions & 6 deletionspackages/rule-tester/tests/filename.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
/* eslint-disable perfectionist/sort-objects */
import { RuleTester } from '@typescript-eslint/rule-tester';
import { ESLintUtils } from '@typescript-eslint/utils';
import type { TSESLint } from '@typescript-eslint/utils';

const ruleTester = new RuleTester();
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';

import { RuleTester } from '../src/RuleTester';

const rule = ESLintUtils.RuleCreator.withoutDocs({
meta: {
Expand All@@ -11,33 +12,68 @@ const rule = ESLintUtils.RuleCreator.withoutDocs({
},
messages: {
foo: 'It works',
createError: 'Create error',
},
schema: [],
type: 'problem',
hasSuggestions: true,
},
defaultOptions: [],
create: context => ({
Program(node): void {
context.report({ node, messageId: 'foo' });
context.report({
node,
messageId: 'foo',
suggest:
node.body.length === 1 &&
node.body[0].type === AST_NODE_TYPES.EmptyStatement
? [
{
messageId: 'createError',
fix(fixer): TSESLint.RuleFix {
return fixer.replaceText(node, '//');
},
},
]
: [],
});
},
}),
});

describe('rule tester filename', () => {
ruleTester.run('absolute path', rule, {
new RuleTester().run('without tsconfigRootDir', rule, {
invalid: [
{
name: 'absolute path',
code: '_',
errors: [{ messageId: 'foo' }],
filename: '/an-absolute-path/foo.js',
},
{
name: 'relative path above project',
code: '_',
errors: [{ messageId: 'foo' }],
filename: '../foo.js',
},
],
valid: [],
});

ruleTester.run('relative path', rule, {
new RuleTester({
languageOptions: {
parserOptions: { tsconfigRootDir: '/some/path/that/totally/exists/' },
},
}).run('with tsconfigRootDir', rule, {
invalid: [
{
name: 'absolute path',
code: '_',
errors: [{ messageId: 'foo' }],
filename: '/an-absolute-path/foo.js',
},
{
name: 'relative path above project',
code: '_',
errors: [{ messageId: 'foo' }],
filename: '../foo.js',
Expand All@@ -46,3 +82,52 @@ describe('rule tester filename', () => {
valid: [],
});
});

describe('rule tester suggestion syntax error checks', () => {
new RuleTester().run('verifies suggestion with absolute path', rule, {
invalid: [
{
code: ';',
errors: [
{
messageId: 'foo',
suggestions: [{ messageId: 'createError', output: '//' }],
Copy link
Member

Choose a reason for hiding this comment

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

This isn't a syntax error, right? Cos a line comment is valid. We need something invalid like( that will truly crash out the parser.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

},
],
filename: '/an-absolute-path/foo.js',
},
],
valid: [],
});

new RuleTester().run('verifies suggestion with relative path', rule, {
invalid: [
{
code: ';',
errors: [
{
messageId: 'foo',
suggestions: [{ messageId: 'createError', output: '//' }],
},
],
filename: '../foo.js',
},
],
valid: [],
});

new RuleTester().run('verifies suggestion with no path', rule, {
invalid: [
{
code: ';',
errors: [
{
messageId: 'foo',
suggestions: [{ messageId: 'createError', output: '//' }],
},
],
},
],
valid: [],
});
});
Loading

[8]ページ先頭

©2009-2025 Movatter.jp