@blumintinc/eslint-plugin-blumint 0.1.23 → 0.1.24

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/README.md CHANGED
@@ -56,21 +56,24 @@ Or use the recommended config:
56
56
  ✅ Set in the `recommended` configuration.\
57
57
  🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).
58
58
 
59
- | Name                           | Description | 💼 | ⚠️ | 🔧 |
60
- | :----------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------- | :- | :- | :- |
61
- | [array-methods-this-context](docs/rules/array-methods-this-context.md) | Prevent misuse of Array methods in OOP | | ✅ | |
62
- | [export-if-in-doubt](docs/rules/export-if-in-doubt.md) | All top-level const definitions, type definitions, and functions should be exported | | | |
63
- | [extract-global-constants](docs/rules/extract-global-constants.md) | Extract constants/functions to the global scope when possible | | | |
64
- | [generic-starts-with-t](docs/rules/generic-starts-with-t.md) | Enforce TypeScript generic types to start with T | || |
65
- | [no-async-array-filter](docs/rules/no-async-array-filter.md) | Disallow async callbacks for Array.filter | | | |
66
- | [no-async-foreach](docs/rules/no-async-foreach.md) | Disallow Array.forEach with an async callback function | ✅ | | |
67
- | [no-conditional-literals-in-jsx](docs/rules/no-conditional-literals-in-jsx.md) | Disallow use of conditional literals in JSX code | ✅ | | |
68
- | [no-filter-without-return](docs/rules/no-filter-without-return.md) | Disallow Array.filter callbacks without an explicit return (if part of a block statement) | ✅ | | |
69
- | [no-misused-switch-case](docs/rules/no-misused-switch-case.md) | Prevent misuse of logical OR in switch case statements | ✅ | | |
70
- | [no-unpinned-dependencies](docs/rules/no-unpinned-dependencies.md) | Enforces pinned dependencies | ✅ | | 🔧 |
71
- | [no-useless-fragment](docs/rules/no-useless-fragment.md) | Prevent unnecessary use of React fragments | | ✅ | 🔧 |
72
- | [prefer-fragment-shorthand](docs/rules/prefer-fragment-shorthand.md) | Prefer <> shorthand for <React.Fragment> | | ✅ | 🔧 |
73
- | [prefer-type-over-interface](docs/rules/prefer-type-over-interface.md) | Prefer using type alias over interface | | ✅ | 🔧 |
59
+ | Name                             | Description | 💼 | ⚠️ | 🔧 |
60
+ | :--------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------- | :- | :- | :- |
61
+ | [array-methods-this-context](docs/rules/array-methods-this-context.md) | Prevent misuse of Array methods in OOP | | ✅ | |
62
+ | [class-methods-read-top-to-bottom](docs/rules/class-methods-read-top-to-bottom.md) | Ensures classes read linearly from top to bottom. | || 🔧 |
63
+ | [dynamic-https-errors](docs/rules/dynamic-https-errors.md) | Dynamic error details should only be in the third argument of the HttpsError constructor. The second argument is hashed to produce a unique id. | || |
64
+ | [export-if-in-doubt](docs/rules/export-if-in-doubt.md) | All top-level const definitions, type definitions, and functions should be exported | | | |
65
+ | [extract-global-constants](docs/rules/extract-global-constants.md) | Extract constants/functions to the global scope when possible | | | |
66
+ | [generic-starts-with-t](docs/rules/generic-starts-with-t.md) | Enforce TypeScript generic types to start with T | | ✅ | |
67
+ | [no-async-array-filter](docs/rules/no-async-array-filter.md) | Disallow async callbacks for Array.filter | ✅ | | |
68
+ | [no-async-foreach](docs/rules/no-async-foreach.md) | Disallow Array.forEach with an async callback function | ✅ | | |
69
+ | [no-conditional-literals-in-jsx](docs/rules/no-conditional-literals-in-jsx.md) | Disallow use of conditional literals in JSX code | ✅ | | |
70
+ | [no-filter-without-return](docs/rules/no-filter-without-return.md) | Disallow Array.filter callbacks without an explicit return (if part of a block statement) | ✅ | | |
71
+ | [no-misused-switch-case](docs/rules/no-misused-switch-case.md) | Prevent misuse of logical OR in switch case statements | ✅ | | |
72
+ | [no-unpinned-dependencies](docs/rules/no-unpinned-dependencies.md) | Enforces pinned dependencies | ✅ | | 🔧 |
73
+ | [no-useless-fragment](docs/rules/no-useless-fragment.md) | Prevent unnecessary use of React fragments | | ✅ | 🔧 |
74
+ | [prefer-fragment-shorthand](docs/rules/prefer-fragment-shorthand.md) | Prefer <> shorthand for <React.Fragment> | | ✅ | 🔧 |
75
+ | [prefer-type-over-interface](docs/rules/prefer-type-over-interface.md) | Prefer using type alias over interface | | ✅ | 🔧 |
76
+ | [require-memo](docs/rules/require-memo.md) | React components must be memoized | ✅ | | 🔧 |
74
77
 
75
78
  <!-- end auto-generated rules list -->
76
79
 
@@ -1,7 +1,9 @@
1
- # Try to keep classes reading top to bottom (`@blumintinc/blumint/class-methods-read-top-to-bottom`)
1
+ # Ensures classes read linearly from top to bottom (`@blumintinc/blumint/class-methods-read-top-to-bottom`)
2
2
 
3
3
  ⚠️ This rule _warns_ in the ✅ `recommended` config.
4
4
 
5
+ 🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
6
+
5
7
  <!-- end auto-generated rule header -->
6
8
 
7
9
  This rule enforces an ordering of class methods according to BluMint's code style.
@@ -1,4 +1,4 @@
1
- # Dynamic elements of errors should be in the 'details' field (`@blumintinc/blumint/dynamic-https-errors`)
1
+ # Dynamic error details should only be in the third argument of the HttpsError constructor. The second argument is hashed to produce a unique id (`@blumintinc/blumint/dynamic-https-errors`)
2
2
 
3
3
  ⚠️ This rule _warns_ in the ✅ `recommended` config.
4
4
 
@@ -1,4 +1,8 @@
1
- # Requires memoization of react components (`@blumintinc/blumint/require-memo`)
1
+ # React components must be memoized (`@blumintinc/blumint/require-memo`)
2
+
3
+ 💼 This rule is enabled in the ✅ `recommended` config.
4
+
5
+ 🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
2
6
 
3
7
  <!-- end auto-generated rule header -->
4
8
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blumintinc/eslint-plugin-blumint",
3
- "version": "0.1.23",
3
+ "version": "0.1.24",
4
4
  "description": "Custom eslint rules for use at BluMint",
5
5
  "keywords": [
6
6
  "eslint",
@@ -13,7 +13,7 @@
13
13
  "scripts": {
14
14
  "lint": "npm-run-all \"lint:*\"",
15
15
  "lint:eslint-docs": "npm-run-all \"update:eslint-docs -- --check\"",
16
- "lint:js": "eslint .",
16
+ "lint:js": "eslint ./src",
17
17
  "test": "jest",
18
18
  "docs": "./scripts/make-docs.sh && npm run update:eslint-docs",
19
19
  "update:eslint-docs": "eslint-doc-generator"
@@ -36,7 +36,7 @@
36
36
  "ts-jest": "29.0.5"
37
37
  },
38
38
  "engines": {
39
- "node": "^16.0.0"
39
+ "node": "^20.0.0"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "eslint": ">=7"
@@ -1,80 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ClassGraphBuilder = void 0;
4
- /* eslint-disable security/detect-object-injection */
5
- const utils_1 = require("@typescript-eslint/utils");
6
- const ClassGraphSorter_1 = require("./ClassGraphSorter");
7
- const ASTHelpers_1 = require("./ASTHelpers");
8
- /**
9
- * Builds a graph of class methods and properties with their dependencies from a class declaration.
10
- * A dependency in this case is the name of another class method.
11
- */
12
- class ClassGraphBuilder {
13
- constructor(className, classBody) {
14
- this.className = className;
15
- this.classBody = classBody;
16
- this.graph = {};
17
- this.buildGraph();
18
- // Note: extension requires injection of other sorters
19
- this.sorter = new ClassGraphSorter_1.ClassGraphSorterReadability(this.graph);
20
- }
21
- buildGraph() {
22
- // NOTE: these need to be run sequentially for each member,
23
- // since we need to know the class members before we can search
24
- // methods for dependencies
25
- this.classBody.body.forEach((member) => {
26
- if (ClassGraphBuilder.isClassMember(member)) {
27
- this.addMemberToGraph(member);
28
- }
29
- });
30
- this.classBody.body.forEach((member) => {
31
- if (ClassGraphBuilder.isNamedClassMethod(member)) {
32
- const { key } = member;
33
- if (utils_1.ASTUtils.isIdentifier(key)) {
34
- this.addDependencies(member, key.name);
35
- }
36
- }
37
- });
38
- }
39
- static isClassMember(node) {
40
- return (node.type === 'MethodDefinition' || node.type === 'PropertyDefinition');
41
- }
42
- addMemberToGraph(member) {
43
- const name = member.key.name;
44
- const type = ClassGraphBuilder.nodeTypeOf(member);
45
- const node = ClassGraphBuilder.createGraphNode(name, type, member.accessibility, member.static);
46
- this.graph[name] = node;
47
- }
48
- static nodeTypeOf(member) {
49
- if (member.type === 'MethodDefinition') {
50
- return member.kind === 'constructor' ? 'constructor' : 'method';
51
- }
52
- return 'property';
53
- }
54
- static createGraphNode(name, type, accessibility, isStatic = false) {
55
- return {
56
- name,
57
- type,
58
- accessibility,
59
- isStatic,
60
- dependencies: [],
61
- };
62
- }
63
- static isNamedClassMethod(node) {
64
- return node.type === 'MethodDefinition';
65
- }
66
- addDependencies(node, methodName) {
67
- const newDependencies = ASTHelpers_1.ASTHelpers.classMethodDependenciesOf(node, this.graph, this.className).filter((name) => !!this.graph[name] && name !== methodName);
68
- if (this.graph[methodName]) {
69
- this.graph[methodName].dependencies.push(...newDependencies);
70
- }
71
- }
72
- get graphSorted() {
73
- return this.sorter.nodesSorted;
74
- }
75
- get memberNamesSorted() {
76
- return this.sorter.nodeNamesSorted;
77
- }
78
- }
79
- exports.ClassGraphBuilder = ClassGraphBuilder;
80
- //# sourceMappingURL=ClassGraphBuilder.js.map
@@ -1,99 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ClassGraphSorterReadability = exports.ClassGraphSorter = void 0;
4
- class ClassGraphSorter {
5
- constructor(graph) {
6
- this.graph = graph;
7
- }
8
- }
9
- exports.ClassGraphSorter = ClassGraphSorter;
10
- class ClassGraphSorterReadability extends ClassGraphSorter {
11
- constructor(graph) {
12
- super(graph);
13
- this.graph = graph;
14
- }
15
- get nodeNamesSorted() {
16
- return this.nodesSorted.map((node) => node.name);
17
- }
18
- get nodesSorted() {
19
- return this.sortNodes();
20
- }
21
- sortNodes() {
22
- const { methods, properties, classConstructor } = this.groupNodesByType();
23
- const [propertiesSorted, methodsSorted] = [properties, methods].map((nodeGroup) => nodeGroup.sort(ClassGraphSorterReadability.sortMembersForReadability));
24
- const searchNodeCandidates = [classConstructor, ...methodsSorted].filter((node) => !!node);
25
- const searchNodeCandidatesValidated = searchNodeCandidates.filter((method, _index, arr) => ClassGraphSorterReadability.SEARCH_NODE_PRIORITY_FUNCTIONS.some((fn) => !!fn(method, arr)));
26
- const searchNodesSorted = searchNodeCandidatesValidated.sort((a, b) => {
27
- const getPriority = (method) => {
28
- const priority = ClassGraphSorterReadability.SEARCH_NODE_PRIORITY_FUNCTIONS.findIndex((fn) => !!fn(method, methodsSorted));
29
- return priority === -1
30
- ? ClassGraphSorterReadability.SEARCH_NODE_PRIORITY_FUNCTIONS.length
31
- : priority;
32
- };
33
- return getPriority(a) - getPriority(b);
34
- });
35
- const dfsSortedNodes = this.dependencyDfs(searchNodesSorted);
36
- return [...propertiesSorted, ...dfsSortedNodes];
37
- }
38
- groupNodesByType() {
39
- const nodes = Object.values(this.graph);
40
- return nodes.reduce((acc, node) => {
41
- switch (node.type) {
42
- case 'property':
43
- acc.properties.push(node);
44
- break;
45
- case 'constructor':
46
- acc.classConstructor = node;
47
- break;
48
- case 'method':
49
- acc.methods.push(node);
50
- break;
51
- }
52
- return acc;
53
- }, {
54
- properties: [],
55
- methods: [],
56
- classConstructor: null,
57
- });
58
- }
59
- static sortMembersForReadability(a, b) {
60
- // NOTE: the ordering from Object.entries is safe here since it is readonly
61
- for (const [key, priorities] of Object.entries(ClassGraphSorterReadability.MODIFIER_PRIORITY_MAP)) {
62
- const indexA = priorities.indexOf(a[key]);
63
- const indexB = priorities.indexOf(b[key]);
64
- if (indexA !== indexB) {
65
- return indexA - indexB;
66
- }
67
- }
68
- return 0;
69
- }
70
- dependencyDfs(searchNodes) {
71
- const visited = new Set();
72
- const dfsSortedNodes = [];
73
- const dfs = (node) => {
74
- if (visited.has(node.name) || !node) {
75
- return;
76
- }
77
- visited.add(node.name);
78
- dfsSortedNodes.push(node);
79
- for (const dep of node.dependencies) {
80
- dfs(this.graph[String(dep)]);
81
- }
82
- };
83
- searchNodes.forEach((node) => dfs(node));
84
- return dfsSortedNodes;
85
- }
86
- }
87
- exports.ClassGraphSorterReadability = ClassGraphSorterReadability;
88
- ClassGraphSorterReadability.MODIFIER_PRIORITY_MAP = {
89
- isStatic: [true, false],
90
- accessibility: ['public', undefined, 'private'],
91
- };
92
- ClassGraphSorterReadability.SEARCH_NODE_PRIORITY_FUNCTIONS = [
93
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
94
- (method, _methods) => method.type === 'constructor',
95
- (method, _methods) => !_methods.some((node) => node.dependencies.includes(method.name)),
96
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
97
- (method, _methods) => method.dependencies.length === 0,
98
- ];
99
- //# sourceMappingURL=ClassGraphSorter.js.map
@@ -1,109 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.classNodesDfs = exports.ClassGraphSorter = void 0;
4
- class ClassGraphSorter {
5
- constructor(graph, sortFunction) {
6
- this.graph = graph;
7
- this.sortFunction = sortFunction;
8
- }
9
- sort() {
10
- const nodesGrouped = this.sortFunction(Object.values(this.graph));
11
- // Additional logic for DFS and sorting will be implemented here
12
- // Placeholder for the sorted result
13
- const sortedResult = [];
14
- // Logic to populate sortedResult
15
- return sortedResult;
16
- }
17
- defaultEnhancedSort(nodes) {
18
- // Implementation of the default enhanced sort logic
19
- // This will include sorting by type, static status, and accessibility
20
- // Placeholder for return value
21
- return { properties: [], notProperties: [] };
22
- }
23
- // Additional private helper methods will be implemented here
24
- // Placeholder for depth-first search (DFS) logic
25
- dfs(node, visited, sortedNodes) {
26
- // Implementation of the DFS logic
27
- }
28
- }
29
- exports.ClassGraphSorter = ClassGraphSorter;
30
- function classNodesDfs(graph) {
31
- const { properties, notProperties } = sortClassMembersForReadability(Object.values(graph));
32
- //now we have nodes in properties -> constructor -> methods order
33
- //we need to sort constructor & methods such that
34
- //if a node has dependencies, then the dependencies are moved after that node in a dfs manner
35
- const visited = new Set();
36
- const dfsSortedNodes = [];
37
- const dfs = (node) => {
38
- if (visited.has(node.name)) {
39
- return;
40
- }
41
- visited.add(node.name);
42
- dfsSortedNodes.push(node);
43
- for (const dep of node.dependencies) {
44
- dfs(graph[dep]);
45
- }
46
- };
47
- const searchNodes = notProperties
48
- .filter((method) => method.type === 'constructor' ||
49
- !notProperties.some((node) => node.dependencies.includes(method.name)) ||
50
- method.dependencies.length === 0)
51
- .sort((a, b) => {
52
- const getPriority = (method) => {
53
- if (method.type === 'constructor')
54
- return 1;
55
- if (!notProperties.some((node) => node.dependencies.includes(method.name)))
56
- return 2;
57
- if (method.dependencies.length === 0)
58
- return 3;
59
- return 4; // In case none of the conditions match
60
- };
61
- return getPriority(a) - getPriority(b);
62
- });
63
- for (const searchNode of searchNodes) {
64
- dfs(searchNode);
65
- }
66
- return [...properties, ...dfsSortedNodes].map((node) => node.name);
67
- }
68
- exports.classNodesDfs = classNodesDfs;
69
- function sortClassMembersForReadability(nodes) {
70
- // 1. Group nodes by type
71
- const properties = [];
72
- const methods = [];
73
- let constructor = null;
74
- for (const node of nodes) {
75
- switch (node.type) {
76
- case 'property':
77
- properties.push(node);
78
- break;
79
- case 'constructor':
80
- constructor = node;
81
- break;
82
- case 'method':
83
- methods.push(node);
84
- break;
85
- }
86
- }
87
- // 2. Fine-tune ordering within each group
88
- // For properties and methods
89
- const comparator = (a, b) => {
90
- // Static methods/properties come first
91
- if (a.isStatic && !b.isStatic)
92
- return -1;
93
- if (!a.isStatic && b.isStatic)
94
- return 1;
95
- const accessibilityA = a.accessibility ?? 'public';
96
- const accessibilityB = b.accessibility ?? 'public';
97
- if (accessibilityA === 'public' && accessibilityB === 'private')
98
- return -1;
99
- if (accessibilityA === 'private' && accessibilityB === 'public')
100
- return 1;
101
- // Maintain original order otherwise
102
- return 0;
103
- };
104
- properties.sort(comparator);
105
- methods.sort(comparator);
106
- // Return the nodes in the desired order
107
- return { properties, notProperties: [constructor, ...methods] };
108
- }
109
- //# sourceMappingURL=graph.js.map
@@ -1,45 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ASTHelpers = void 0;
4
- class ASTHelpers {
5
- constructor() {
6
- ASTHelpers.returnsJSX;
7
- }
8
- static returnsJSX(node) {
9
- if (node.type === 'JSXElement' || node.type === 'JSXFragment') {
10
- return true;
11
- }
12
- this.bar();
13
- if (node.type === 'BlockStatement') {
14
- for (const statement of node.body) {
15
- if (statement.type === 'ReturnStatement' &&
16
- (statement.argument?.type === 'JSXElement' ||
17
- statement.argument?.type === 'JSXFragment')) {
18
- return true;
19
- }
20
- // Handle conditional returns
21
- if (statement.type === 'ReturnStatement' &&
22
- statement.argument?.type === 'ConditionalExpression') {
23
- const conditionalExpr = statement.argument;
24
- if (ASTHelpers.returnsJSX(conditionalExpr.consequent) ||
25
- ASTHelpers.returnsJSX(conditionalExpr.alternate)) {
26
- return true;
27
- }
28
- }
29
- }
30
- }
31
- if (node.type === 'ConditionalExpression') {
32
- return (ASTHelpers.returnsJSX(node.consequent) ||
33
- ASTHelpers.returnsJSX(node.alternate));
34
- }
35
- return false;
36
- }
37
- bar() {
38
- this.foo();
39
- }
40
- foo() {
41
- return ASTHelpers.returnsJSX(node);
42
- }
43
- }
44
- exports.ASTHelpers = ASTHelpers;
45
- //# sourceMappingURL=testClass.js.map