Skip to content

Commit

Permalink
feat!: use bundler (#360)
Browse files Browse the repository at this point in the history
* feat!: use bundler

* Create proud-poems-grin.md

* fix
  • Loading branch information
ota-meshi committed Apr 26, 2024
1 parent 3626074 commit 80dfa09
Show file tree
Hide file tree
Showing 30 changed files with 332 additions and 189 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-poems-grin.md
@@ -0,0 +1,5 @@
---
"eslint-plugin-astro": major
---

feat!: use bundler
155 changes: 86 additions & 69 deletions README.md

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions docs-build/src/components/RightSidebar/MoreMenu.astro
Expand Up @@ -11,7 +11,6 @@ const showMoreSection = CONFIG.COMMUNITY_INVITE_URL || editHref
editHref && (
<li class:list={[`header-link`, `depth-2`]}>
<a class="edit-on-github" href={editHref} target="_blank">
{/* eslint-disable-next-line astro/jsx-a11y/prefer-tag-over-role -- how to fix it? */}
<svg
aria-hidden="true"
focusable="false"
Expand All @@ -38,7 +37,6 @@ const showMoreSection = CONFIG.COMMUNITY_INVITE_URL || editHref
CONFIG.COMMUNITY_INVITE_URL && (
<li class:list={[`header-link`, `depth-2`]}>
<a href={CONFIG.COMMUNITY_INVITE_URL} target="_blank">
{/* eslint-disable-next-line astro/jsx-a11y/prefer-tag-over-role -- how to fix it? */}
<svg
aria-hidden="true"
focusable="false"
Expand Down
29 changes: 23 additions & 6 deletions docs/user-guide.md
Expand Up @@ -47,7 +47,24 @@ import eslintPluginAstro from 'eslint-plugin-astro';
export default [
// add more generic rule sets here, such as:
// js.configs.recommended,
...eslintPluginAstro.configs['flat/recommended'],
...eslintPluginAstro.configs.recommended,
{
rules: {
// override/add rules settings here, such as:
// "astro/no-set-html-directive": "error"
}
}
];
```

Example **eslint.config.cjs**:

```js
const eslintPluginAstro = require('eslint-plugin-astro');
module.exports = [
// add more generic rule sets here, such as:
// js.configs.recommended,
...eslintPluginAstro.configs['flat/recommended'], // In CommonJS, the `flat/` prefix is required.
{
rules: {
// override/add rules settings here, such as:
Expand All @@ -59,12 +76,12 @@ export default [

This plugin provides configs:

- `*.configs['flat/base']` ... Minimal configuration to enable correct Astro component linting.
- `*.configs['flat/recommended']` ... Above, plus rules to prevent errors or unintended behavior.
- `*.configs['flat/all']` ... Configuration enables all astro rules. It's meant for testing, not for production use because it changes with every minor and major version of the plugin. Use it at your own risk.
- `*.configs['base']` ... Minimal configuration to enable correct Astro component linting.
- `*.configs['recommended']` ... Above, plus rules to prevent errors or unintended behavior.
- `*.configs['all']` ... Configuration enables all astro rules. It's meant for testing, not for production use because it changes with every minor and major version of the plugin. Use it at your own risk.
- Extension of sharable configuration provided by [eslint-plugin-jsx-a11y]. You need to install [eslint-plugin-jsx-a11y] to use it.
- `*.configs['flat/jsx-a11y-recommended']` ... Similar to the [`"plugin:jsx-a11y/recommended"` configuration](https://github.com/jsx-eslint/eslint-plugin-jsx-a11y#rule-strictness-in-different-modes), but with the rules extended for Astro components enabled.
- `*.configs['flat/jsx-a11y-strict']` ... Similar to the [`"plugin:jsx-a11y/strict"` configuration](https://github.com/jsx-eslint/eslint-plugin-jsx-a11y#rule-strictness-in-different-modes), but with the rules extended for Astro components enabled.
- `*.configs['jsx-a11y-recommended']` ... Similar to the [`"plugin:jsx-a11y/recommended"` configuration](https://github.com/jsx-eslint/eslint-plugin-jsx-a11y#rule-strictness-in-different-modes), but with the rules extended for Astro components enabled.
- `*.configs['jsx-a11y-strict']` ... Similar to the [`"plugin:jsx-a11y/strict"` configuration](https://github.com/jsx-eslint/eslint-plugin-jsx-a11y#rule-strictness-in-different-modes), but with the rules extended for Astro components enabled.

See [the rule list](./rules.md) to get the `rules` that this plugin provides.

Expand Down
8 changes: 5 additions & 3 deletions package.json
Expand Up @@ -2,7 +2,8 @@
"name": "eslint-plugin-astro",
"version": "0.34.0",
"description": "ESLint plugin for Astro component",
"main": "lib/index.js",
"main": "lib/index.cjs",
"module": "lib/index.mjs",
"files": [
"lib"
],
Expand All @@ -11,9 +12,9 @@
},
"scripts": {
"prebuild": "npm run -s clean",
"build": "npm run build:meta && npm run build:ts",
"build": "npm run build:meta && npm run build:tsup",
"build:meta": "npm run ts -- ./tools/update-meta.ts",
"build:ts": "tsc --project ./tsconfig.build.json",
"build:tsup": "tsup",
"clean": "rimraf lib .nyc_output dist coverage",
"test": "npm run mocha -- \"tests/src/**/*.ts\" --reporter dot --timeout 60000 && npm run test:type",
"test:type": "tsc --noEmit",
Expand Down Expand Up @@ -124,6 +125,7 @@
"sass": "^1.52.2",
"stylus": "^0.63.0",
"svelte": "^4.0.0",
"tsup": "^8.0.2",
"typescript": "~5.4.0",
"typescript-eslint": "^7.5.0",
"vite-plugin-eslint4b": "^0.4.0"
Expand Down
23 changes: 15 additions & 8 deletions src/a11y/configs.ts
@@ -1,19 +1,15 @@
import { getPluginJsxA11y } from "./load"
import path from "path"
import { a11yConfigKeys } from "./keys"
import flatBase from "../configs/flat/base"
import type { Linter } from "eslint"

/** Build a11y configs */
export function buildConfigs(): Record<string, unknown> {
const basePath = require.resolve("../configs/base")
const baseExtend =
path.extname(`${basePath}`) === ".ts" ? "plugin:astro/base" : basePath

const configs: Record<string, unknown> = {}
export function buildFlatConfigs(): Record<string, Linter.FlatConfig[]> {
const configs: Record<string, Linter.FlatConfig[]> = {}

for (const configName of a11yConfigKeys) {
// flat config
Object.defineProperty(configs, `flat/jsx-a11y-${configName}`, {
Object.defineProperty(configs, `jsx-a11y-${configName}`, {
enumerable: true,
get() {
const base = getPluginJsxA11y()
Expand All @@ -34,6 +30,17 @@ export function buildConfigs(): Record<string, unknown> {
]
},
})
}
return configs
}

/** Build a11y configs */
export function buildLegacyConfigs(): Record<string, Linter.Config> {
const baseExtend = "plugin:astro/base"

const configs: Record<string, Linter.Config> = {}

for (const configName of a11yConfigKeys) {
// legacy config
Object.defineProperty(configs, `jsx-a11y-${configName}`, {
enumerable: true,
Expand Down
12 changes: 9 additions & 3 deletions src/a11y/index.ts
@@ -1,5 +1,6 @@
import type { Linter } from "eslint"
import type { RuleModule } from "../types"
import { buildConfigs } from "./configs"
import { buildFlatConfigs, buildLegacyConfigs } from "./configs"
import { buildRules } from "./rules"

/** Build a11y rules */
Expand All @@ -8,6 +9,11 @@ export function buildA11yRules(): RuleModule[] {
}

/** Build a11y configs */
export function buildA11yConfigs(): Record<string, unknown> {
return buildConfigs()
export function buildA11yFlatConfigs(): Record<string, Linter.FlatConfig[]> {
return buildFlatConfigs()
}

/** Build a11y configs */
export function buildA11yLegacyConfigs(): Record<string, Linter.Config> {
return buildLegacyConfigs()
}
2 changes: 1 addition & 1 deletion src/configs/all.ts
Expand Up @@ -8,7 +8,7 @@ for (const rule of rules.filter(
all[rule.meta.docs.ruleId] = "error"
}

export = {
export default {
extends: recommended.extends,
rules: {
...all,
Expand Down
2 changes: 1 addition & 1 deletion src/configs/base.ts
Expand Up @@ -3,7 +3,7 @@
// in order to update its content execute "npm run update"
import { hasTypescriptEslintParser } from "./has-typescript-eslint-parser"

export = {
export default {
plugins: ["astro"],
overrides: [
{
Expand Down
2 changes: 1 addition & 1 deletion src/configs/flat/base.ts
Expand Up @@ -14,7 +14,7 @@ export default [
plugins: {
get astro(): ESLint.Plugin {
// eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore
return require("../../index")
return require("../../plugin-without-config")
},
},
},
Expand Down
7 changes: 2 additions & 5 deletions src/configs/recommended.ts
@@ -1,11 +1,8 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "npm run update"
import path from "path"
const base = require.resolve("./base")
const baseExtend =
path.extname(`${base}`) === ".ts" ? "plugin:astro/base" : base
export = {
const baseExtend = "plugin:astro/base"
export default {
extends: [baseExtend],
rules: {
// eslint-plugin-astro rules
Expand Down
4 changes: 4 additions & 0 deletions src/index.cts
@@ -0,0 +1,4 @@
import { buildCjsConfigs } from "./plugin-config-builder"
import { meta, rules, processors, environments } from "./plugin-without-config"
const configs = buildCjsConfigs()
export = { meta, configs, rules, processors, environments }
5 changes: 5 additions & 0 deletions src/index.mts
@@ -0,0 +1,5 @@
import { buildEsmConfigs } from "./plugin-config-builder"
import { meta, rules, processors, environments } from "./plugin-without-config"
const configs = buildEsmConfigs()
export default { meta, configs, rules, processors, environments }
export { meta, configs, rules, processors, environments }
52 changes: 0 additions & 52 deletions src/index.ts

This file was deleted.

109 changes: 109 additions & 0 deletions src/plugin-config-builder.ts
@@ -0,0 +1,109 @@
import base from "./configs/base"
import recommended from "./configs/recommended"
import all from "./configs/all"
import flatBase from "./configs/flat/base"
import flatRecommended from "./configs/flat/recommended"
import flatAll from "./configs/flat/all"
import { buildA11yFlatConfigs, buildA11yLegacyConfigs } from "./a11y"
import type { Linter } from "eslint"

type ESMConfigs = {
base: Linter.FlatConfig[]
recommended: Linter.FlatConfig[]
all: Linter.FlatConfig[]
"jsx-a11y-strict": Linter.FlatConfig[]
"jsx-a11y-recommended": Linter.FlatConfig[]
// For backward compatibility
"flat/base": Linter.FlatConfig[]
"flat/recommended": Linter.FlatConfig[]
"flat/all": Linter.FlatConfig[]
"flat/jsx-a11y-strict": Linter.FlatConfig[]
"flat/jsx-a11y-recommended": Linter.FlatConfig[]
}
/**
* Build configs for ESM Module
*/
export function buildEsmConfigs(): ESMConfigs {
const esmConfigs: ESMConfigs = {
base: flatBase as Linter.FlatConfig[],
recommended: flatRecommended as Linter.FlatConfig[],
all: flatAll as Linter.FlatConfig[],
"jsx-a11y-strict": null as never,
"jsx-a11y-recommended": null as never,
// For backward compatibility
"flat/base": flatBase as Linter.FlatConfig[],
"flat/recommended": flatRecommended as Linter.FlatConfig[],
"flat/all": flatAll as Linter.FlatConfig[],
"flat/jsx-a11y-strict": null as never,
"flat/jsx-a11y-recommended": null as never,
}

const a11yFlatConfigs = buildA11yFlatConfigs()
for (const configName of Object.keys(a11yFlatConfigs)) {
Object.defineProperty(esmConfigs, configName, {
enumerable: true,
get() {
return a11yFlatConfigs[configName]
},
})
Object.defineProperty(esmConfigs, `flat/${configName}`, {
enumerable: true,
get() {
return a11yFlatConfigs[configName]
},
})
}

return esmConfigs
}

type CJSConfigs = {
base: Linter.Config
recommended: Linter.Config
all: Linter.Config
"jsx-a11y-strict": Linter.Config
"jsx-a11y-recommended": Linter.Config
"flat/base": Linter.FlatConfig[]
"flat/recommended": Linter.FlatConfig[]
"flat/all": Linter.FlatConfig[]
"flat/jsx-a11y-strict": Linter.FlatConfig[]
"flat/jsx-a11y-recommended": Linter.FlatConfig[]
}

/**
* Build configs for CJS Module
*/
export function buildCjsConfigs(): CJSConfigs {
const cjsConfigs: CJSConfigs = {
base: base as never,
recommended: recommended as Linter.Config,
all: all as Linter.Config,
"jsx-a11y-strict": null as never as Linter.Config,
"jsx-a11y-recommended": null as never as Linter.Config,
"flat/base": flatBase as Linter.FlatConfig[],
"flat/recommended": flatRecommended as Linter.FlatConfig[],
"flat/all": flatAll as Linter.FlatConfig[],
"flat/jsx-a11y-strict": null as never as Linter.FlatConfig[],
"flat/jsx-a11y-recommended": null as never as Linter.FlatConfig[],
}

const a11yFlatConfigs = buildA11yFlatConfigs()
for (const configName of Object.keys(a11yFlatConfigs)) {
Object.defineProperty(cjsConfigs, `flat/${configName}`, {
enumerable: true,
get() {
return a11yFlatConfigs[configName]
},
})
}
const a11yLegacyConfigs = buildA11yLegacyConfigs()
for (const configName of Object.keys(a11yLegacyConfigs)) {
Object.defineProperty(cjsConfigs, configName, {
enumerable: true,
get() {
return a11yLegacyConfigs[configName]
},
})
}
return cjsConfigs
}
20 changes: 20 additions & 0 deletions src/plugin-without-config.ts
@@ -0,0 +1,20 @@
import { rules as ruleList } from "./utils/rules"
import * as processorsDefines from "./processor"
import type { Rule } from "eslint"

export * as meta from "./meta"
export { environments } from "./environments"

export const rules = ruleList.reduce(
(obj, r) => {
obj[r.meta.docs.ruleName] = r as never
return obj
},
{} as { [key: string]: Rule.RuleModule },
)

export const processors = {
".astro": processorsDefines.astroProcessor,
astro: processorsDefines.astroProcessor,
"client-side-ts": processorsDefines.clientSideTsProcessor,
}

0 comments on commit 80dfa09

Please sign in to comment.