@bigbinary/eslint-plugin-neeto 1.13.1 → 1.13.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/dist/index.js +114 -10
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -286,7 +286,7 @@
|
|
|
286
286
|
}
|
|
287
287
|
});
|
|
288
288
|
|
|
289
|
-
var version = "1.13.
|
|
289
|
+
var version = "1.13.1";
|
|
290
290
|
var packageJson = {
|
|
291
291
|
version: version};
|
|
292
292
|
|
|
@@ -550,8 +550,9 @@
|
|
|
550
550
|
const INVALID_SWITCH_CHILD_MESSAGE = "Switch component can only contain Route, Redirect, or PrivateRoute as direct children";
|
|
551
551
|
const PREFER_RADIO_BUTTONS_FOR_FEW_OPTIONS = "Consider using Radio buttons instead of Dropdown/Select when there are fewer than 4 options.";
|
|
552
552
|
const NO_EDITS_TO_GENERATED_TRANSLATION_FILES = "This translation file is auto-generated. Only edit 'en.json'; translations for all other locales are generated from it automatically. Please revert your changes in this file.";
|
|
553
|
-
const YUP_WHEN_BRANCH_SHOULD_BE_FUNCTION = "yup 1 needs `then` and `otherwise` to be functions of the base schema. Use `schema => schema.concat(...)`, which
|
|
554
|
-
const
|
|
553
|
+
const YUP_WHEN_BRANCH_SHOULD_BE_FUNCTION = "yup 1 needs `then` and `otherwise` to be functions of the base schema. Use `schema => schema.concat(...)`, which is what yup 0.32 did with a schema branch.";
|
|
554
|
+
const YUP_WHEN_BUILDER_SHOULD_DESTRUCTURE_VALUES = "yup 1 passes a .when() builder the dependency values as one array, so a builder that reads them spread gets that array and its conditions silently stop matching. Destructure them: `([a, b], schema) =>`.";
|
|
555
|
+
const YUP_WHEN_VALUES_IS_NOT_NEEDED = "`whenValues` was a bridge for yup 0.32, and every product is on yup 1 now. Pass the builder to `.when()` directly and destructure its values: `([a, b], schema) =>`.";
|
|
555
556
|
|
|
556
557
|
const PACKAGES_TO_CHECK = ["@bigbinary/neeto-molecules", "@bigbinary/neetoui", "@bigbinary/neeto-commons-frontend"];
|
|
557
558
|
|
|
@@ -11814,7 +11815,10 @@
|
|
|
11814
11815
|
|
|
11815
11816
|
const isFunction = node => FUNCTION_TYPES.includes(node?.type);
|
|
11816
11817
|
const keyName = property => property.key?.name ?? property.key?.value;
|
|
11817
|
-
|
|
11818
|
+
|
|
11819
|
+
// `utils/yup` is where products and nanos keep their yup setup; some re-export
|
|
11820
|
+
// yup from it.
|
|
11821
|
+
const isYupSource = source => source === "yup" || source.startsWith("yup/") || source === "utils/yup";
|
|
11818
11822
|
const isWhenCall = node => node?.type === TYPES.CALL_EXPRESSION && node.callee.type === TYPES.MEMBER_EXPRESSION && !node.callee.computed && node.callee.property.name === "when";
|
|
11819
11823
|
const findVariable = (scope, name) => {
|
|
11820
11824
|
for (let current = scope; current; current = current.upper) {
|
|
@@ -11910,13 +11914,108 @@
|
|
|
11910
11914
|
});
|
|
11911
11915
|
});
|
|
11912
11916
|
};
|
|
11917
|
+
const isWhenValuesCall = node => node?.type === TYPES.CALL_EXPRESSION && node.callee.type === TYPES.IDENTIFIER && node.callee.name === WHEN_VALUES;
|
|
11918
|
+
|
|
11919
|
+
// How many dependency values `.when(keys, …)` passes, or null if unknown.
|
|
11920
|
+
const keyCount = keys => {
|
|
11921
|
+
if (keys?.type === "ArrayExpression") {
|
|
11922
|
+
return keys.elements.every(element => element?.type !== "SpreadElement") ? keys.elements.length : null;
|
|
11923
|
+
}
|
|
11924
|
+
return keys?.type === TYPES.LITERAL && typeof keys.value === "string" ? 1 : null;
|
|
11925
|
+
};
|
|
11926
|
+
|
|
11927
|
+
// `(a, b, schema) => …` as `([a, b], schema) => …`, the yup 1 builder form.
|
|
11928
|
+
const destructuredParamsText = (sourceCode, builder, count) => {
|
|
11929
|
+
const {
|
|
11930
|
+
params
|
|
11931
|
+
} = builder;
|
|
11932
|
+
if (count === null || params.length === 0) return null;
|
|
11933
|
+
const valueParams = params.slice(0, count);
|
|
11934
|
+
if (valueParams.some(param => param.type === "RestElement")) return null;
|
|
11935
|
+
const texts = params.map(param => sourceCode.getText(param));
|
|
11936
|
+
const rest = texts.slice(count);
|
|
11937
|
+
return [`[${texts.slice(0, count).join(", ")}]`, ...rest].join(", ");
|
|
11938
|
+
};
|
|
11939
|
+
|
|
11940
|
+
// The builder's source with its parameters destructured.
|
|
11941
|
+
const destructuredBuilderText = (sourceCode, builder, count) => {
|
|
11942
|
+
const paramsText = destructuredParamsText(sourceCode, builder, count);
|
|
11943
|
+
if (paramsText === null) return null;
|
|
11944
|
+
const {
|
|
11945
|
+
params
|
|
11946
|
+
} = builder;
|
|
11947
|
+
const text = sourceCode.getText(builder);
|
|
11948
|
+
const start = params[0].range[0] - builder.range[0];
|
|
11949
|
+
const end = params.at(-1).range[1] - builder.range[0];
|
|
11950
|
+
const isParenthesized = sourceCode.getTokenBefore(params[0]).value === "(" && sourceCode.getTokenBefore(params[0]).range[0] >= builder.range[0];
|
|
11951
|
+
const head = isParenthesized ? paramsText : `(${paramsText})`;
|
|
11952
|
+
return text.slice(0, start) + head + text.slice(end);
|
|
11953
|
+
};
|
|
11954
|
+
|
|
11955
|
+
// `[a, b]` or `[a, b] = []`: the builder already destructures yup 1's values.
|
|
11956
|
+
const isArrayPattern = param => param.type === "ArrayPattern" || param.type === "AssignmentPattern" && param.left.type === "ArrayPattern";
|
|
11957
|
+
|
|
11958
|
+
// Whether a builder's first parameter is used as the values array:
|
|
11959
|
+
// `values[0]`, `values.length`, `...values` or `const [a] = values`. Property
|
|
11960
|
+
// reads such as `field.value` are not counted, since a dependency value is often
|
|
11961
|
+
// an object, and reading it spread is exactly the mistake this rule reports.
|
|
11962
|
+
const readsAsValuesArray = (sourceCode, builder, param) => {
|
|
11963
|
+
if (param.type !== TYPES.IDENTIFIER) return false;
|
|
11964
|
+
const variable = sourceCode.getDeclaredVariables(builder).find(({
|
|
11965
|
+
name
|
|
11966
|
+
}) => name === param.name);
|
|
11967
|
+
return Boolean(variable?.references.some(({
|
|
11968
|
+
identifier
|
|
11969
|
+
}) => {
|
|
11970
|
+
const {
|
|
11971
|
+
parent
|
|
11972
|
+
} = identifier;
|
|
11973
|
+
if (parent.type === TYPES.MEMBER_EXPRESSION && parent.object === identifier) {
|
|
11974
|
+
return parent.computed ? parent.property.type === TYPES.LITERAL && typeof parent.property.value === "number" : parent.property.name === "length";
|
|
11975
|
+
}
|
|
11976
|
+
if (parent.type === "SpreadElement") return true;
|
|
11977
|
+
return parent.type === "VariableDeclarator" && parent.init === identifier && parent.id.type === "ArrayPattern";
|
|
11978
|
+
}));
|
|
11979
|
+
};
|
|
11980
|
+
const checkBuilder = (context, whenCall, builder) => {
|
|
11981
|
+
const [firstParam] = builder.params;
|
|
11982
|
+
if (!firstParam || isArrayPattern(firstParam)) return;
|
|
11983
|
+
const {
|
|
11984
|
+
sourceCode
|
|
11985
|
+
} = context;
|
|
11986
|
+
if (readsAsValuesArray(sourceCode, builder, firstParam)) return;
|
|
11987
|
+
const text = destructuredBuilderText(sourceCode, builder, keyCount(whenCall.arguments[0]));
|
|
11988
|
+
context.report({
|
|
11989
|
+
node: builder,
|
|
11990
|
+
message: YUP_WHEN_BUILDER_SHOULD_DESTRUCTURE_VALUES,
|
|
11991
|
+
fix: text === null ? null : fixer => fixer.replaceText(builder, text)
|
|
11992
|
+
});
|
|
11993
|
+
};
|
|
11994
|
+
const checkWhenValues = (context, whenCall, wrapper) => {
|
|
11995
|
+
const {
|
|
11996
|
+
sourceCode
|
|
11997
|
+
} = context;
|
|
11998
|
+
const [builder] = wrapper.arguments;
|
|
11999
|
+
let text = null;
|
|
12000
|
+
|
|
12001
|
+
// `whenValues` spreads the values, so every value parameter, even an array
|
|
12002
|
+
// pattern such as `[firstTag]`, moves inside the new outer array.
|
|
12003
|
+
if (wrapper.arguments.length === 1 && isFunction(builder)) {
|
|
12004
|
+
text = destructuredBuilderText(sourceCode, builder, keyCount(whenCall.arguments[0]));
|
|
12005
|
+
}
|
|
12006
|
+
context.report({
|
|
12007
|
+
node: wrapper,
|
|
12008
|
+
message: YUP_WHEN_VALUES_IS_NOT_NEEDED,
|
|
12009
|
+
fix: text === null ? null : fixer => fixer.replaceText(wrapper, text)
|
|
12010
|
+
});
|
|
12011
|
+
};
|
|
11913
12012
|
var useYup1CompatibleWhen = {
|
|
11914
12013
|
meta: {
|
|
11915
12014
|
type: "problem",
|
|
11916
12015
|
fixable: "code",
|
|
11917
12016
|
docs: {
|
|
11918
12017
|
url: "https://github.com/neetozone/eslint-plugin-neeto/blob/main/docs/src/rules/use-yup-1-compatible-when.md",
|
|
11919
|
-
description: "yup .when() conditions should use function branches and builders that
|
|
12018
|
+
description: "yup .when() conditions should use function branches and builders that destructure their values, as yup 1 needs."
|
|
11920
12019
|
}
|
|
11921
12020
|
},
|
|
11922
12021
|
create: context => {
|
|
@@ -11928,16 +12027,21 @@
|
|
|
11928
12027
|
},
|
|
11929
12028
|
CallExpression(node) {
|
|
11930
12029
|
if (!importsYup || !isWhenCall(node)) return;
|
|
11931
|
-
if (!isYupSchema(node.callee.object)) return;
|
|
11932
12030
|
const [, config] = node.arguments;
|
|
11933
12031
|
if (!config) return;
|
|
12032
|
+
|
|
12033
|
+
// `whenValues` only ever wraps a yup builder, so its receiver need not
|
|
12034
|
+
// be traced, which matters for schemas from helpers such as
|
|
12035
|
+
// `buildNullableSchema()`.
|
|
12036
|
+
if (isWhenValuesCall(config)) {
|
|
12037
|
+
checkWhenValues(context, node, config);
|
|
12038
|
+
return;
|
|
12039
|
+
}
|
|
12040
|
+
if (!isYupSchema(node.callee.object)) return;
|
|
11934
12041
|
if (config.type === TYPES.OBJECT_EXPRESSION) {
|
|
11935
12042
|
checkBranches(context, isYupSchema, config);
|
|
11936
12043
|
} else if (isFunction(config)) {
|
|
11937
|
-
context
|
|
11938
|
-
node: config,
|
|
11939
|
-
message: YUP_WHEN_BUILDER_SHOULD_USE_WHEN_VALUES
|
|
11940
|
-
});
|
|
12044
|
+
checkBuilder(context, node, config);
|
|
11941
12045
|
}
|
|
11942
12046
|
}
|
|
11943
12047
|
};
|