eslint-plugin-depend
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -13,2 +13,10 @@ import { Rule } from 'eslint'; | ||
/** | ||
* Creates a rule listener for detecting dependencies in a `package.json` | ||
* file | ||
* @param {Rule.RuleContext} context ESLint context | ||
* @param {ImportListenerCallback} callback Listener callback | ||
* @return {Rule.RuleListener} | ||
*/ | ||
export declare function createPackageJsonListener(context: Rule.RuleContext, callback: ImportListenerCallback): Rule.RuleListener; | ||
/** | ||
* Creates a rule listener which finds replacements in imports/requires | ||
@@ -15,0 +23,0 @@ * @param {Rule.RuleContext} context ESLint context |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createReplacementListener = exports.createImportListener = void 0; | ||
exports.createReplacementListener = exports.createPackageJsonListener = exports.createImportListener = void 0; | ||
const package_json_js_1 = require("./package-json.js"); | ||
@@ -110,3 +110,31 @@ const rule_meta_js_1 = require("./rule-meta.js"); | ||
} | ||
const dependencyKeys = ['dependencies', 'devDependencies']; | ||
/** | ||
* Creates a rule listener for detecting dependencies in a `package.json` | ||
* file | ||
* @param {Rule.RuleContext} context ESLint context | ||
* @param {ImportListenerCallback} callback Listener callback | ||
* @return {Rule.RuleListener} | ||
*/ | ||
function createPackageJsonListener(context, callback) { | ||
return { | ||
'Program > JSONExpressionStatement > JSONObjectExpression > JSONProperty': (astNode) => { | ||
const node = astNode; | ||
if (node.key.type === 'JSONLiteral' && | ||
typeof node.key.value === 'string' && | ||
dependencyKeys.includes(node.key.value) && | ||
node.value.type === 'JSONObjectExpression') { | ||
for (const prop of node.value.properties) { | ||
if (prop.key.type === 'JSONLiteral' && | ||
typeof prop.key.value === 'string') { | ||
callback(context, prop, prop.key.value); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
exports.createPackageJsonListener = createPackageJsonListener; | ||
const packageJsonLikePath = /(^|[/\\])package.json$/; | ||
/** | ||
* Creates a rule listener which finds replacements in imports/requires | ||
@@ -118,4 +146,7 @@ * @param {Rule.RuleContext} context ESLint context | ||
function createReplacementListener(context, replacements) { | ||
if (packageJsonLikePath.test(context.filename)) { | ||
return createPackageJsonListener(context, (context, node, name) => replacementListenerCallback(context, replacements, node, name)); | ||
} | ||
return createImportListener(context, (context, node, source) => replacementListenerCallback(context, replacements, node, source)); | ||
} | ||
exports.createReplacementListener = createReplacementListener; |
{ | ||
"name": "eslint-plugin-depend", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "An ESLint plugin to suggest optimized dependencies, native alternatives and more", | ||
@@ -49,2 +49,3 @@ "main": "lib/main.js", | ||
"eslint-plugin-eslint-plugin": "^5.3.0", | ||
"jsonc-eslint-parser": "^2.4.0", | ||
"prettier": "^3.2.5", | ||
@@ -51,0 +52,0 @@ "rimraf": "^5.0.5", |
@@ -42,2 +42,28 @@ # eslint-plugin-depend | ||
### With `package.json` | ||
Some rules (e.g. `ban-dependencies`) can be used against your `package.json`. | ||
You can achieve this by using `jsonc-eslint-parser`. | ||
For example, in your `.eslintrc.json`: | ||
```json | ||
{ | ||
"overrides": [ | ||
{ | ||
"files": ["package.json"], | ||
"parser": "jsonc-eslint-parser", | ||
"plugins": ["depend"], | ||
"rules": { | ||
"depend/ban-dependencies": "error" | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
Read more at the | ||
[`jsonc-eslint-parser` docs](https://github.com/ota-meshi/jsonc-eslint-parser). | ||
## Rules | ||
@@ -44,0 +70,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28147
705
75
15