From 75d52274ff83a2ef7851913a428edbd90fd9824b Mon Sep 17 00:00:00 2001 From: Artem Baranov <48395731+temoncher@users.noreply.github.com> Date: Mon, 9 Sep 2024 13:59:44 +0300 Subject: [PATCH] fix #16 critical build issue (#17) * revert tsup build changes * add tsup config * rework build * make readable names for imports * fix `PluginDocs` type * add changeset * add eslint fix to rules generation --- .changeset/few-icons-roll.md | 5 +++++ generate-all-rules.js | 25 +++++++++++++++++++++++++ package-lock.json | 4 ++-- package.json | 5 +++-- src/PluginDocs.ts | 1 + src/createRule.ts | 4 +++- src/index.ts | 23 +++++------------------ src/rules/allRules.generated.ts | 19 +++++++++++++++++++ tsup.config.ts | 9 +++++++++ 9 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 .changeset/few-icons-roll.md create mode 100644 generate-all-rules.js create mode 100644 src/PluginDocs.ts create mode 100644 src/rules/allRules.generated.ts create mode 100644 tsup.config.ts diff --git a/.changeset/few-icons-roll.md b/.changeset/few-icons-roll.md new file mode 100644 index 0000000..684a6d7 --- /dev/null +++ b/.changeset/few-icons-roll.md @@ -0,0 +1,5 @@ +--- +'eslint-plugin-clsx': patch +--- + +fix #16 critical build issue diff --git a/generate-all-rules.js b/generate-all-rules.js new file mode 100644 index 0000000..a9ee330 --- /dev/null +++ b/generate-all-rules.js @@ -0,0 +1,25 @@ +/* eslint-disable @typescript-eslint/no-require-imports */ +const fs = require('fs'); + +function camelize(s) { + return s.replace(/-./g, (x) => x[1].toUpperCase()); +} + +const ruleNames = fs + .readdirSync(`${__dirname}/src/rules`) + .filter( + (fileName) => + !fileName.includes('.test') && + !fileName.includes('.generated') && + fileName !== 'tests-setup.mjs' && + fileName !== 'index.ts' + ) + .map((fileName) => fileName.replace(/\.ts$/, '')); + +const imports = ruleNames.map((name) => `import ${camelize(name)} from './${name}';`).join('\n'); + +const pairs = ruleNames.map((name) => `"${name}": ${camelize(name)}`).join(',\n'); + +const content = `${imports}\nexport const allRules = {\n${pairs}\n};\n`; + +fs.writeFileSync(`${__dirname}/src/rules/allRules.generated.ts`, content); diff --git a/package-lock.json b/package-lock.json index 5ae9fc6..f5c0787 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "eslint-plugin-clsx", - "version": "0.0.4", + "version": "0.0.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "eslint-plugin-clsx", - "version": "0.0.4", + "version": "0.0.5", "license": "MIT", "dependencies": { "remeda": "^2.12.0" diff --git a/package.json b/package.json index 103a166..ac047c1 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,14 @@ "description": "An ESLint plugin for clsx/classnames", "main": "dist/index.js", "scripts": { - "build": "rimraf dist && tsup src/**/*.ts !src/**/*.test.ts !src/rules/tests-setup.mjs --format cjs", + "build": "rimraf dist && npm run generate-rules && tsup", "lint": "npm-run-all \"lint:*\"", "lint:tsc": "tsc", "lint:docs": "markdownlint \"**/*.md\"", - "//lint:eslint-docs": "npm run update:eslint-docs -- --check", + "lint:eslint-docs": "npm run update:eslint-docs -- --check", "lint:js": "eslint .", "update:eslint-docs": "npm run build && eslint-doc-generator", + "generate-rules": "node ./generate-all-rules.js && eslint ./src/rules/*.generated.ts --fix", "release": "npm run build && changeset publish", "test": "vitest run" }, diff --git a/src/PluginDocs.ts b/src/PluginDocs.ts new file mode 100644 index 0000000..af91bf8 --- /dev/null +++ b/src/PluginDocs.ts @@ -0,0 +1 @@ +export type PluginDocs = { recommended?: boolean }; diff --git a/src/createRule.ts b/src/createRule.ts index c5e51bd..302dad9 100644 --- a/src/createRule.ts +++ b/src/createRule.ts @@ -1,5 +1,7 @@ import { ESLintUtils } from '@typescript-eslint/utils'; -export const createRule = ESLintUtils.RuleCreator<{ recommended?: boolean }>( +import type { PluginDocs } from './PluginDocs'; + +export const createRule = ESLintUtils.RuleCreator( (name) => `https://github.com/temoncher/eslint-plugin-clsx/blob/main/docs/rules/${name}.md` ); diff --git a/src/index.ts b/src/index.ts index 0c4afe9..f9850c3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,31 +1,18 @@ -import * as fs from 'fs'; -import * as path from 'path'; - -import type { Rule } from 'eslint'; +import type { RuleModule } from '@typescript-eslint/utils/ts-eslint'; import * as R from 'remeda'; import packageJson from '../package.json'; +import { PluginDocs } from './PluginDocs'; +import { allRules } from './rules/allRules.generated'; + const REPO_URL = packageJson.repository.url.replace(/^git\+/, '').replace(/\.git$/, ''); const configFilters = { all: () => true, - recommended: (rule: Rule.RuleModule) => rule.meta?.docs?.recommended, + recommended: (rule: RuleModule) => rule.meta?.docs?.recommended, }; -const allRules = Object.fromEntries( - fs - .readdirSync(`${__dirname}/rules`) - .map((fileName) => fileName.replace(/\.js$/, '')) - - .map((ruleName) => [ - ruleName, - - // eslint-disable-next-line @typescript-eslint/no-require-imports - require(path.join(__dirname, 'rules', ruleName)) as Rule.RuleModule, - ]) -); - export = { rules: R.mapValues(allRules, (rule, ruleName) => ({ ...rule, diff --git a/src/rules/allRules.generated.ts b/src/rules/allRules.generated.ts new file mode 100644 index 0000000..7f2b98c --- /dev/null +++ b/src/rules/allRules.generated.ts @@ -0,0 +1,19 @@ +import forbidArrayExpressions from './forbid-array-expressions'; +import forbidFalseInsideObjectExpressions from './forbid-false-inside-object-expressions'; +import forbidTrueInsideObjectExpressions from './forbid-true-inside-object-expressions'; +import noRedundantClsx from './no-redundant-clsx'; +import noSpreading from './no-spreading'; +import preferLogicalOverObjects from './prefer-logical-over-objects'; +import preferMergedNeighboringElements from './prefer-merged-neighboring-elements'; +import preferObjectsOverLogical from './prefer-objects-over-logical'; + +export const allRules = { + 'forbid-array-expressions': forbidArrayExpressions, + 'forbid-false-inside-object-expressions': forbidFalseInsideObjectExpressions, + 'forbid-true-inside-object-expressions': forbidTrueInsideObjectExpressions, + 'no-redundant-clsx': noRedundantClsx, + 'no-spreading': noSpreading, + 'prefer-logical-over-objects': preferLogicalOverObjects, + 'prefer-merged-neighboring-elements': preferMergedNeighboringElements, + 'prefer-objects-over-logical': preferObjectsOverLogical, +}; diff --git a/tsup.config.ts b/tsup.config.ts new file mode 100644 index 0000000..8f8a660 --- /dev/null +++ b/tsup.config.ts @@ -0,0 +1,9 @@ +import { defineConfig } from 'tsup'; + +export default defineConfig({ + entry: ['src/index.ts'], + splitting: false, + sourcemap: false, + clean: true, + treeshake: true, +});