Skip to content

Commit

Permalink
fix #16 critical build issue (#17)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
temoncher authored Sep 9, 2024
1 parent 5e25675 commit 75d5227
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-icons-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-clsx': patch
---

fix #16 critical build issue
25 changes: 25 additions & 0 deletions generate-all-rules.js
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\"",
"//<disabled-for-now>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"
},
Expand Down
1 change: 1 addition & 0 deletions src/PluginDocs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type PluginDocs = { recommended?: boolean };
4 changes: 3 additions & 1 deletion src/createRule.ts
Original file line number Diff line number Diff line change
@@ -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<PluginDocs>(
(name) => `https://github.com/temoncher/eslint-plugin-clsx/blob/main/docs/rules/${name}.md`
);
23 changes: 5 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown[], PluginDocs>) => 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,
Expand Down
19 changes: 19 additions & 0 deletions src/rules/allRules.generated.ts
Original file line number Diff line number Diff line change
@@ -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,
};
9 changes: 9 additions & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from 'tsup';

export default defineConfig({
entry: ['src/index.ts'],
splitting: false,
sourcemap: false,
clean: true,
treeshake: true,
});

0 comments on commit 75d5227

Please sign in to comment.