Skip to content

Commit

Permalink
feat: add prefer-light-dependencies rule (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j committed Mar 10, 2024
1 parent a59eea9 commit ee58de7
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default [

- [`depend/redundant-polyfills`](./docs/rules/redundant-polyfills.md)
- [`depend/avoid-micro-utilities`](./docs/rules/avoid-micro-utilities.md)
- [`depend/prefer-light-dependencies`](./docs/rules/prefer-light-dependencies.md)

## License

Expand Down
32 changes: 32 additions & 0 deletions docs/rules/prefer-light-dependencies.md
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.
4 changes: 3 additions & 1 deletion src/main.ts
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
};
33 changes: 33 additions & 0 deletions src/rules/prefer-light-dependencies.ts
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)
};
}
};
4 changes: 2 additions & 2 deletions src/rules/redundant-polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export const rule: Rule.RuleModule = {
messages: {
simpleReplacement:
'"{{name}}" is redundant within your supported versions of node.' +
'It can likely be replaced by the natively available ' +
'It should be replaced by the natively available ' +
'"{{replacement}}"',
nativeReplacement:
'"{{name}}" is redundant within your supported versions of node.' +
'It can likely be replaced by the natively available ' +
'It should be replaced by the natively available ' +
'"{{replacement}}" ({{url}})',
documentedReplacement:
'"{{name}}" is redundant within your supported versions of node.' +
Expand Down
100 changes: 100 additions & 0 deletions src/test/rules/prefer-light-dependencies_test.ts
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')
}
}
]
}
]
});
34 changes: 34 additions & 0 deletions src/test/util/rule-meta_test.ts
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'
);
});
});
2 changes: 1 addition & 1 deletion src/util/rule-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ export function getMdnUrl(path: string): string {
* @return {string}
*/
export function getReplacementsDocUrl(path: string): string {
return `https://github.com/es-tooling/module-replacements/blob/main/docs/modules/${path}`;
return `https://github.com/es-tooling/module-replacements/blob/main/docs/modules/${path}.md`;
}

0 comments on commit ee58de7

Please sign in to comment.