Skip to content

Commit

Permalink
feat: add support for eslint v9 (#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Jan 18, 2024
1 parent 9cf5efb commit b72b97b
Show file tree
Hide file tree
Showing 94 changed files with 450 additions and 302 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-crews-brush.md
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": patch
---

feat: add support for eslint v9
5 changes: 3 additions & 2 deletions .eslintrc.js
Expand Up @@ -94,7 +94,7 @@ module.exports = {
},
{
selector: 'variable',
format: ['camelCase', 'UPPER_CASE'],
format: ['camelCase', 'UPPER_CASE', 'PascalCase'],
leadingUnderscore: 'allow',
trailingUnderscore: 'allow'
},
Expand Down Expand Up @@ -156,7 +156,8 @@ module.exports = {
{
files: ['tests/**'],
rules: {
'@typescript-eslint/no-misused-promises': 'off'
'@typescript-eslint/no-misused-promises': 'off',
'@typescript-eslint/no-require-imports': 'off'
}
},
{
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/NodeCI.yml
Expand Up @@ -41,6 +41,11 @@ jobs:
os: [ubuntu-latest]
eslint: [7, 8]
node: [20]
include:
# On next ESLint version
- eslint: ^9.0.0-0
node: 20
os: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Expand Up @@ -6,5 +6,6 @@
"source.fixAll": "explicit",
"source.fixAll.stylelint": "explicit"
},
"svelte.plugin.typescript.diagnostics.enable": false
"svelte.plugin.typescript.diagnostics.enable": false,
"typescript.tsdk": "node_modules/typescript/lib"
}
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -57,7 +57,7 @@
"version:ci": "env-cmd -e version-ci pnpm run update && changeset version"
},
"peerDependencies": {
"eslint": "^7.0.0 || ^8.0.0-0",
"eslint": "^7.0.0 || ^8.0.0-0 || ^9.0.0-0",
"svelte": "^3.37.0 || ^4.0.0 || ^5.0.0-next.33"
},
"peerDependenciesMeta": {
Expand All @@ -69,7 +69,7 @@
"@eslint-community/eslint-utils": "^4.2.0",
"@jridgewell/sourcemap-codec": "^1.4.14",
"debug": "^4.3.1",
"eslint-compat-utils": "^0.4.0",
"eslint-compat-utils": "^0.4.1",
"esutils": "^2.0.3",
"known-css-properties": "^0.29.0",
"postcss": "^8.4.5",
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-spaces-around-equal-signs-in-attribute.ts
Expand Up @@ -9,7 +9,7 @@ export default createRule('no-spaces-around-equal-signs-in-attribute', {
recommended: false,
conflictWithPrettier: true
},
schema: {},
schema: [],
fixable: 'whitespace',
messages: {
noSpaces: 'Unexpected spaces found around equal signs.'
Expand Down
2 changes: 1 addition & 1 deletion src/rules/prefer-destructured-store-props.ts
Expand Up @@ -186,7 +186,7 @@ export default createRule('prefer-destructured-store-props', {
// dynamic accesses like {$foo[bar]}
!node.computed
) {
for (const variable of findReactiveVariable(node.object, node.property.name)) {
for (const variable of new Set(findReactiveVariable(node.object, node.property.name))) {
suggest.push({
messageId: 'fixUseVariable',
data: {
Expand Down
18 changes: 12 additions & 6 deletions src/utils/eslint-core.ts
Expand Up @@ -76,11 +76,17 @@ let ruleMap: Map<string, RuleModule> | null = null;
* Get the core rule implementation from the rule name
*/
export function getCoreRule(ruleName: string): RuleModule {
let map: Map<string, RuleModule>;
if (ruleMap) {
map = ruleMap;
} else {
ruleMap = map = (new Linter() as any).getRules();
try {
const map: Map<string, RuleModule> = ruleMap
? ruleMap
: (ruleMap = (new Linter() as any).getRules());
return map.get(ruleName)!;
} catch {
// getRules() is no longer available in flat config.
}
return map.get(ruleName)!;

// eslint-disable-next-line @typescript-eslint/no-require-imports , @typescript-eslint/no-var-requires -- Ignore
const { builtinRules } = require('eslint/use-at-your-own-risk');
ruleMap = builtinRules;
return builtinRules.get(ruleName) || null;
}
@@ -0,0 +1,5 @@
{
"rules": {
"no-var": "off"
}
}
@@ -0,0 +1,3 @@
{
"options": ["functions", { "blockScopedFunctions": "disallow" }]
}
@@ -0,0 +1,3 @@
{
"eslint": "^9.0.0-0"
}
@@ -1,3 +1,3 @@
{
"options": ["both"]
"options": ["both", { "blockScopedFunctions": "disallow" }]
}
@@ -0,0 +1,3 @@
{
"eslint": "^9.0.0-0"
}
Expand Up @@ -3,26 +3,26 @@
column: 3
suggestions: null
- message: Move variable declaration to program root.
line: 6
line: 5
column: 11
suggestions: null
- message: Move variable declaration to program root.
line: 8
line: 6
column: 31
suggestions: null
- message: Move function declaration to program root.
line: 10
line: 8
column: 3
suggestions: null
- message: Move variable declaration to function body root.
line: 13
line: 10
column: 5
suggestions: null
- message: Move function declaration to program root.
line: 18
line: 15
column: 3
suggestions: null
- message: Move variable declaration to function body root.
line: 20
line: 16
column: 13
suggestions: null
Expand Up @@ -2,21 +2,17 @@
if (test) {
function doSomething() {}
}
// eslint-disable-next-line no-var -- test
if (foo) var a;
// eslint-disable-next-line no-var -- test
if (foo) /* some comments */ var a;
if (foo) {
function f() {
if (bar) {
// eslint-disable-next-line no-var -- test
var a;
}
}
}
if (foo) {
function f() {
// eslint-disable-next-line no-var -- test
if (bar) var a;
}
}
Expand Down
Expand Up @@ -3,10 +3,10 @@
column: 3
suggestions: null
- message: Move function declaration to program root.
line: 10
line: 8
column: 3
suggestions: null
- message: Move function declaration to program root.
line: 18
line: 15
column: 3
suggestions: null
Expand Up @@ -2,21 +2,17 @@
if (test) {
function doSomething() {}
}
// eslint-disable-next-line no-var -- test
if (foo) var a;
// eslint-disable-next-line no-var -- test
if (foo) /* some comments */ var a;
if (foo) {
function f() {
if (bar) {
// eslint-disable-next-line no-var -- test
var a;
}
}
}
if (foo) {
function f() {
// eslint-disable-next-line no-var -- test
if (bar) var a;
}
}
Expand Down
@@ -0,0 +1,3 @@
{
"options": ["functions"]
}
@@ -0,0 +1,3 @@
{
"eslint": "<=8"
}
@@ -0,0 +1,3 @@
{
"options": ["both"]
}
@@ -0,0 +1,3 @@
{
"eslint": "<=8"
}
@@ -0,0 +1,28 @@
- message: Move function declaration to program root.
line: 3
column: 3
suggestions: null
- message: Move variable declaration to program root.
line: 5
column: 11
suggestions: null
- message: Move variable declaration to program root.
line: 6
column: 31
suggestions: null
- message: Move function declaration to program root.
line: 8
column: 3
suggestions: null
- message: Move variable declaration to function body root.
line: 10
column: 5
suggestions: null
- message: Move function declaration to program root.
line: 15
column: 3
suggestions: null
- message: Move variable declaration to function body root.
line: 16
column: 13
suggestions: null
@@ -0,0 +1,19 @@
<script>
if (test) {
function doSomething() {}
}
if (foo) var a;
if (foo) /* some comments */ var a;
if (foo) {
function f() {
if (bar) {
var a;
}
}
}
if (foo) {
function f() {
if (bar) var a;
}
}
</script>
@@ -0,0 +1,12 @@
- message: Move function declaration to program root.
line: 3
column: 3
suggestions: null
- message: Move function declaration to program root.
line: 8
column: 3
suggestions: null
- message: Move function declaration to program root.
line: 15
column: 3
suggestions: null
@@ -0,0 +1,19 @@
<script>
if (test) {
function doSomething() {}
}
if (foo) var a;
if (foo) /* some comments */ var a;
if (foo) {
function f() {
if (bar) {
var a;
}
}
}
if (foo) {
function f() {
if (bar) var a;
}
}
</script>
Expand Up @@ -95,23 +95,6 @@
bar.foo: {b.foo}
bar.baz: {$store.bar.baz}
baz.foo: {bbb.foo}
baz.bar: {bbb.bar}
- desc: Using the predefined reactive variable bbb
messageId: fixUseVariable
output: |
<script>
import store from './store.js';
$: ({ foo, bar: b } = $store);
$: bbb = $store.baz;
</script>
foo.bar: {foo.bar}
foo.baz: {$store.foo.baz}
bar.foo: {b.foo}
bar.baz: {$store.bar.baz}
baz.foo: {bbb.foo}
baz.bar: {bbb.bar}
- desc: 'Using destructuring like $: ({ baz } = $store); will run faster'
Expand Down
8 changes: 5 additions & 3 deletions tests/fixtures/rules/valid-compile/valid/babel/_config.json
@@ -1,5 +1,7 @@
{
"parserOptions": {
"parser": "@babel/eslint-parser"
}
"languageOptions": {
"parserOptions": {
"parser": "@babel/eslint-parser"
}
}
}
4 changes: 2 additions & 2 deletions tests/src/configs/all.ts
@@ -1,12 +1,12 @@
import assert from 'assert';
import eslint from 'eslint';
import plugin from '../../../src/index';
import { LegacyESLint } from '../../utils/eslint-compat';

describe('`all` config', () => {
it('`all` config should work. ', async () => {
const code = `<script>const a = 1, b = 2;</script>{@html a+b}`;

const linter = new eslint.ESLint({
const linter = new LegacyESLint({
plugins: {
svelte: plugin as never
},
Expand Down
10 changes: 6 additions & 4 deletions tests/src/integration/no-unused-vars.ts
@@ -1,10 +1,12 @@
import { RuleTester, Linter } from 'eslint';
import type { Rule } from 'eslint';
import { getCoreRule } from '../../../src/utils/eslint-core';
import { RuleTester } from '../../utils/eslint-compat';

describe('Integration test for no-unused-vars', () => {
const ruleNoUnusedVars = new Linter().getRules().get('no-unused-vars')!;
const ruleNoUnusedVars = getCoreRule('no-unused-vars') as unknown as Rule.RuleModule;
const tester = new RuleTester({
parser: require.resolve('svelte-eslint-parser'),
parserOptions: {
languageOptions: {
parser: require('svelte-eslint-parser'),
ecmaVersion: 2020,
sourceType: 'module'
}
Expand Down

0 comments on commit b72b97b

Please sign in to comment.