Skip to content

test: use Linter and ESLint directly from eslint in tests #1177

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 3 commits into from
Mar 31, 2025
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
5 changes: 5 additions & 0 deletions .changeset/hungry-days-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': patch
---

test: use ESLint and Linter directly from eslint in tests
14 changes: 9 additions & 5 deletions packages/eslint-plugin-svelte/tests/src/configs/all.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import assert from 'assert';
import semver from 'semver';
import plugin from '../../../src/index.js';
import { ESLint } from '../../utils/eslint-compat.js';
import { loadESLint, type ESLint as ESLintClass } from 'eslint';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

as far as i understand, ESLint here is the flat ESLint already. but when we force install 8.x in CI, it will be the legacy one at runtime. which is why we can't just import {ESLint} from 'eslint'


describe('`all` config', () => {
let ESLint: typeof ESLintClass;

before(async () => {
ESLint = await loadESLint({ useFlatConfig: true });
});

it('`all` config should work. ', async () => {
if (semver.satisfies(ESLint.version, '<8.0.0')) return;
const code = `<script>const a = 1, b = 2;</script>{@html a+b}`;

const linter = new ESLint({
overrideConfigFile: true as never,
overrideConfig: plugin.configs['flat/all'] as never
overrideConfigFile: true,
overrideConfig: plugin.configs['flat/all']
});
const result = await linter.lintText(code, { filePath: 'test.svelte' });
const messages = result[0].messages;
Expand Down
14 changes: 9 additions & 5 deletions packages/eslint-plugin-svelte/tests/src/configs/base.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import assert from 'assert';
import semver from 'semver';
import plugin from '../../../src/index.js';
import { ESLint } from '../../utils/eslint-compat.js';
import { loadESLint, type ESLint as ESLintClass } from 'eslint';

describe('`base` config', () => {
let ESLint: typeof ESLintClass;

before(async () => {
ESLint = await loadESLint({ useFlatConfig: true });
});

it('`base` config should work. ', async () => {
if (semver.satisfies(ESLint.version, '<8.0.0')) return;
const code = `<script>const a = 1, b = 2;</script>
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html a+b}
{@html a+b}`;
const linter = new ESLint({
overrideConfigFile: true as never,
overrideConfigFile: true,
overrideConfig: [
...plugin.configs['flat/base'],
{
rules: {
'svelte/no-at-html-tags': 'error'
}
}
] as never
]
});
const result = await linter.lintText(code, { filePath: 'test.svelte' });
const messages = result[0].messages;
Expand Down
14 changes: 9 additions & 5 deletions packages/eslint-plugin-svelte/tests/src/configs/recommended.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import assert from 'assert';
import semver from 'semver';
import plugin from '../../../src/index.js';
import { ESLint } from '../../utils/eslint-compat.js';
import { loadESLint, type ESLint as ESLintClass } from 'eslint';

describe('`recommended` config', () => {
let ESLint: typeof ESLintClass;

before(async () => {
ESLint = await loadESLint({ useFlatConfig: true });
});

it('`recommended` config should work. ', async () => {
if (semver.satisfies(ESLint.version, '<8.0.0')) return;
const code = `<script>const a = 1, b = 2;</script>{@html a+b}`;

const linter = new ESLint({
overrideConfigFile: true as never,
overrideConfig: plugin.configs['flat/recommended'] as never
overrideConfigFile: true,
overrideConfig: plugin.configs['flat/recommended']
});
const result = await linter.lintText(code, { filePath: 'test.svelte' });
const messages = result[0].messages;
Expand Down
92 changes: 49 additions & 43 deletions packages/eslint-plugin-svelte/tests/src/rules/comment-directive.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
import assert from 'assert';
import plugin from '../../../src/index.js';
import { ESLint } from '../../utils/eslint-compat.js';
import { loadESLint, type ESLint as ESLintClass } from 'eslint';
import * as svelteParser from 'svelte-eslint-parser';

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

// Initialize linter.
const linter = new ESLint({
// @ts-expect-error -- Type error?
baseConfig: {
languageOptions: {
parser: svelteParser,
ecmaVersion: 2020
},
plugins: {
svelte: plugin
},
rules: {
'no-undef': 'error',
'space-infix-ops': 'error',
'svelte/no-at-html-tags': 'error',
'svelte/comment-directive': 'error'
},
processor: 'svelte/svelte',
files: ['**']
},
overrideConfigFile: true
});

describe('comment-directive', () => {
let ESLint: typeof ESLintClass;
let linter: InstanceType<typeof ESLintClass>;

before(async () => {
ESLint = await loadESLint({ useFlatConfig: true });
linter = new ESLint({
baseConfig: {
languageOptions: {
parser: svelteParser,
ecmaVersion: 2020
},
plugins: {
svelte: plugin
},
rules: {
'no-undef': 'error',
'space-infix-ops': 'error',
'svelte/no-at-html-tags': 'error',
'svelte/comment-directive': 'error'
},
processor: 'svelte/svelte',
files: ['**']
},
overrideConfigFile: true
});
});

describe('eslint-disable/eslint-enable', () => {
it('disable all rules if <!-- eslint-disable -->', async () => {
const code = `
Expand Down Expand Up @@ -351,27 +355,29 @@ describe('comment-directive', () => {
});

describe('reportUnusedDisableDirectives', () => {
const linter = new ESLint({
// @ts-expect-error -- Type error?
baseConfig: {
languageOptions: {
parser: svelteParser,
ecmaVersion: 2020
},
plugins: {
svelte: plugin
},
rules: {
'no-unused-vars': 'error',
'svelte/comment-directive': ['error', { reportUnusedDisableDirectives: true }],
'svelte/no-at-html-tags': 'error',
'svelte/no-at-debug-tags': 'error'
before(() => {
linter = new ESLint({
baseConfig: {
languageOptions: {
parser: svelteParser,
ecmaVersion: 2020
},
plugins: {
svelte: plugin
},
rules: {
'no-unused-vars': 'error',
'svelte/comment-directive': ['error', { reportUnusedDisableDirectives: true }],
'svelte/no-at-html-tags': 'error',
'svelte/no-at-debug-tags': 'error'
},
files: ['**'],
processor: 'svelte/svelte'
},
files: ['**'],
processor: 'svelte/svelte'
},
overrideConfigFile: true
overrideConfigFile: true
});
});

it('report unused <!-- eslint-disable -->', async () => {
const code = `
<!-- eslint-disable -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import assert from 'assert';
import plugin from '../../../src/index.js';
import { ESLint } from '../../utils/eslint-compat.js';
import { loadESLint, type ESLint as ESLintClass } from 'eslint';
import * as svelteParser from 'svelte-eslint-parser';

describe('ignore-warnings', () => {
let ESLint: typeof ESLintClass;

before(async () => {
ESLint = await loadESLint({ useFlatConfig: true });
});

it('disable rules if ignoreWarnings: [ruleName]', async () => {
const code = `
{@html a+b}
Expand Down
8 changes: 1 addition & 7 deletions packages/eslint-plugin-svelte/tests/utils/eslint-compat.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import { getLegacyESLint, getESLint } from 'eslint-compat-utils/eslint';
import { getLinter } from 'eslint-compat-utils/linter';
import { getRuleTester, getRuleIdPrefix } from 'eslint-compat-utils/rule-tester';
import { getRuleTester } from 'eslint-compat-utils/rule-tester';

export const LegacyESLint = getLegacyESLint();
export const ESLint = getESLint();
export const RuleTester = getRuleTester();
export const TEST_RULE_ID_PREFIX = getRuleIdPrefix();
export const Linter = getLinter();
2 changes: 1 addition & 1 deletion packages/eslint-plugin-svelte/tests/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { applyFixes } from './source-code-fixer.js';
import { parse as parseYaml, stringify as stringifyYaml } from 'yaml';
import semver from 'semver';
import { writeAndFormat } from '../../tools/lib/write.js';
import { Linter } from './eslint-compat.js';
import { Linter } from 'eslint';
import * as svelteParser from 'svelte-eslint-parser';
import * as typescriptParser from '@typescript-eslint/parser';
import Module from 'module';
Expand Down
Loading