@blumintinc/eslint-plugin-blumint 1.19.30 → 1.19.31
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/lib/index.js +1 -1
- package/lib/rules/enforce-positive-naming.js +132 -0
- package/package.json +1 -1
- package/release-manifest.json +14 -0
package/lib/index.js
CHANGED
|
@@ -921,6 +921,122 @@ const BOOLEAN_POSITIVE_ALTERNATIVES = {
|
|
|
921
921
|
WILL_NOT: ['WILL'],
|
|
922
922
|
DOES_NOT: ['DOES'],
|
|
923
923
|
};
|
|
924
|
+
/**
|
|
925
|
+
* Recognizes type nodes that denote a boolean-only value: the `boolean`
|
|
926
|
+
* keyword, a `true`/`false` literal type, or a union whose every arm is
|
|
927
|
+
* boolean. A validator's `string | true` return type is deliberately NOT
|
|
928
|
+
* boolean-only, so a predicate annotated that way is not treated as a boolean.
|
|
929
|
+
*/
|
|
930
|
+
function isBooleanOnlyType(typeNode) {
|
|
931
|
+
if (typeNode.type === utils_1.AST_NODE_TYPES.TSBooleanKeyword) {
|
|
932
|
+
return true;
|
|
933
|
+
}
|
|
934
|
+
if (typeNode.type === utils_1.AST_NODE_TYPES.TSLiteralType &&
|
|
935
|
+
typeNode.literal.type === utils_1.AST_NODE_TYPES.Literal &&
|
|
936
|
+
typeof typeNode.literal.value === 'boolean') {
|
|
937
|
+
return true;
|
|
938
|
+
}
|
|
939
|
+
if (typeNode.type === utils_1.AST_NODE_TYPES.TSUnionType) {
|
|
940
|
+
return typeNode.types.every(isBooleanOnlyType);
|
|
941
|
+
}
|
|
942
|
+
return false;
|
|
943
|
+
}
|
|
944
|
+
/**
|
|
945
|
+
* Detects an expression that is definitively non-boolean — a string or number
|
|
946
|
+
* literal, or a template/object/array literal. Validator predicates return an
|
|
947
|
+
* error-message string on rejection (and `true` on success), so a string
|
|
948
|
+
* literal return is the syntactic tell that an `is`/`has`-prefixed value is not
|
|
949
|
+
* actually a boolean. Ambiguous expressions (calls, identifiers, comparisons,
|
|
950
|
+
* negations) are intentionally NOT treated as non-boolean, preserving the
|
|
951
|
+
* rule's existing flagging of genuine negatively-named booleans.
|
|
952
|
+
*/
|
|
953
|
+
function isDefinitelyNonBooleanExpression(node) {
|
|
954
|
+
switch (node.type) {
|
|
955
|
+
case utils_1.AST_NODE_TYPES.Literal:
|
|
956
|
+
return typeof node.value === 'string' || typeof node.value === 'number';
|
|
957
|
+
case utils_1.AST_NODE_TYPES.TemplateLiteral:
|
|
958
|
+
case utils_1.AST_NODE_TYPES.ObjectExpression:
|
|
959
|
+
case utils_1.AST_NODE_TYPES.ArrayExpression:
|
|
960
|
+
return true;
|
|
961
|
+
case utils_1.AST_NODE_TYPES.ConditionalExpression:
|
|
962
|
+
return (isDefinitelyNonBooleanExpression(node.consequent) ||
|
|
963
|
+
isDefinitelyNonBooleanExpression(node.alternate));
|
|
964
|
+
default:
|
|
965
|
+
return false;
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
/**
|
|
969
|
+
* Yields the immediate AST-node children of `node`, skipping the `parent`
|
|
970
|
+
* back-reference so traversal only walks downward.
|
|
971
|
+
*/
|
|
972
|
+
function childNodesOf(node) {
|
|
973
|
+
const children = [];
|
|
974
|
+
for (const key of Object.keys(node)) {
|
|
975
|
+
if (key === 'parent')
|
|
976
|
+
continue;
|
|
977
|
+
const value = node[key];
|
|
978
|
+
const candidates = Array.isArray(value) ? value : [value];
|
|
979
|
+
for (const candidate of candidates) {
|
|
980
|
+
if (candidate &&
|
|
981
|
+
typeof candidate === 'object' &&
|
|
982
|
+
typeof candidate.type === 'string') {
|
|
983
|
+
children.push(candidate);
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
return children;
|
|
988
|
+
}
|
|
989
|
+
/**
|
|
990
|
+
* Whether any `return` statement belonging to `fn`'s own body (not a nested
|
|
991
|
+
* function's) yields a definitively non-boolean value.
|
|
992
|
+
*/
|
|
993
|
+
function blockReturnsNonBoolean(block) {
|
|
994
|
+
const stack = [block];
|
|
995
|
+
while (stack.length > 0) {
|
|
996
|
+
const current = stack.pop();
|
|
997
|
+
// Do not descend into nested function scopes: their returns belong to them.
|
|
998
|
+
if (current !== block &&
|
|
999
|
+
(current.type === utils_1.AST_NODE_TYPES.FunctionDeclaration ||
|
|
1000
|
+
current.type === utils_1.AST_NODE_TYPES.FunctionExpression ||
|
|
1001
|
+
current.type === utils_1.AST_NODE_TYPES.ArrowFunctionExpression)) {
|
|
1002
|
+
continue;
|
|
1003
|
+
}
|
|
1004
|
+
if (current.type === utils_1.AST_NODE_TYPES.ReturnStatement &&
|
|
1005
|
+
current.argument &&
|
|
1006
|
+
isDefinitelyNonBooleanExpression(current.argument)) {
|
|
1007
|
+
return true;
|
|
1008
|
+
}
|
|
1009
|
+
for (const child of childNodesOf(current)) {
|
|
1010
|
+
stack.push(child);
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
return false;
|
|
1014
|
+
}
|
|
1015
|
+
/**
|
|
1016
|
+
* Whether a function backing an `is`/`has`-prefixed name actually returns a
|
|
1017
|
+
* non-boolean value — e.g. a validator predicate returning `string | true`. An
|
|
1018
|
+
* explicit return-type annotation is authoritative; otherwise the body's own
|
|
1019
|
+
* `return` statements (or the concise-arrow expression) are inspected.
|
|
1020
|
+
*/
|
|
1021
|
+
function functionReturnsNonBoolean(fn) {
|
|
1022
|
+
if (fn.returnType) {
|
|
1023
|
+
return !isBooleanOnlyType(fn.returnType.typeAnnotation);
|
|
1024
|
+
}
|
|
1025
|
+
if (fn.body.type !== utils_1.AST_NODE_TYPES.BlockStatement) {
|
|
1026
|
+
return isDefinitelyNonBooleanExpression(fn.body);
|
|
1027
|
+
}
|
|
1028
|
+
return blockReturnsNonBoolean(fn.body);
|
|
1029
|
+
}
|
|
1030
|
+
/**
|
|
1031
|
+
* When a declarator/property value is a function, whether that function is a
|
|
1032
|
+
* non-boolean predicate that must be exempt from boolean negative-naming.
|
|
1033
|
+
*/
|
|
1034
|
+
function isNonBooleanFunctionValue(node) {
|
|
1035
|
+
return (!!node &&
|
|
1036
|
+
(node.type === utils_1.AST_NODE_TYPES.ArrowFunctionExpression ||
|
|
1037
|
+
node.type === utils_1.AST_NODE_TYPES.FunctionExpression) &&
|
|
1038
|
+
functionReturnsNonBoolean(node));
|
|
1039
|
+
}
|
|
924
1040
|
exports.enforcePositiveNaming = (0, createRule_1.createRule)({
|
|
925
1041
|
name: 'enforce-positive-naming',
|
|
926
1042
|
meta: {
|
|
@@ -1152,6 +1268,12 @@ exports.enforcePositiveNaming = (0, createRule_1.createRule)({
|
|
|
1152
1268
|
// Only check boolean-like variables
|
|
1153
1269
|
if (!isBooleanLike(node.id) && !isBooleanLike(node))
|
|
1154
1270
|
return;
|
|
1271
|
+
// A validator predicate (e.g. returns `string | true`) is name-prefixed
|
|
1272
|
+
// with `is`/`has` but is not a boolean, so its domain-correct negation
|
|
1273
|
+
// ("isNotBlank") must not be flagged. The name heuristic alone cannot
|
|
1274
|
+
// tell them apart; the initializer's return shape can.
|
|
1275
|
+
if (isNonBooleanFunctionValue(node.init))
|
|
1276
|
+
return;
|
|
1155
1277
|
const variableName = node.id.name;
|
|
1156
1278
|
const { isNegative, alternatives } = hasBooleanNegativeNaming(variableName);
|
|
1157
1279
|
if (isNegative) {
|
|
@@ -1192,6 +1314,10 @@ exports.enforcePositiveNaming = (0, createRule_1.createRule)({
|
|
|
1192
1314
|
// Only check boolean-returning functions
|
|
1193
1315
|
if (!isBooleanLike(node.id || node))
|
|
1194
1316
|
return;
|
|
1317
|
+
// Skip validator predicates that return a non-boolean value (e.g.
|
|
1318
|
+
// `string | true`), whose negation is the domain-correct term.
|
|
1319
|
+
if (functionReturnsNonBoolean(node))
|
|
1320
|
+
return;
|
|
1195
1321
|
const { isNegative, alternatives } = hasBooleanNegativeNaming(functionName);
|
|
1196
1322
|
if (isNegative) {
|
|
1197
1323
|
context.report({
|
|
@@ -1213,6 +1339,9 @@ exports.enforcePositiveNaming = (0, createRule_1.createRule)({
|
|
|
1213
1339
|
// Only check boolean-returning methods
|
|
1214
1340
|
if (!isBooleanLike(node.key))
|
|
1215
1341
|
return;
|
|
1342
|
+
// Skip validator predicates returning a non-boolean value.
|
|
1343
|
+
if (isNonBooleanFunctionValue(node.value))
|
|
1344
|
+
return;
|
|
1216
1345
|
const methodName = node.key.name;
|
|
1217
1346
|
const { isNegative, alternatives } = hasBooleanNegativeNaming(methodName);
|
|
1218
1347
|
if (isNegative) {
|
|
@@ -1235,6 +1364,9 @@ exports.enforcePositiveNaming = (0, createRule_1.createRule)({
|
|
|
1235
1364
|
// Only check boolean properties
|
|
1236
1365
|
if (!isBooleanLike(node.key))
|
|
1237
1366
|
return;
|
|
1367
|
+
// Skip validator predicates returning a non-boolean value.
|
|
1368
|
+
if (isNonBooleanFunctionValue(node.value))
|
|
1369
|
+
return;
|
|
1238
1370
|
const propertyName = node.key.name;
|
|
1239
1371
|
const { isNegative, alternatives } = hasBooleanNegativeNaming(propertyName);
|
|
1240
1372
|
if (isNegative) {
|
package/package.json
CHANGED
package/release-manifest.json
CHANGED
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
[
|
|
2
|
+
{
|
|
3
|
+
"version": "1.19.31",
|
|
4
|
+
"date": "2026-07-24T00:26:32.378Z",
|
|
5
|
+
"rules": [
|
|
6
|
+
{
|
|
7
|
+
"name": "enforce-positive-naming",
|
|
8
|
+
"changeType": "fix",
|
|
9
|
+
"issues": [
|
|
10
|
+
1344
|
|
11
|
+
],
|
|
12
|
+
"summary": "don't flag non-boolean validator predicates (closes #1344)"
|
|
13
|
+
}
|
|
14
|
+
]
|
|
15
|
+
},
|
|
2
16
|
{
|
|
3
17
|
"version": "1.19.30",
|
|
4
18
|
"date": "2026-07-23T22:28:34.611Z",
|