Skip to content

Commit

Permalink
feat: use eslint-compat-utils (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Nov 6, 2023
1 parent ff28fd3 commit ef5f965
Show file tree
Hide file tree
Showing 51 changed files with 187 additions and 77 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-jokes-reflect.md
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": minor
---

feat: use eslint-compat-utils
13 changes: 13 additions & 0 deletions .eslintrc.js
Expand Up @@ -133,6 +133,19 @@ module.exports = {
}
]
}
],
'no-restricted-properties': [
'error',
{ object: 'context', property: 'getSourceCode', message: 'Use src/utils/compat.ts' },
{ object: 'context', property: 'getFilename', message: 'Use src/utils/compat.ts' },
{
object: 'context',
property: 'getPhysicalFilename',
message: 'Use src/utils/compat.ts'
},
{ object: 'context', property: 'getCwd', message: 'Use src/utils/compat.ts' },
{ object: 'context', property: 'getScope', message: 'Use src/utils/compat.ts' },
{ object: 'context', property: 'parserServices', message: 'Use src/utils/compat.ts' }
]
}
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -69,6 +69,7 @@
"@eslint-community/eslint-utils": "^4.2.0",
"@jridgewell/sourcemap-codec": "^1.4.14",
"debug": "^4.3.1",
"eslint-compat-utils": "^0.1.2",
"esutils": "^2.0.3",
"known-css-properties": "^0.29.0",
"postcss": "^8.4.5",
Expand Down
3 changes: 2 additions & 1 deletion src/rules/@typescript-eslint/no-unnecessary-condition.ts
Expand Up @@ -22,6 +22,7 @@ import {
isTupleType
} from '../../utils/ts-utils';
import type { TS, TSTools } from '../../utils/ts-utils';
import { getSourceCode } from '../../utils/compat';

/**
* Returns all types of a union type or an array containing `type` itself if it's no union type.
Expand Down Expand Up @@ -156,7 +157,7 @@ export default createRule('@typescript-eslint/no-unnecessary-condition', {

const { service, ts } = tools;
const checker = service.program.getTypeChecker();
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const compilerOptions = service.program.getCompilerOptions();
const isStrictNullChecks = compilerOptions.strict
? compilerOptions.strictNullChecks !== false
Expand Down
3 changes: 2 additions & 1 deletion src/rules/block-lang.ts
@@ -1,6 +1,7 @@
import { createRule } from '../utils';
import { getLangValue } from '../utils/ast-utils';
import type { SvelteScriptElement, SvelteStyleElement } from 'svelte-eslint-parser/lib/ast';
import { getSourceCode } from '../utils/compat';

export default createRule('block-lang', {
meta: {
Expand Down Expand Up @@ -56,7 +57,7 @@ export default createRule('block-lang', {
type: 'suggestion'
},
create(context) {
if (!context.parserServices.isSvelte) {
if (!getSourceCode(context).parserServices.isSvelte) {
return {};
}
const enforceScriptPresent: boolean = context.options[0]?.enforceScriptPresent ?? false;
Expand Down
5 changes: 3 additions & 2 deletions src/rules/comment-directive.ts
Expand Up @@ -2,6 +2,7 @@ import type { AST } from 'svelte-eslint-parser';
import { getShared } from '../shared';
import type { CommentDirectives } from '../shared/comment-directives';
import { createRule } from '../utils';
import { getFilename, getSourceCode } from '../utils/compat';

type RuleAndLocation = {
ruleId: string;
Expand Down Expand Up @@ -53,7 +54,7 @@ export default createRule('comment-directive', {
type: 'problem'
},
create(context) {
const shared = getShared(context.getFilename());
const shared = getShared(getFilename(context));
if (!shared) return {};
const options = context.options[0] || {};
const reportUnusedDisableDirectives = Boolean(options.reportUnusedDisableDirectives);
Expand All @@ -62,7 +63,7 @@ export default createRule('comment-directive', {
reportUnusedDisableDirectives
});

const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

/**
* Parse a given comment.
Expand Down
3 changes: 2 additions & 1 deletion src/rules/first-attribute-linebreak.ts
@@ -1,5 +1,6 @@
import type { AST } from 'svelte-eslint-parser';
import { createRule } from '../utils';
import { getSourceCode } from '../utils/compat';

export default createRule('first-attribute-linebreak', {
meta: {
Expand Down Expand Up @@ -29,7 +30,7 @@ export default createRule('first-attribute-linebreak', {
create(context) {
const multiline: 'below' | 'beside' = context.options[0]?.multiline || 'below';
const singleline: 'below' | 'beside' = context.options[0]?.singleline || 'beside';
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

/**
* Report attribute
Expand Down
3 changes: 2 additions & 1 deletion src/rules/html-quotes.ts
Expand Up @@ -3,6 +3,7 @@ import { createRule } from '../utils';
import type { QuoteAndRange } from '../utils/ast-utils';
import { getMustacheTokens } from '../utils/ast-utils';
import { getAttributeValueQuoteAndRange } from '../utils/ast-utils';
import { getSourceCode } from '../utils/compat';

const QUOTE_CHARS = {
double: '"',
Expand Down Expand Up @@ -48,7 +49,7 @@ export default createRule('html-quotes', {
type: 'layout' // "problem",
},
create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const preferQuote: 'double' | 'single' = context.options[0]?.prefer ?? 'double';
const dynamicQuote = context.options[0]?.dynamic?.quoted ? preferQuote : 'unquoted';
const avoidInvalidUnquotedInHTML = Boolean(
Expand Down
7 changes: 4 additions & 3 deletions src/rules/html-self-closing.ts
@@ -1,6 +1,7 @@
import type { AST } from 'svelte-eslint-parser';
import { createRule } from '../utils';
import { getNodeName, isVoidHtmlElement } from '../utils/ast-utils';
import { getSourceCode } from '../utils/compat';

const TYPE_MESSAGES = {
normal: 'HTML elements',
Expand Down Expand Up @@ -126,9 +127,9 @@ export default createRule('html-self-closing', {
context.report({
node,
loc: {
start: context
.getSourceCode()
.getLocFromIndex(node.startTag.range[1] - (node.startTag.selfClosing ? 2 : 1)),
start: getSourceCode(context).getLocFromIndex(
node.startTag.range[1] - (node.startTag.selfClosing ? 2 : 1)
),
end: node.loc.end
},
messageId: shouldBeClosed ? 'requireClosing' : 'disallowClosing',
Expand Down
5 changes: 3 additions & 2 deletions src/rules/indent-helpers/index.ts
Expand Up @@ -9,6 +9,7 @@ import { isCommentToken } from '@eslint-community/eslint-utils';
import type { AnyToken, IndentOptions } from './commons';
import type { OffsetCalculator } from './offset-context';
import { OffsetContext } from './offset-context';
import { getFilename, getSourceCode } from '../../utils/compat';

type IndentUserOptions = {
indent?: number | 'tab';
Expand Down Expand Up @@ -78,10 +79,10 @@ export function defineVisitor(
context: RuleContext,
defaultOptions: Partial<IndentOptions>
): RuleListener {
if (!context.getFilename().endsWith('.svelte')) return {};
if (!getFilename(context).endsWith('.svelte')) return {};

const options = parseOptions(context.options[0] || {}, defaultOptions);
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const offsets = new OffsetContext({ sourceCode, options });

/**
Expand Down
7 changes: 4 additions & 3 deletions src/rules/infinite-reactive-loop.ts
Expand Up @@ -5,14 +5,15 @@ import { createRule } from '../utils';
import type { RuleContext } from '../types';
import { findVariable } from '../utils/ast-utils';
import { traverseNodes } from 'svelte-eslint-parser';
import { getSourceCode } from '../utils/compat';

/**
* Get usage of `tick`
*/
function extractTickReferences(
context: RuleContext
): { node: TSESTree.CallExpression; name: string }[] {
const referenceTracker = new ReferenceTracker(context.getSourceCode().scopeManager.globalScope!);
const referenceTracker = new ReferenceTracker(getSourceCode(context).scopeManager.globalScope!);
const a = referenceTracker.iterateEsmReferences({
svelte: {
[ReferenceTracker.ESM]: true,
Expand All @@ -35,7 +36,7 @@ function extractTickReferences(
function extractTaskReferences(
context: RuleContext
): { node: TSESTree.CallExpression; name: string }[] {
const referenceTracker = new ReferenceTracker(context.getSourceCode().scopeManager.globalScope!);
const referenceTracker = new ReferenceTracker(getSourceCode(context).scopeManager.globalScope!);
const a = referenceTracker.iterateGlobalReferences({
setTimeout: { [ReferenceTracker.CALL]: true },
setInterval: { [ReferenceTracker.CALL]: true },
Expand Down Expand Up @@ -122,7 +123,7 @@ function isPromiseThenOrCatchBody(node: TSESTree.Node): boolean {
* Get all reactive variable reference.
*/
function getReactiveVariableReferences(context: RuleContext) {
const scopeManager = context.getSourceCode().scopeManager;
const scopeManager = getSourceCode(context).scopeManager;
// Find the top-level (module or global) scope.
// Any variable defined at the top-level (module scope or global scope) can be made reactive.
const toplevelScope =
Expand Down
3 changes: 2 additions & 1 deletion src/rules/max-attributes-per-line.ts
@@ -1,5 +1,6 @@
import type { AST } from 'svelte-eslint-parser';
import { createRule } from '../utils';
import { getSourceCode } from '../utils/compat';

/**
* Check whether the component is declared in a single line or not.
Expand Down Expand Up @@ -57,7 +58,7 @@ export default createRule('max-attributes-per-line', {
create(context) {
const multilineMaximum = context.options[0]?.multiline ?? 1;
const singlelineMaximum = context.options[0]?.singleline ?? 1;
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

/**
* Report attributes
Expand Down
3 changes: 2 additions & 1 deletion src/rules/mustache-spacing.ts
Expand Up @@ -2,6 +2,7 @@ import type { AST } from 'svelte-eslint-parser';
import { isClosingBraceToken, isOpeningBraceToken } from '@eslint-community/eslint-utils';
import { createRule } from '../utils';
import { getMustacheTokens } from '../utils/ast-utils';
import { getSourceCode } from '../utils/compat';
type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};
Expand Down Expand Up @@ -73,7 +74,7 @@ export default createRule('mustache-spacing', {
},
create(context) {
const options = parseOptions(context.options[0]);
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

function verifyBraces(
openingBrace: AST.Token | AST.Comment,
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-dupe-else-if-blocks.ts
Expand Up @@ -2,6 +2,7 @@ import type { AST } from 'svelte-eslint-parser';
import type { TSESTree } from '@typescript-eslint/types';
import { createRule } from '../utils';
import { equalTokens } from '../utils/ast-utils';
import { getSourceCode } from '../utils/compat';

// ------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -81,7 +82,7 @@ export default createRule('no-dupe-else-if-blocks', {
type: 'problem' // "problem",
},
create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

/**
* Determines whether the two given nodes are considered to be equal. In particular, given that the nodes
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-dupe-on-directives.ts
Expand Up @@ -2,6 +2,7 @@ import type { AST } from 'svelte-eslint-parser';
import type { TSESTree } from '@typescript-eslint/types';
import { createRule } from '../utils';
import { equalTokens } from '../utils/ast-utils';
import { getSourceCode } from '../utils/compat';

export default createRule('no-dupe-on-directives', {
meta: {
Expand All @@ -18,7 +19,7 @@ export default createRule('no-dupe-on-directives', {
type: 'problem'
},
create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

const directiveDataMap = new Map<
string, // event type
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-dupe-use-directives.ts
Expand Up @@ -2,6 +2,7 @@ import type { AST } from 'svelte-eslint-parser';
import type { TSESTree } from '@typescript-eslint/types';
import { createRule } from '../utils';
import { equalTokens, getAttributeKeyText } from '../utils/ast-utils';
import { getSourceCode } from '../utils/compat';

export default createRule('no-dupe-use-directives', {
meta: {
Expand All @@ -18,7 +19,7 @@ export default createRule('no-dupe-use-directives', {
type: 'problem'
},
create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

const directiveDataMap = new Map<
string, // key text
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-dynamic-slot-name.ts
Expand Up @@ -6,6 +6,7 @@ import {
getAttributeValueQuoteAndRange,
getStringIfConstant
} from '../utils/ast-utils';
import { getSourceCode } from '../utils/compat';

export default createRule('no-dynamic-slot-name', {
meta: {
Expand All @@ -23,7 +24,7 @@ export default createRule('no-dynamic-slot-name', {
type: 'problem'
},
create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
return {
"SvelteElement[name.name='slot'] > SvelteStartTag.startTag > SvelteAttribute[key.name='name']"(
node: AST.SvelteAttribute
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-extra-reactive-curlies.ts
@@ -1,5 +1,6 @@
import type { TSESTree } from '@typescript-eslint/types';
import { createRule } from '../utils';
import { getSourceCode } from '../utils/compat';

export default createRule('no-extra-reactive-curlies', {
meta: {
Expand All @@ -23,7 +24,7 @@ export default createRule('no-extra-reactive-curlies', {
[`SvelteReactiveStatement > BlockStatement[body.length=1]`]: (
node: TSESTree.BlockStatement
) => {
const source = context.getSourceCode();
const source = getSourceCode(context);

return context.report({
node,
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-immutable-reactive-statements.ts
Expand Up @@ -3,6 +3,7 @@ import { createRule } from '../utils';
import type { Scope, Variable, Reference, Definition } from '@typescript-eslint/scope-manager';
import type { TSESTree } from '@typescript-eslint/types';
import { findVariable, iterateIdentifiers } from '../utils/ast-utils';
import { getSourceCode } from '../utils/compat';

export default createRule('no-immutable-reactive-statements', {
meta: {
Expand All @@ -20,7 +21,7 @@ export default createRule('no-immutable-reactive-statements', {
type: 'suggestion'
},
create(context) {
const scopeManager = context.getSourceCode().scopeManager;
const scopeManager = getSourceCode(context).scopeManager;
const globalScope = scopeManager.globalScope;
const toplevelScope =
globalScope?.childScopes.find((scope) => scope.type === 'module') || globalScope;
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-reactive-functions.ts
@@ -1,6 +1,7 @@
import type { TSESTree } from '@typescript-eslint/types';
import type { AST } from 'svelte-eslint-parser';
import { createRule } from '../utils';
import { getSourceCode } from '../utils/compat';

export default createRule('no-reactive-functions', {
meta: {
Expand Down Expand Up @@ -30,7 +31,7 @@ export default createRule('no-reactive-functions', {
return false;
}

const source = context.getSourceCode();
const source = getSourceCode(context);

return context.report({
node: parent,
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-reactive-literals.ts
@@ -1,5 +1,6 @@
import type { TSESTree } from '@typescript-eslint/types';
import { createRule } from '../utils';
import { getSourceCode } from '../utils/compat';

export default createRule('no-reactive-literals', {
meta: {
Expand Down Expand Up @@ -36,7 +37,7 @@ export default createRule('no-reactive-literals', {
return false;
}

const source = context.getSourceCode();
const source = getSourceCode(context);

return context.report({
node: parent,
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-reactive-reassign.ts
Expand Up @@ -2,6 +2,7 @@ import type { TSESTree } from '@typescript-eslint/types';
import type { AST } from 'svelte-eslint-parser';
import { createRule } from '../utils';
import { getPropertyName } from '@eslint-community/eslint-utils';
import { getSourceCode } from '../utils/compat';

export default createRule('no-reactive-reassign', {
meta: {
Expand Down Expand Up @@ -30,7 +31,7 @@ export default createRule('no-reactive-reassign', {
},
create(context) {
const props = context.options[0]?.props !== false; // default true
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const scopeManager = sourceCode.scopeManager;
const globalScope = scopeManager.globalScope;
const toplevelScope =
Expand Down

0 comments on commit ef5f965

Please sign in to comment.