Skip to content

Commit

Permalink
Enhance ESLint plugin configuration and documentation
Browse files Browse the repository at this point in the history
- Added support for eslint-plugin and package-json plugins in eslint.config.js.
- Updated package.json with new keywords, repository details, and scripts for release management.
- Improved README.md with installation instructions, usage examples, and release process details.
- Refactored lib/index.js to dynamically read package.json and set up plugin metadata.
- Enhanced enforce-package-type rule documentation for clarity.

These changes improve the plugin's functionality, usability, and maintainability.
  • Loading branch information
jimmyn committed Jan 10, 2025
1 parent 7f23960 commit 2673b1f
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 59 deletions.
19 changes: 19 additions & 0 deletions .release-it.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"git": {
"commitMessage": "chore: release v${version}",
"tagName": "v${version}",
"requireCleanWorkingDir": true,
"requireUpstream": true
},
"github": {
"release": true,
"releaseName": "v${version}"
},
"npm": {
"publish": true
},
"hooks": {
"before:init": ["npm run lint", "npm test"],
"after:bump": "npm run format"
}
}
70 changes: 66 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ ESLint plugin to enforce package.json type field to be either "module" or "commo

## Installation

First, install the required peer dependencies:

```bash
npm install --save-dev eslint@^9.0.0 jsonc-eslint-parser@^2.0.0
```

Then, install the plugin:

```bash
npm install --save-dev eslint-enforce-package-type
```
Expand All @@ -15,27 +23,41 @@ Add the plugin to your ESLint configuration:
```js
// eslint.config.js
import enforcePackageType from 'eslint-enforce-package-type';
import jsoncParser from 'jsonc-eslint-parser';

export default [
// Use recommended configuration (enforces "module" by default)
enforcePackageType.configs.recommended,
...enforcePackageType.configs.recommended,

// Or configure manually
{
files: ['package.json'],
plugins: {
'enforce-package-type': enforcePackageType
},
rules: {
// Default to 'module'
'enforce-package-type/enforce-package-type': 'error',

'enforce-package-type/enforce-package-type': 'error'
// Or enforce 'commonjs'
'enforce-package-type/enforce-package-type': ['error', {enforceType: 'commonjs'}]
// 'enforce-package-type/enforce-package-type': ['error', { enforceType: 'commonjs' }]
},
languageOptions: {
parser: jsoncParser
}
}
];
```

Then run ESLint:

```bash
# Check for issues
npx eslint .

# Or automatically fix issues
npx eslint . --fix
```

## Rule Details

This rule enforces that the `type` field in package.json is set to either "module" or "commonjs".
Expand All @@ -46,6 +68,14 @@ The rule accepts an options object with the following properties:

- `enforceType`: The type to enforce ("module" or "commonjs"). Defaults to "module".

### Auto-fix

This rule supports the `--fix` option. When enabled, it will:

- Add the `type` field if it's missing
- Change the `type` field value to match the enforced type
- Preserve all other fields and formatting

### Examples

#### Valid
Expand Down Expand Up @@ -115,6 +145,9 @@ npm run test:watch
# Lint files
npm run lint

# Lint and fix files
npm run lint:fix

# Format files
npm run format
```
Expand Down Expand Up @@ -144,6 +177,35 @@ The test suite includes:
- Edge cases (malformed JSON, invalid types)
- Auto-fix functionality tests

### Releasing

This project uses [release-it](https://github.com/release-it/release-it) for version management and package publishing.

Available commands:

```bash
# Dry run (no changes)
npm run release:dry

# Regular release
npm run release

# Alpha release
npm run release:alpha

# Beta release
npm run release:beta
```

The release process:

1. Runs tests and linting
2. Bumps version in package.json
3. Creates a git tag
4. Creates a GitHub release
5. Publishes to npm
6. Formats files after version bump

### Contributing

1. Fork the repository
Expand Down
26 changes: 10 additions & 16 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
import js from '@eslint/js';
import eslintPlugin from 'eslint-plugin-eslint-plugin';
import packageJson from 'eslint-plugin-package-json/configs/recommended';
import enforcePackageType from './lib/index.js';

export default [
js.configs.recommended,
eslintPlugin.configs['flat/recommended'],
...enforcePackageType.configs.recommended,
packageJson,
{
files: ['**/*.test.js'],
...eslintPlugin.configs['flat/tests-recommended']
},
{
files: ['**/*.js'],
languageOptions: {
ecmaVersion: 2022,
sourceType: 'module'
}
},
{
files: ['package.json'],
plugins: {
'enforce-package-type': {
rules: {
'enforce-package-type': (await import('./lib/rules/enforce-package-type.js')).default
}
}
},
rules: {
'enforce-package-type/enforce-package-type': 'error'
},
languageOptions: {
parser: (await import('jsonc-eslint-parser')).default
}
}
];
34 changes: 28 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
import fs from 'fs';
import jsoncParser from 'jsonc-eslint-parser';
import {URL} from 'url';
import enforcePackageType from './rules/enforce-package-type.js';

export default {
const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf8'));

const plugin = {
meta: {
name: pkg.name,
version: pkg.version
},
rules: {
'enforce-package-type': enforcePackageType
},
configs: {
recommended: {
plugins: ['enforce-package-type'],
configs: {}
};

// Assign configs after plugin object is created so we can reference it
Object.assign(plugin.configs, {
recommended: [
{
files: ['package.json'],
plugins: {
'enforce-package-type': plugin
},
rules: {
'enforce-package-type/enforce-package-type': 'error'
},
languageOptions: {
parser: jsoncParser
}
}
}
};
]
});

export default plugin;
2 changes: 1 addition & 1 deletion lib/rules/enforce-package-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default {
meta: {
type: 'problem',
docs: {
description: 'Enforce package.json type field to be either "module" or "commonjs"',
description: 'enforce package.json type field to be either "module" or "commonjs"',
recommended: true,
url: 'https://github.com/MONEI/eslint-enforce-package-type/blob/main/docs/rules/enforce-package-type.md'
},
Expand Down
73 changes: 41 additions & 32 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,49 @@
"name": "eslint-enforce-package-type",
"version": "1.0.0",
"description": "ESLint plugin to enforce package.json type field (module or commonjs)",
"main": "lib/index.js",
"keywords": [
"eslint",
"eslintplugin",
"eslint-plugin",
"package.json",
"type",
"module",
"commonjs",
"esm",
"cjs",
"package-type"
],
"homepage": "https://github.com/MONEI/eslint-enforce-package-type#readme",
"bugs": {
"url": "https://github.com/MONEI/eslint-enforce-package-type/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/MONEI/eslint-enforce-package-type.git"
},
"license": "MIT",
"author": "MONEI",
"type": "module",
"main": "lib/index.js",
"files": [
"lib",
"README.md",
"LICENSE"
],
"scripts": {
"test": "vitest run",
"test:watch": "vitest",
"lint": "eslint .",
"format": "prettier --write .",
"format:check": "prettier --check .",
"prepare": "husky install",
"lint": "eslint .",
"lint-staged": "lint-staged",
"prepublishOnly": "npm run test && npm run lint"
"lint:fix": "eslint . --fix",
"prepare": "husky install",
"prepublishOnly": "npm run test && npm run lint",
"release": "release-it",
"release:alpha": "release-it --preRelease=alpha",
"release:beta": "release-it --preRelease=beta",
"release:dry": "release-it --dry-run",
"test": "vitest run",
"test:watch": "vitest"
},
"lint-staged": {
"*.{js,jsx,ts,tsx,json,css,md}": [
Expand All @@ -27,40 +54,22 @@
"eslint --fix"
]
},
"repository": {
"type": "git",
"url": "git+https://github.com/MONEI/eslint-enforce-package-type.git"
},
"keywords": [
"eslint",
"eslintplugin",
"eslint-plugin",
"package.json",
"type",
"module",
"commonjs",
"esm",
"cjs",
"package-type"
],
"author": "MONEI",
"license": "MIT",
"bugs": {
"url": "https://github.com/MONEI/eslint-enforce-package-type/issues"
},
"homepage": "https://github.com/MONEI/eslint-enforce-package-type#readme",
"peerDependencies": {
"eslint": ">=9.0.0"
},
"devDependencies": {
"@eslint/js": "^9.0.0",
"eslint": "^9.0.0",
"eslint": "^9.17.0",
"eslint-plugin-eslint-plugin": "^6.4.0",
"eslint-plugin-package-json": "^0.19.0",
"husky": "^8.0.3",
"jsonc-eslint-parser": "^2.4.0",
"lint-staged": "^15.2.0",
"prettier": "^3.1.1",
"release-it": "^17.0.1",
"vitest": "^1.1.3"
},
"peerDependencies": {
"eslint": ">=9.0.0",
"jsonc-eslint-parser": ">=2.0.0"
},
"engines": {
"node": ">=18.0.0"
},
Expand Down

0 comments on commit 2673b1f

Please sign in to comment.