Skip to content

Commit

Permalink
feat: use eslint-compat-utils (#277)
Browse files Browse the repository at this point in the history
* feat: use eslint-compat-utils

* Create chilled-nails-mix.md

* fix

* fix

* fix
  • Loading branch information
ota-meshi authored Nov 29, 2023
1 parent b1db9cd commit 8e785db
Show file tree
Hide file tree
Showing 30 changed files with 192 additions and 63 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-nails-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-astro": minor
---

feat: use eslint-compat-utils
33 changes: 33 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,39 @@ 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",
},
],
},
overrides: [
{
Expand Down
7 changes: 4 additions & 3 deletions docs-build/shim/fs.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint require-jsdoc:0 -- shim */

export {}
export default {}
export function existsSync() {
return true
}
export default { existsSync }
18 changes: 13 additions & 5 deletions docs-build/shim/path.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
/* eslint require-jsdoc:0 -- shim */

function extname(p) {
export function extname(p) {
return /\.[\w$-]+$/iu.exec(p)[0]
}

function join(...args) {
export function join(...args) {
return args.join("/")
}

function isAbsolute() {
export function isAbsolute() {
return false
}

export { extname, join, isAbsolute }
export default { extname, join, isAbsolute }
export function basename(p, ext) {
const base = p.split("/").pop() || p
return base.endsWith(ext) ? base.slice(0, -ext.length) : base
}

export function dirname(p) {
return p.split("/").slice(0, -1).join("/") || p
}

export default { extname, join, isAbsolute, basename, dirname }
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"@jridgewell/sourcemap-codec": "^1.4.14",
"@typescript-eslint/types": "^5.25.0",
"astro-eslint-parser": "^0.16.0",
"eslint-compat-utils": "^0.1.2",
"postcss": "^8.4.14",
"postcss-selector-parser": "^6.0.10"
},
Expand Down
4 changes: 3 additions & 1 deletion src/a11y/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ASTNode } from "../types-for-node"
import { createRule } from "../utils"
import { a11yRuleKeys } from "./keys"
import { getAttributeName } from "../utils/ast-utils"
import { getSourceCode } from "../utils/compat"

const TYPE_MAP: Partial<Record<ASTNode["type"], ASTNode["type"]>> = {
AstroRawText: "JSXText",
Expand Down Expand Up @@ -94,7 +95,8 @@ function defineWrapperListener(
coreRule: PluginRuleModule,
context: RuleContext,
): RuleListener {
if (!context.parserServices.isAstro) {
const sourceCode = getSourceCode(context)
if (!sourceCode.parserServices.isAstro) {
return {}
}
const listener = coreRule.create(context)
Expand Down
5 changes: 3 additions & 2 deletions src/rules/no-conflict-set-directives.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AST } from "astro-eslint-parser"
import { createRule } from "../utils"
import { getAttributeName } from "../utils/ast-utils"
import { getSourceCode } from "../utils/compat"

export default createRule("no-conflict-set-directives", {
meta: {
Expand All @@ -16,10 +17,10 @@ export default createRule("no-conflict-set-directives", {
type: "problem",
},
create(context) {
if (!context.parserServices.isAstro) {
const sourceCode = getSourceCode(context)
if (!sourceCode.parserServices.isAstro) {
return {}
}
const sourceCode = context.getSourceCode()

return {
JSXElement(node) {
Expand Down
8 changes: 5 additions & 3 deletions src/rules/no-deprecated-astro-canonicalurl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { READ, ReferenceTracker } from "@eslint-community/eslint-utils"
import { createRule } from "../utils"
import { getSourceCode } from "../utils/compat"

export default createRule("no-deprecated-astro-canonicalurl", {
meta: {
Expand All @@ -16,13 +17,14 @@ export default createRule("no-deprecated-astro-canonicalurl", {
type: "problem",
},
create(context) {
if (!context.parserServices.isAstro) {
const sourceCode = getSourceCode(context)
if (!sourceCode.parserServices.isAstro) {
return {}
}

return {
"Program:exit"() {
const tracker = new ReferenceTracker(context.getScope())
"Program:exit"(node) {
const tracker = new ReferenceTracker(sourceCode.getScope(node))
for (const { node, path } of tracker.iterateGlobalReferences({
Astro: {
canonicalURL: { [READ]: true },
Expand Down
8 changes: 5 additions & 3 deletions src/rules/no-deprecated-astro-fetchcontent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { READ, ReferenceTracker } from "@eslint-community/eslint-utils"
import { createRule } from "../utils"
import { getSourceCode } from "../utils/compat"

export default createRule("no-deprecated-astro-fetchcontent", {
meta: {
Expand All @@ -17,13 +18,14 @@ export default createRule("no-deprecated-astro-fetchcontent", {
fixable: "code",
},
create(context) {
if (!context.parserServices.isAstro) {
const sourceCode = getSourceCode(context)
if (!sourceCode.parserServices.isAstro) {
return {}
}

return {
"Program:exit"() {
const tracker = new ReferenceTracker(context.getScope())
"Program:exit"(node) {
const tracker = new ReferenceTracker(sourceCode.getScope(node))
for (const { node, path } of tracker.iterateGlobalReferences({
Astro: {
fetchContent: { [READ]: true },
Expand Down
8 changes: 5 additions & 3 deletions src/rules/no-deprecated-astro-resolve.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { READ, ReferenceTracker } from "@eslint-community/eslint-utils"
import { createRule } from "../utils"
import { getSourceCode } from "../utils/compat"

export default createRule("no-deprecated-astro-resolve", {
meta: {
Expand All @@ -15,13 +16,14 @@ export default createRule("no-deprecated-astro-resolve", {
type: "problem",
},
create(context) {
if (!context.parserServices.isAstro) {
const sourceCode = getSourceCode(context)
if (!sourceCode.parserServices.isAstro) {
return {}
}

return {
"Program:exit"() {
const tracker = new ReferenceTracker(context.getScope())
"Program:exit"(node) {
const tracker = new ReferenceTracker(sourceCode.getScope(node))
for (const { node, path } of tracker.iterateGlobalReferences({
Astro: {
resolve: { [READ]: true },
Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-deprecated-getentrybyslug.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createRule } from "../utils"
import type { TSESTree } from "@typescript-eslint/types"
import { getSourceCode } from "../utils/compat"

export default createRule("no-deprecated-getentrybyslug", {
meta: {
Expand All @@ -15,7 +16,8 @@ export default createRule("no-deprecated-getentrybyslug", {
type: "problem",
},
create(context) {
if (!context.parserServices.isAstro) {
const sourceCode = getSourceCode(context)
if (!sourceCode.parserServices.isAstro) {
return {}
}

Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-set-html-directive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AST } from "astro-eslint-parser"
import { createRule } from "../utils"
import { getAttributeName } from "../utils/ast-utils"
import { getSourceCode } from "../utils/compat"

export default createRule("no-set-html-directive", {
meta: {
Expand All @@ -16,7 +17,8 @@ export default createRule("no-set-html-directive", {
type: "suggestion",
},
create(context) {
if (!context.parserServices.isAstro) {
const sourceCode = getSourceCode(context)
if (!sourceCode.parserServices.isAstro) {
return {}
}

Expand Down
5 changes: 3 additions & 2 deletions src/rules/no-set-text-directive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AST } from "astro-eslint-parser"
import { createRule } from "../utils"
import { getAttributeName } from "../utils/ast-utils"
import { getSourceCode } from "../utils/compat"

export default createRule("no-set-text-directive", {
meta: {
Expand All @@ -17,7 +18,8 @@ export default createRule("no-set-text-directive", {
fixable: "code",
},
create(context) {
if (!context.parserServices.isAstro) {
const sourceCode = getSourceCode(context)
if (!sourceCode.parserServices.isAstro) {
return {}
}

Expand All @@ -43,7 +45,6 @@ export default createRule("no-set-text-directive", {
) {
return
}
const sourceCode = context.getSourceCode()

const valueText =
attr.type === "AstroTemplateLiteralAttribute"
Expand Down
8 changes: 5 additions & 3 deletions src/rules/no-unused-css-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "../utils/ast-utils"
import type { StyleContentCSS } from "../utils/transform"
import { getStyleContentCSS } from "../utils/transform"
import { getSourceCode } from "../utils/compat"

type JSXElementTreeNode = {
parent: JSXElementTreeNode | RootJSXElementTreeNode
Expand Down Expand Up @@ -43,7 +44,8 @@ export default createRule("no-unused-css-selector", {
type: "problem",
},
create(context) {
if (!context.parserServices.isAstro) {
const sourceCode = getSourceCode(context)
if (!sourceCode.parserServices.isAstro) {
return {}
}
const styles: AST.JSXElement[] = []
Expand Down Expand Up @@ -100,7 +102,6 @@ export default createRule("no-unused-css-selector", {
function reportSelector(start: number, selector: string) {
const remapStart = css.remap(start)
const remapEnd = css.remap(start + selector.length)
const sourceCode = context.getSourceCode()
context.report({
loc: {
start: sourceCode.getLocFromIndex(remapStart),
Expand Down Expand Up @@ -982,9 +983,10 @@ function* extractClassListFromExpression(
}
return
}
const sourceCode = getSourceCode(context)
const staticValue = getStaticValue(
node as never,
context.getSourceCode().scopeManager.globalScope!,
sourceCode.scopeManager.globalScope!,
)
if (staticValue) {
yield* extractClassListFromUnknown(staticValue.value)
Expand Down
6 changes: 4 additions & 2 deletions src/rules/no-unused-define-vars-in-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getPropertyName } from "@eslint-community/eslint-utils"
import { createRule } from "../utils"
import { getAttributeName } from "../utils/ast-utils"
import { iterateCSSVars } from "../utils/style"
import { getSourceCode } from "../utils/compat"

export default createRule("no-unused-define-vars-in-style", {
meta: {
Expand All @@ -20,7 +21,8 @@ export default createRule("no-unused-define-vars-in-style", {
type: "problem",
},
create(context) {
if (!context.parserServices.isAstro) {
const sourceCode = getSourceCode(context)
if (!sourceCode.parserServices.isAstro) {
return {}
}

Expand Down Expand Up @@ -63,7 +65,7 @@ export default createRule("no-unused-define-vars-in-style", {
)
.map((prop) => ({
prop,
name: getPropertyName(prop, context.getScope()),
name: getPropertyName(prop, sourceCode.getScope(node)),
}))
.filter((data): data is typeof data & { name: string } =>
Boolean(data.name),
Expand Down
4 changes: 3 additions & 1 deletion src/rules/prefer-class-list-directive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AST } from "astro-eslint-parser"
import { createRule } from "../utils"
import { getAttributeName } from "../utils/ast-utils"
import { getSourceCode } from "../utils/compat"

export default createRule("prefer-class-list-directive", {
meta: {
Expand All @@ -19,7 +20,8 @@ export default createRule("prefer-class-list-directive", {
type: "suggestion",
},
create(context) {
if (!context.parserServices.isAstro) {
const sourceCode = getSourceCode(context)
if (!sourceCode.parserServices.isAstro) {
return {}
}

Expand Down
5 changes: 3 additions & 2 deletions src/rules/prefer-object-class-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
isStringLiteral,
needParentheses,
} from "../utils/ast-utils"
import { getSourceCode } from "../utils/compat"

export default createRule("prefer-object-class-list", {
meta: {
Expand All @@ -32,10 +33,10 @@ export default createRule("prefer-object-class-list", {
type: "suggestion",
},
create(context) {
if (!context.parserServices.isAstro) {
const sourceCode = getSourceCode(context)
if (!sourceCode.parserServices.isAstro) {
return {}
}
const sourceCode = context.getSourceCode()

type Expr = {
not?: true
Expand Down
Loading

0 comments on commit 8e785db

Please sign in to comment.