Skip to content

fix(rule-tester): provide Linter a cwd in its constructor #9678

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import rule, {
PACKAGES_DIR,
} from '../../src/rules/no-relative-paths-to-internal-packages';

const ruleTester = new RuleTester();
const ruleTester = new RuleTester({
languageOptions: {
parserOptions: {
tsconfigRootDir: PACKAGES_DIR,
},
},
});

ruleTester.run('no-relative-paths-to-internal-packages', rule, {
valid: [
Expand Down
1 change: 0 additions & 1 deletion packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
"cross-env": "^7.0.3",
"cross-fetch": "*",
"eslint": "*",
"espree": "^10.0.1",
"jest": "29.7.0",
"jest-specific-snapshot": "^8.0.0",
"json-schema": "*",
Expand Down
24 changes: 0 additions & 24 deletions packages/eslint-plugin/tests/rules/no-use-before-define.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,30 +722,6 @@ function a() {}
},
{
code: `
'use strict';
a();
{
function a() {}
}
`,
languageOptions: {
// eslint-disable-next-line @typescript-eslint/no-require-imports
parser: require('espree'),
parserOptions: {
ecmaVersion: 6,
sourceType: 'script',
},
},
errors: [
{
messageId: 'noUseBeforeDefine',
data: { name: 'a' },
type: AST_NODE_TYPES.Identifier,
},
],
},
{
code: `
a();
try {
throw new Error();
Expand Down
59 changes: 29 additions & 30 deletions packages/rule-tester/src/RuleTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function getUnsubstitutedMessagePlaceholders(
export class RuleTester extends TestFramework {
readonly #testerConfig: TesterConfigWithDefaults;
readonly #rules: Record<string, AnyRuleCreateFunction | AnyRuleModule> = {};
readonly #linter: Linter = new Linter({ configType: 'flat' });
readonly #linter: Linter;

/**
* Creates a new instance of RuleTester.
Expand All @@ -183,6 +183,11 @@ export class RuleTester extends TestFramework {
rules: { [`${RULE_TESTER_PLUGIN_PREFIX}validate-ast`]: 'error' },
});

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

// 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
const constructor = this.constructor as typeof RuleTester;
Expand Down Expand Up @@ -282,14 +287,19 @@ export class RuleTester extends TestFramework {
Hugely helps with the string-based valid test cases as it means they don't
need to be made objects!
*/
const getFilename = (testOptions?: ParserOptions): string => {
const getFilename = (
originalFilename: string | undefined,
testOptions: ParserOptions | undefined,
): string => {
const resolvedOptions = deepMerge(
this.#testerConfig.languageOptions.parserOptions,
testOptions,
) as ParserOptions;
const filename = resolvedOptions.ecmaFeatures?.jsx
? this.#testerConfig.defaultFilenames.tsx
: this.#testerConfig.defaultFilenames.ts;
const filename =
originalFilename ??
(resolvedOptions.ecmaFeatures?.jsx
? this.#testerConfig.defaultFilenames.tsx
: this.#testerConfig.defaultFilenames.ts);
if (resolvedOptions.project) {
return path.join(
resolvedOptions.tsconfigRootDir ?? process.cwd(),
Expand All @@ -309,22 +319,19 @@ export class RuleTester extends TestFramework {
if (languageOptions.parser === parser) {
throw new Error(DUPLICATE_PARSER_ERROR_MESSAGE);
}
if (!test.filename) {
return {
...test,
filename: getFilename(languageOptions.parserOptions),
languageOptions: {
...languageOptions,
parserOptions: {
// Re-running simulates --fix mode, which implies an isolated program
// (i.e. parseAndGenerateServicesCalls[test.filename] > 1).
disallowAutomaticSingleRunInference: true,
...languageOptions.parserOptions,
},
return {
...test,
filename: getFilename(test.filename, languageOptions.parserOptions),
languageOptions: {
...languageOptions,
parserOptions: {
// Re-running simulates --fix mode, which implies an isolated program
// (i.e. parseAndGenerateServicesCalls[test.filename] > 1).
disallowAutomaticSingleRunInference: true,
...languageOptions.parserOptions,
},
};
}
return test;
},
};
};

const normalizedTests = {
Expand Down Expand Up @@ -707,16 +714,7 @@ export class RuleTester extends TestFramework {
},
},
});
messages = this.#linter.verify(
code,
// ESLint uses an internal FlatConfigArray that extends @humanwhocodes/config-array.
// Linter uses a typeof getConfig === "function" check.
// We mock out that check here to force it not to use Linter's cwd as basePath.
Object.assign([], {
getConfig: () => actualConfig,
}),
filename,
);
messages = this.#linter.verify(code, actualConfig, filename);
} finally {
SourceCode.prototype.applyInlineConfig = applyInlineConfig;
SourceCode.prototype.applyLanguageOptions = applyLanguageOptions;
Expand Down Expand Up @@ -917,6 +915,7 @@ export class RuleTester extends TestFramework {

const hasMessageOfThisRule = messages.some(m => m.ruleId === ruleName);

// console.log({ messages });
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// console.log({ messages });

Copy link
Member Author

Choose a reason for hiding this comment

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

oop will do this on the branch

for (let i = 0, l = item.errors.length; i < l; i++) {
const error = item.errors[i];
const message = messages[i];
Expand Down
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5640,7 +5640,6 @@ __metadata:
cross-env: ^7.0.3
cross-fetch: "*"
eslint: "*"
espree: ^10.0.1
graphemer: ^1.4.0
ignore: ^5.3.1
jest: 29.7.0
Expand Down
Loading