-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add prefer-light-dependencies rule (#3)
- Loading branch information
Showing
8 changed files
with
206 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Prefers lighter alternatives over certain dependencies | ||
|
||
This rule prefers lighter alternatives over possibly problematic dependencies. | ||
|
||
For example, dependencies known to be bloated, no longer maintained or | ||
have security issues may be detected by this rule. | ||
|
||
Note this is an _opinionated_ rule, in that the dependencies detected are | ||
driven by those in the | ||
[module-replacements](https://github.com/es-tooling/module-replacements) | ||
project. | ||
|
||
## Rule Details | ||
|
||
This rule detects possibly problematic dependencies. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```ts | ||
const runAll = require('npm-run-all'); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```ts | ||
const runAll = require('npm-run-all2'); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you disagree with the opinionated list of dependencies this rule detects, | ||
you should not use this rule. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import {recommended} from './configs/recommended.js'; | ||
import {rule as redundantPolyfills} from './rules/redundant-polyfills.js'; | ||
import {rule as avoidMicroUtils} from './rules/avoid-micro-utilities.js'; | ||
import {rule as preferLightDependencies} from './rules/prefer-light-dependencies.js'; | ||
|
||
export const configs = { | ||
recommended | ||
}; | ||
|
||
export const rules = { | ||
'redundant-polyfills': redundantPolyfills, | ||
'avoid-micro-utilities': avoidMicroUtils | ||
'avoid-micro-utilities': avoidMicroUtils, | ||
'prefer-light-dependencies': preferLightDependencies | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {Rule} from 'eslint'; | ||
import {getDocsUrl} from '../util/rule-meta.js'; | ||
import {lighterReplacements} from '../replacements.js'; | ||
import {createReplacementListener} from '../util/imports.js'; | ||
|
||
export const rule: Rule.RuleModule = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Prefers lighter alternatives over certain dependencies', | ||
url: getDocsUrl('prefer-light-dependencies') | ||
}, | ||
schema: [], | ||
messages: { | ||
simpleReplacement: | ||
'"{{name}}" is redundant within your supported versions of node.' + | ||
'It should be replaced by the natively available ' + | ||
'"{{replacement}}"', | ||
nativeReplacement: | ||
'"{{name}}" is redundant within your supported versions of node.' + | ||
'It should be replaced by the natively available ' + | ||
'"{{replacement}}" ({{url}})', | ||
documentedReplacement: | ||
'"{{name}}" should be replaced with a lighter alternative.' + | ||
'For possible replacements, see {{url}}' | ||
} | ||
}, | ||
create: (context) => { | ||
return { | ||
...createReplacementListener(context, lighterReplacements) | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import {rule} from '../../rules/prefer-light-dependencies.js'; | ||
import {RuleTester} from 'eslint'; | ||
import {getReplacementsDocUrl} from '../../util/rule-meta.js'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
sourceType: 'module', | ||
ecmaVersion: 2022 | ||
} | ||
}); | ||
|
||
const tseslintParser = require.resolve('@typescript-eslint/parser'); | ||
|
||
ruleTester.run('prefer-light-dependencies', rule, { | ||
valid: [ | ||
'const foo = 303;', | ||
{ | ||
code: `import foo = require('unknown-module');`, | ||
parser: tseslintParser | ||
}, | ||
{ | ||
code: `import foo from 'unknown-module';` | ||
}, | ||
{ | ||
code: `const foo = require('unknown-module');` | ||
}, | ||
{ | ||
code: ` | ||
const moduleName = 'npm-run-' + 'all'; | ||
require(moduleName); | ||
` | ||
}, | ||
{ | ||
code: ` | ||
const moduleName = 'npm-run-' + 'all'; | ||
await import(moduleName); | ||
` | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: `const foo = require('npm-run-all');`, | ||
errors: [ | ||
{ | ||
line: 1, | ||
column: 13, | ||
messageId: 'documentedReplacement', | ||
data: { | ||
name: 'npm-run-all', | ||
url: getReplacementsDocUrl('npm-run-all') | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
code: `import foo from 'npm-run-all';`, | ||
errors: [ | ||
{ | ||
line: 1, | ||
column: 1, | ||
messageId: 'documentedReplacement', | ||
data: { | ||
name: 'npm-run-all', | ||
url: getReplacementsDocUrl('npm-run-all') | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
code: `const foo = await import('npm-run-all');`, | ||
errors: [ | ||
{ | ||
line: 1, | ||
column: 19, | ||
messageId: 'documentedReplacement', | ||
data: { | ||
name: 'npm-run-all', | ||
url: getReplacementsDocUrl('npm-run-all') | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
code: `import foo = require('npm-run-all');`, | ||
parser: tseslintParser, | ||
errors: [ | ||
{ | ||
line: 1, | ||
column: 1, | ||
messageId: 'documentedReplacement', | ||
data: { | ||
name: 'npm-run-all', | ||
url: getReplacementsDocUrl('npm-run-all') | ||
} | ||
} | ||
] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as assert from 'node:assert'; | ||
import {test} from 'node:test'; | ||
import { | ||
getDocsUrl, | ||
getMdnUrl, | ||
getReplacementsDocUrl | ||
} from '../../util/rule-meta.js'; | ||
|
||
test('getDocsUrl', async (t) => { | ||
await t.test('gets the url of a given rule doc', () => { | ||
assert.equal( | ||
getDocsUrl('bloop'), | ||
'https://github.com/43081j/eslint-plugin-assert/blob/main/docs/rules/bloop.md' | ||
); | ||
}); | ||
}); | ||
|
||
test('getMdnUrl', async (t) => { | ||
await t.test('gets the url of a given mdn doc', () => { | ||
assert.equal( | ||
getMdnUrl('bloop'), | ||
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/bloop' | ||
); | ||
}); | ||
}); | ||
|
||
test('getReplacementsDocUrl', async (t) => { | ||
await t.test('gets the url of a given replacements doc', () => { | ||
assert.equal( | ||
getReplacementsDocUrl('bloop'), | ||
'https://github.com/es-tooling/module-replacements/blob/main/docs/modules/bloop.md' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters