@oscarpalmer/atoms 0.172.1 → 0.172.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oscarpalmer/atoms",
3
- "version": "0.172.1",
3
+ "version": "0.172.2",
4
4
  "description": "Atomic utilities for making your JavaScript better.",
5
5
  "keywords": [
6
6
  "helper",
@@ -0,0 +1,22 @@
1
+ import {getPlugin, isArrayMethod} from './helpers.js';
2
+
3
+ export default [
4
+ getPlugin(
5
+ {
6
+ full: 'array.exists',
7
+ short: 'exists',
8
+ },
9
+ new Set(['includes']),
10
+ isArrayMethod,
11
+ 'Array.prototype',
12
+ ),
13
+ getPlugin(
14
+ {
15
+ full: 'array.sort',
16
+ short: 'sort',
17
+ },
18
+ new Set(['sort']),
19
+ isArrayMethod,
20
+ 'Array.prototype',
21
+ ),
22
+ ];
package/plugin/helpers.js CHANGED
@@ -1,3 +1,88 @@
1
+ /**
2
+ * @typedef Name
3
+ * @property {string} full
4
+ * @property {string} short
5
+ */
6
+
7
+ /**
8
+ * @typedef Plugin
9
+ * @property {string} message
10
+ * @property {string} name
11
+ * @property {string} selector
12
+ * @property {import('eslint').Rule.RuleModule} value
13
+ */
14
+
15
+ /**
16
+ * @typedef Validator
17
+ * @param {import('eslint').Rule.RuleContext} context
18
+ * @param {import('estree').CallExpression & import('eslint').Rule.NodeParentExtension} node
19
+ * @param {Set<string>} methods
20
+ * @returns {boolean}
21
+ */
22
+
23
+ /**
24
+ * @param {Set<string>} methods
25
+ * @param {string|undefined} prefix
26
+ * @returns {string}
27
+ */
28
+ function getMethodNames(methods, prefix) {
29
+ const array = [...methods];
30
+ const {length} = array;
31
+
32
+ let names = '';
33
+
34
+ for (let index = 0; index < length; index += 1) {
35
+ const method = array[index];
36
+
37
+ const name = prefix == null ? method : `${prefix}.${method}`;
38
+
39
+ if (index === 0) {
40
+ names = `'${name}'`;
41
+ } else {
42
+ names += index === length - 1 ? `, and '${name}'` : `, '${name}'`;
43
+ }
44
+ }
45
+
46
+ return names;
47
+ }
48
+
49
+ /**
50
+ * @param {Name} name
51
+ * @param {Set<string>} methods
52
+ * @param {Validator} validator
53
+ * @param {string|undefined} prefix
54
+ * @returns {Plugin}
55
+ */
56
+ export function getPlugin(name, methods, validator, prefix) {
57
+ const message = `Prefer using '${name.short}' (@oscarpalmer/atoms) instead of ${getMethodNames(methods, prefix)}`;
58
+
59
+ return {
60
+ message,
61
+ name: name.full,
62
+ selector: `CallExpression[callee.type="${memberExpression}"][callee.object.type="${arrayExpression}"][callee.property.type="${identifierType}"]`,
63
+ value: {
64
+ create(context) {
65
+ return {
66
+ CallExpression(node) {
67
+ if (validator(context, node, methods)) {
68
+ context.report({
69
+ message,
70
+ node,
71
+ });
72
+ }
73
+ },
74
+ };
75
+ },
76
+ meta: {
77
+ docs: {
78
+ description: message,
79
+ },
80
+ type: 'suggestion',
81
+ },
82
+ },
83
+ };
84
+ }
85
+
1
86
  /**
2
87
  * @param {import('eslint').Rule.RuleContext} context
3
88
  * @param {import('estree').CallExpression & import('eslint').Rule.NodeParentExtension} node
package/plugin/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import arrayExistsPlugin from './array/array.exists.plugin.js';
2
- import arraySortPlugin from './array/array.sort.plugin.js';
1
+ import {groupBy} from '../dist/array/group-by.mjs';
2
+ import array from './array.js';
3
3
 
4
4
  /**
5
5
  * @typedef {import('eslint').Linter.Config} ESLintConfig
@@ -11,7 +11,6 @@ export default {
11
11
  version: '1.0.0',
12
12
  },
13
13
  rules: {
14
- [arrayExistsPlugin.name]: arrayExistsPlugin.value,
15
- [arraySortPlugin.name]: arraySortPlugin.value,
14
+ ...groupBy(array, 'name', 'value'),
16
15
  },
17
16
  };
@@ -1,39 +0,0 @@
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
- };
@@ -1,38 +0,0 @@
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
- };