@oscarpalmer/atoms 0.172.0 → 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.0",
3
+ "version": "0.172.2",
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,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
+ ];
@@ -0,0 +1,112 @@
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
+
86
+ /**
87
+ * @param {import('eslint').Rule.RuleContext} context
88
+ * @param {import('estree').CallExpression & import('eslint').Rule.NodeParentExtension} node
89
+ * @param {Set<string>} methods
90
+ * @returns {boolean}
91
+ */
92
+ export function isArrayMethod(context, node, methods) {
93
+ const {callee} = node;
94
+
95
+ if (callee.type !== memberExpression) {
96
+ return false;
97
+ }
98
+
99
+ const {object, property} = callee;
100
+
101
+ return (
102
+ object.type === arrayExpression &&
103
+ property.type === identifierType &&
104
+ methods.has(property.name)
105
+ );
106
+ }
107
+
108
+ const arrayExpression = 'ArrayExpression';
109
+
110
+ const memberExpression = 'MemberExpression';
111
+
112
+ const identifierType = 'Identifier';
@@ -0,0 +1,16 @@
1
+ import {groupBy} from '../dist/array/group-by.mjs';
2
+ import array from './array.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
+ ...groupBy(array, 'name', 'value'),
15
+ },
16
+ };