@bigbinary/eslint-plugin-neeto 1.12.3 → 1.13.1

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.
Files changed (3) hide show
  1. package/README.md +2 -0
  2. package/dist/index.js +185 -1
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -90,6 +90,7 @@ of the issues identified in JavaScript code.
90
90
  - [prefix-neeto-ui-import-alias](./docs/src/rules/prefix-neeto-ui-import-alias.md)
91
91
  - [routes-should-be-declared-and-default-exported](./docs/src/rules/routes-should-be-declared-and-default-exported.md)
92
92
  - [routes-should-match-object-path](./docs/src/rules/routes-should-match-object-path.md)
93
+ - [routes-should-use-kebab-case](./docs/src/rules/routes-should-use-kebab-case.md)
93
94
  - [use-array-methods](./docs/src/rules/use-array-methods.md)
94
95
  - [use-axios-config-for-passing-query-parameters](./docs/src/rules/use-axios-config-for-passing-query-parameters.md)
95
96
  - [use-camel-case-or-pascal-case-for-function-names](./docs/src/rules/use-camel-case-or-pascal-case-for-function-names.md)
@@ -120,6 +121,7 @@ of the issues identified in JavaScript code.
120
121
  - [ensure-peer-dependencies-are-in-sync](./docs/src/rules/ensure-peer-dependencies-are-in-sync.md)
121
122
  - [use-dayjs-from-neeto-commons-fronted](./docs/src/rules/use-dayjs-from-neeto-commons-fronted.md)
122
123
  - [standardize-help-urls](./docs/src/rules/standardize-help-urls.md)
124
+ - [use-yup-1-compatible-when](./docs/src/rules/use-yup-1-compatible-when.md)
123
125
 
124
126
  ### Warnings
125
127
 
package/dist/index.js CHANGED
@@ -130,6 +130,7 @@
130
130
  "@bigbinary/neeto/ramda-tips": "warn",
131
131
  "@bigbinary/neeto/routes-should-be-declared-and-default-exported": "error",
132
132
  "@bigbinary/neeto/routes-should-match-object-path": "error",
133
+ "@bigbinary/neeto/routes-should-use-kebab-case": "error",
133
134
  "@bigbinary/neeto/use-array-methods": "warn",
134
135
  "@bigbinary/neeto/use-axios-config-for-passing-query-parameters": "warn",
135
136
  "@bigbinary/neeto/use-camel-case-or-pascal-case-for-function-names": "error",
@@ -169,6 +170,7 @@
169
170
  "@bigbinary/neeto/jsx-prefer-guard-clause": "warn",
170
171
  "@bigbinary/neeto/ensure-usestate-has-initial-value": "error",
171
172
  "@bigbinary/neeto/use-show-thumbs-up-toastr": "error",
173
+ "@bigbinary/neeto/use-yup-1-compatible-when": "error",
172
174
  "@bigbinary/neeto/ensure-peer-dependencies-are-in-sync": "warn",
173
175
  "@bigbinary/neeto/standardize-help-urls": "error"
174
176
  };
@@ -284,7 +286,7 @@
284
286
  }
285
287
  });
286
288
 
287
- var version = "1.12.2";
289
+ var version = "1.13.0";
288
290
  var packageJson = {
289
291
  version: version};
290
292
 
@@ -463,6 +465,7 @@
463
465
  const PREFIX_NEETO_UI_IMPORT_ALIAS_MESSAGE = "Aliases for anything imported from neetoUI or neetoIcons should start with `NeetoUI`";
464
466
  const REPLACE_WITH_HANDLE_META_CLICK = "Replace with handleMetaClick function";
465
467
  const ROUTE_SHOULD_BE_A_STRING = "Route should be a plain string literal.";
468
+ const routeShouldUseKebabCase = segments => `Use kebab-case in route paths: ${segments.map(([segment, kebab]) => `\`${segment}\` should be \`${kebab}\``).join(", ")}.`;
466
469
  const ROUTE_SHOULD_MATCH_OBJECT_PATH = "Route string should match with its object path.";
467
470
  const ROUTES_HAS_ALREADY_BEEN_DECLARED = "Identifier `routes` has already been declared";
468
471
  const ROUTES_CONSTANT_SHOULD_BE_DECLARED = "A constant object named `routes` should be declared inside routes.js";
@@ -547,6 +550,8 @@
547
550
  const INVALID_SWITCH_CHILD_MESSAGE = "Switch component can only contain Route, Redirect, or PrivateRoute as direct children";
548
551
  const PREFER_RADIO_BUTTONS_FOR_FEW_OPTIONS = "Consider using Radio buttons instead of Dropdown/Select when there are fewer than 4 options.";
549
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 works the same on yup 0.32.";
554
+ const YUP_WHEN_BUILDER_SHOULD_USE_WHEN_VALUES = "yup 1 passes a .when() builder the dependency values as one array, not spread, so this builder's conditions would silently stop matching. Wrap it in `whenValues` from `utils/yup`, which works on yup 0.32 and 1.";
550
555
 
551
556
  const PACKAGES_TO_CHECK = ["@bigbinary/neeto-molecules", "@bigbinary/neetoui", "@bigbinary/neeto-commons-frontend"];
552
557
 
@@ -970,6 +975,9 @@
970
975
 
971
976
  const SPECIAL_KEYS = ["home", "index", "show"];
972
977
 
978
+ // Devise owns these route segments, so apps can't rename them.
979
+ const DEVISE_SEGMENTS = ["sign_in", "sign_out", "sign_up"];
980
+
973
981
  // import routes from "routes", import routes from "src/routes"
974
982
  const ROUTES_IMPORT_PATTERN = {
975
983
  type: TYPES.IMPORT_DECLARATION,
@@ -1231,6 +1239,42 @@
1231
1239
  })
1232
1240
  };
1233
1241
 
1242
+ const toKebabCase = segment => segment.replace(/([a-z\d])([A-Z])/g, "$1-$2").replace(/_/g, "-").toLowerCase();
1243
+ const isNotKebabCase = segment => /[_A-Z]/.test(segment);
1244
+
1245
+ // Params like ":ticketId" and patterns like "*" never appear in the URL.
1246
+ const isPathSegment = segment => !/^[:*(]/.test(segment) && !DEVISE_SEGMENTS.includes(segment);
1247
+ const checkRoutes = (context, node) => {
1248
+ if (node?.type === TYPES.OBJECT_EXPRESSION) {
1249
+ node.properties.forEach(property => checkRoutes(context, property.value));
1250
+ return;
1251
+ }
1252
+ if (node?.type !== TYPES.LITERAL || typeof node.value !== "string") return;
1253
+ const badSegments = node.value.split("/").map(segment => segment.split("?")[0]).filter(isPathSegment).filter(isNotKebabCase);
1254
+ if (ramda.isEmpty(badSegments)) return;
1255
+ context.report({
1256
+ node,
1257
+ message: routeShouldUseKebabCase(badSegments.map(segment => [segment, toKebabCase(segment)]))
1258
+ });
1259
+ };
1260
+ var routesShouldUseKebabCase = {
1261
+ meta: {
1262
+ type: "problem",
1263
+ docs: {
1264
+ url: "https://github.com/neetozone/eslint-plugin-neeto/blob/main/docs/src/rules/routes-should-use-kebab-case.md",
1265
+ description: "Route paths in the routes object should use kebab-case."
1266
+ }
1267
+ },
1268
+ create: context => ({
1269
+ VariableDeclaration(node) {
1270
+ if (!context.getFilename().endsWith(ROUTES_FILE_PATH)) return;
1271
+ if (neetoCist.matches(DECALARATION_PATTERN$1, node)) {
1272
+ checkRoutes(context, node.declarations[0].init);
1273
+ }
1274
+ }
1275
+ })
1276
+ };
1277
+
1234
1278
  const getCode = (context, start, length) => context.getSourceCode().text.substr(start, length);
1235
1279
  const replacer$2 = nodeReplacerWithImports(ramda.assocPath(["source", "value"], "neetocommons/utils", IMPORT_DECLARATION));
1236
1280
  var useCommonRoutes = {
@@ -11762,6 +11806,144 @@
11762
11806
  }
11763
11807
  };
11764
11808
 
11809
+ const FUNCTION_TYPES = [TYPES.ARROW_FUNCTION_EXPRESSION, TYPES.FUNCTION_EXPRESSION];
11810
+ const BRANCH_KEYS = ["then", "otherwise"];
11811
+ const SCHEMA_PARAM_NAMES = ["schema", "baseSchema", "currentSchema"];
11812
+ const SUSPENDING_KEYWORDS = ["await", "yield"];
11813
+ const WHEN_VALUES = "whenValues";
11814
+
11815
+ const isFunction = node => FUNCTION_TYPES.includes(node?.type);
11816
+ const keyName = property => property.key?.name ?? property.key?.value;
11817
+ const isYupSource = source => source === "yup" || source.startsWith("yup/");
11818
+ const isWhenCall = node => node?.type === TYPES.CALL_EXPRESSION && node.callee.type === TYPES.MEMBER_EXPRESSION && !node.callee.computed && node.callee.property.name === "when";
11819
+ const findVariable = (scope, name) => {
11820
+ for (let current = scope; current; current = current.upper) {
11821
+ const variable = current.set.get(name);
11822
+ if (variable) return variable;
11823
+ }
11824
+ return null;
11825
+ };
11826
+
11827
+ // The identifier at the start of a chain such as `yup.string().trim()`.
11828
+ const chainRoot = node => {
11829
+ let current = node;
11830
+ while (current) {
11831
+ if (current.type === TYPES.CALL_EXPRESSION) current = current.callee;else if (current.type === TYPES.MEMBER_EXPRESSION) current = current.object;else if (current.type === "AwaitExpression") current = current.argument;else return current;
11832
+ }
11833
+ return null;
11834
+ };
11835
+
11836
+ // Whether `fn` is a `.when()` builder or branch, so its parameter is a schema.
11837
+ const isWhenFunction = fn => {
11838
+ const {
11839
+ parent
11840
+ } = fn;
11841
+ if (isWhenCall(parent)) return parent.arguments.includes(fn);
11842
+ if (parent?.type === TYPES.CALL_EXPRESSION && parent.callee.type === TYPES.IDENTIFIER && parent.callee.name === WHEN_VALUES) {
11843
+ return isWhenCall(parent.parent);
11844
+ }
11845
+ return parent?.type === TYPES.PROPERTY && BRANCH_KEYS.includes(keyName(parent)) && isWhenCall(parent.parent?.parent);
11846
+ };
11847
+
11848
+ // Whether `node` evaluates to a yup schema: a chain on a yup import, a variable
11849
+ // initialised with one, or the schema parameter of a .when() builder or branch.
11850
+ const createYupSchemaCheck = sourceCode => {
11851
+ const visiting = new Set();
11852
+ const isYupSchema = node => {
11853
+ const root = chainRoot(node);
11854
+ if (root?.type !== TYPES.IDENTIFIER) return false;
11855
+ const variable = findVariable(sourceCode.getScope(root), root.name);
11856
+ const [definition] = variable?.defs ?? [];
11857
+ if (!definition || visiting.has(definition.name)) return false;
11858
+ if (definition.type === "ImportBinding") {
11859
+ return isYupSource(definition.parent.source.value);
11860
+ }
11861
+ if (definition.type === "Parameter") {
11862
+ return isFunction(definition.node) && isWhenFunction(definition.node);
11863
+ }
11864
+ if (definition.type !== "Variable" || !definition.node.init) return false;
11865
+ visiting.add(definition.name);
11866
+ const result = isYupSchema(definition.node.init);
11867
+ visiting.delete(definition.name);
11868
+ return result;
11869
+ };
11870
+ return isYupSchema;
11871
+ };
11872
+
11873
+ // A parameter name the moved expression does not already use.
11874
+ const freeParamName = (sourceCode, node) => {
11875
+ const text = sourceCode.getText(node);
11876
+ return SCHEMA_PARAM_NAMES.find(name => !new RegExp(`\\b${name}\\b`).test(text));
11877
+ };
11878
+
11879
+ // Moving `await` or `yield` into a plain arrow function would not parse.
11880
+ const suspends = (sourceCode, node) => sourceCode.getTokens(node).some(({
11881
+ type,
11882
+ value
11883
+ }) => ["Keyword", "Identifier"].includes(type) ? SUSPENDING_KEYWORDS.includes(value) : false);
11884
+ const buildFix = (sourceCode, property, value) => {
11885
+ if (suspends(sourceCode, value)) return null;
11886
+ const param = freeParamName(sourceCode, value);
11887
+ if (!param) return null;
11888
+ const branch = `${param} => ${param}.concat(${sourceCode.getText(value)})`;
11889
+
11890
+ // Replace only the value, so the key and any comments around it are kept.
11891
+ return fixer => property.shorthand ? fixer.replaceText(property, `${keyName(property)}: ${branch}`) : fixer.replaceText(value, branch);
11892
+ };
11893
+ const checkBranches = (context, isYupSchema, config) => {
11894
+ const {
11895
+ sourceCode
11896
+ } = context;
11897
+ const properties = config.properties.filter(property => property.type === TYPES.PROPERTY && !property.computed);
11898
+ if (!properties.some(property => keyName(property) === "is")) return;
11899
+ properties.filter(property => BRANCH_KEYS.includes(keyName(property))).forEach(property => {
11900
+ const {
11901
+ value
11902
+ } = property;
11903
+ // Only a value known to be a yup schema is a mistake. A function
11904
+ // reference such as `identity` or `branches.required` is already valid.
11905
+ if (isFunction(value) || !isYupSchema(value)) return;
11906
+ context.report({
11907
+ node: value,
11908
+ message: YUP_WHEN_BRANCH_SHOULD_BE_FUNCTION,
11909
+ fix: buildFix(sourceCode, property, value)
11910
+ });
11911
+ });
11912
+ };
11913
+ var useYup1CompatibleWhen = {
11914
+ meta: {
11915
+ type: "problem",
11916
+ fixable: "code",
11917
+ docs: {
11918
+ 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 also work on yup 1."
11920
+ }
11921
+ },
11922
+ create: context => {
11923
+ let importsYup = false;
11924
+ const isYupSchema = createYupSchemaCheck(context.sourceCode);
11925
+ return {
11926
+ ImportDeclaration(node) {
11927
+ if (isYupSource(node.source.value)) importsYup = true;
11928
+ },
11929
+ CallExpression(node) {
11930
+ if (!importsYup || !isWhenCall(node)) return;
11931
+ if (!isYupSchema(node.callee.object)) return;
11932
+ const [, config] = node.arguments;
11933
+ if (!config) return;
11934
+ if (config.type === TYPES.OBJECT_EXPRESSION) {
11935
+ checkBranches(context, isYupSchema, config);
11936
+ } else if (isFunction(config)) {
11937
+ context.report({
11938
+ node: config,
11939
+ message: YUP_WHEN_BUILDER_SHOULD_USE_WHEN_VALUES
11940
+ });
11941
+ }
11942
+ }
11943
+ };
11944
+ }
11945
+ };
11946
+
11765
11947
  // pick()
11766
11948
  // eslint-disable-next-line import/exports-last
11767
11949
  const PICK_PATTERN_SHORT = {
@@ -12268,6 +12450,7 @@
12268
12450
  "ramda-tips": ramdaTips,
12269
12451
  "routes-should-be-declared-and-default-exported": routesShouldBeDeclaredAndDefaultExported,
12270
12452
  "routes-should-match-object-path": routesShouldMatchObjectPath,
12453
+ "routes-should-use-kebab-case": routesShouldUseKebabCase,
12271
12454
  "use-array-methods": useArrayMethods,
12272
12455
  "use-axios-config-for-passing-query-parameters": useAxiosConfigForPassingQueryParameters,
12273
12456
  "no-buildurl-call-with-empty-params": noBuildurlCallWithEmptyParams,
@@ -12308,6 +12491,7 @@
12308
12491
  "jsx-prefer-guard-clause": jsxPreferGuardClause,
12309
12492
  "ensure-usestate-has-initial-value": ensureUseStateHasInitialValue,
12310
12493
  "use-show-thumbs-up-toastr": useShowThumbsUpToastr,
12494
+ "use-yup-1-compatible-when": useYup1CompatibleWhen,
12311
12495
  "standardize-help-urls": standardizeHelpUrls
12312
12496
  },
12313
12497
  configs: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigbinary/eslint-plugin-neeto",
3
- "version": "1.12.3",
3
+ "version": "1.13.1",
4
4
  "main": "dist/index.js",
5
5
  "license": "UNLICENSED",
6
6
  "scripts": {