Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,29 @@ For older legacy projects, add to your `.eslintrc.json`:

Some rules (e.g. `ban-dependencies`) can be used against your `package.json`.

You can achieve this by using `jsonc-eslint-parser`.
You can achieve this by using `@eslint/json` or `jsonc-eslint-parser`.

For example, in your `.eslintrc.json`:
For example, with `@eslint/json` and `eslint.config.js`:

```ts
import depend from 'eslint-plugin-depend';
import json from '@eslint/json';
import {defineConfig} from 'eslint/config';

export default defineConfig([
{
files: ['package.json'],
language: 'json/json',
plugins: {
depend,
json
},
extends: ['depend/flat/recommended'],
}
]);
```

Or with `jsonc-eslint-parser` and `.eslintrc.json`:

```json
{
Expand All @@ -64,6 +84,7 @@ For example, in your `.eslintrc.json`:
```

Read more at the
[`@eslint/json` docs](https://github.com/eslint/json) and
[`jsonc-eslint-parser` docs](https://github.com/ota-meshi/jsonc-eslint-parser).

## Rules
Expand Down
28 changes: 28 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
},
"devDependencies": {
"@eslint/js": "^9.35.0",
"@eslint/json": "^0.13.2",
"@humanwhocodes/momoa": "^3.3.9",
"@types/eslint": "^9.6.1",
"@types/estree": "^1.0.8",
"@types/node": "^24.4.0",
Expand Down
65 changes: 64 additions & 1 deletion src/test/rules/ban-dependencies_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {runClassic} from 'eslint-vitest-rule-tester';
import {run, runClassic} from 'eslint-vitest-rule-tester';
import * as tseslintParser from '@typescript-eslint/parser';
import * as jsonParser from 'jsonc-eslint-parser';
import eslintJson from '@eslint/json';

import {rule} from '../../rules/ban-dependencies.js';
import {getMdnUrl, getReplacementsDocUrl} from '../../util/rule-meta.js';
Expand Down Expand Up @@ -264,3 +265,65 @@ await runClassic('ban-dependencies', rule, {
}
]
});

// Test using `@eslint/json` plugin
run({
name: 'ban-dependencies-json',
configs: [
{
files: ['**/*.json'],
language: 'json/json',
plugins: {
json: eslintJson
}
}
],
rule,
valid: [
{
code: `{
"dependencies": {
"unknown-module": "^1.0.0"
}
}`,
filename: 'package.json'
},
{
code: `{
"dependencies": {
"npm-run-all": "^1.0.0"
}
}`,
filename: 'not-a-package.json'
},
{
code: `{
"not-dependencies": {
"some-other-nonsense": 123
}
}`,
filename: 'package.json'
}
],
invalid: [
{
code: `{
"dependencies": {
"npm-run-all": "^1.0.0"
}
}`,
filename: 'package.json',
errors: [
{
line: 3,
column: 11,
messageId: 'documentedReplacement',
data: {
name: 'npm-run-all',
url: getReplacementsDocUrl('npm-run-all')
}
}
]
}
]
});
20 changes: 20 additions & 0 deletions src/util/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {Rule} from 'eslint';
import {TSESTree} from '@typescript-eslint/typescript-estree';
import {closestPackageSatisfiesNodeVersion} from './package-json.js';
import {getMdnUrl, getReplacementsDocUrl} from './rule-meta.js';
import type {MemberNode} from '@humanwhocodes/momoa';
import type {AST as JsonESTree} from 'jsonc-eslint-parser';
import type {ModuleReplacement} from 'module-replacements';

Expand Down Expand Up @@ -154,6 +155,25 @@ export function createPackageJsonListener(
callback: ImportListenerCallback
): Rule.RuleListener {
return {
// Support for `@eslint/json`
'Document > Object > Member': (node: MemberNode) => {
if (
node.name.type === 'String' &&
dependencyKeys.includes(node.name.value) &&
node.value.type === 'Object'
) {
for (const member of node.value.members) {
if (member.name.type === 'String') {
callback(
context,
member as unknown as Rule.Node,
member.name.value
);
}
}
}
},
// Support for `jsonc-eslint-parser`
'Program > JSONExpressionStatement > JSONObjectExpression > JSONProperty': (
astNode: Rule.Node
) => {
Expand Down