@oscarpalmer/atoms 0.171.0 → 0.172.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -1
- package/plugin/array/array.exists.plugin.js +39 -0
- package/plugin/array/array.sort.plugin.js +38 -0
- package/plugin/helpers.js +27 -0
- package/plugin/index.js +17 -0
- package/src/is.ts +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oscarpalmer/atoms",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.172.1",
|
|
4
4
|
"description": "Atomic utilities for making your JavaScript better.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"helper",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"files": [
|
|
19
19
|
"dist",
|
|
20
|
+
"plugin",
|
|
20
21
|
"src"
|
|
21
22
|
],
|
|
22
23
|
"type": "module",
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {isArrayMethod} from '../helpers.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef Plugin
|
|
5
|
+
* @property {string} name
|
|
6
|
+
* @property {import('eslint').Rule.RuleModule} value
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const message =
|
|
10
|
+
"Prefer the use of 'exists()' (@oscarpalmer/atoms) over 'Array.prototype.includes()'";
|
|
11
|
+
|
|
12
|
+
const methods = new Set(['includes']);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @type {Plugin}
|
|
16
|
+
*/
|
|
17
|
+
export default {
|
|
18
|
+
name: 'array.exists',
|
|
19
|
+
value: {
|
|
20
|
+
create(context) {
|
|
21
|
+
return {
|
|
22
|
+
CallExpression(node) {
|
|
23
|
+
if (isArrayMethod(context, node, methods)) {
|
|
24
|
+
context.report({
|
|
25
|
+
message,
|
|
26
|
+
node,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
},
|
|
32
|
+
meta: {
|
|
33
|
+
docs: {
|
|
34
|
+
description: message,
|
|
35
|
+
},
|
|
36
|
+
type: 'suggestion',
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {isArrayMethod} from '../helpers.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef Plugin
|
|
5
|
+
* @property {string} name
|
|
6
|
+
* @property {import('eslint').Rule.RuleModule} value
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const message = "Prefer the use of 'sort()' (@oscarpalmer/atoms) over 'Array.prototype.sort()'";
|
|
10
|
+
|
|
11
|
+
const methods = new Set(['sort']);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @type {Plugin}
|
|
15
|
+
*/
|
|
16
|
+
export default {
|
|
17
|
+
name: 'array.sort',
|
|
18
|
+
value: {
|
|
19
|
+
create(context) {
|
|
20
|
+
return {
|
|
21
|
+
CallExpression(node) {
|
|
22
|
+
if (isArrayMethod(context, node, methods)) {
|
|
23
|
+
context.report({
|
|
24
|
+
message,
|
|
25
|
+
node,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
},
|
|
31
|
+
meta: {
|
|
32
|
+
docs: {
|
|
33
|
+
description: message,
|
|
34
|
+
},
|
|
35
|
+
type: 'suggestion',
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {import('eslint').Rule.RuleContext} context
|
|
3
|
+
* @param {import('estree').CallExpression & import('eslint').Rule.NodeParentExtension} node
|
|
4
|
+
* @param {Set<string>} methods
|
|
5
|
+
* @returns {boolean}
|
|
6
|
+
*/
|
|
7
|
+
export function isArrayMethod(context, node, methods) {
|
|
8
|
+
const {callee} = node;
|
|
9
|
+
|
|
10
|
+
if (callee.type !== memberExpression) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const {object, property} = callee;
|
|
15
|
+
|
|
16
|
+
return (
|
|
17
|
+
object.type === arrayExpression &&
|
|
18
|
+
property.type === identifierType &&
|
|
19
|
+
methods.has(property.name)
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const arrayExpression = 'ArrayExpression';
|
|
24
|
+
|
|
25
|
+
const memberExpression = 'MemberExpression';
|
|
26
|
+
|
|
27
|
+
const identifierType = 'Identifier';
|
package/plugin/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import arrayExistsPlugin from './array/array.exists.plugin.js';
|
|
2
|
+
import arraySortPlugin from './array/array.sort.plugin.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {import('eslint').Linter.Config} ESLintConfig
|
|
6
|
+
*/
|
|
7
|
+
export default {
|
|
8
|
+
configs: {},
|
|
9
|
+
meta: {
|
|
10
|
+
name: '@oscarpalmer/atoms',
|
|
11
|
+
version: '1.0.0',
|
|
12
|
+
},
|
|
13
|
+
rules: {
|
|
14
|
+
[arrayExistsPlugin.name]: arrayExistsPlugin.value,
|
|
15
|
+
[arraySortPlugin.name]: arraySortPlugin.value,
|
|
16
|
+
},
|
|
17
|
+
};
|