no-multi-assign.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * @fileoverview Rule to check use of chained assignment expressions
  3. * @author Stewart Rand
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../types').Rule.RuleModule} */
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. defaultOptions: [
  14. {
  15. ignoreNonDeclaration: false,
  16. },
  17. ],
  18. docs: {
  19. description: "Disallow use of chained assignment expressions",
  20. recommended: false,
  21. url: "https://eslint.org/docs/latest/rules/no-multi-assign",
  22. },
  23. schema: [
  24. {
  25. type: "object",
  26. properties: {
  27. ignoreNonDeclaration: {
  28. type: "boolean",
  29. },
  30. },
  31. additionalProperties: false,
  32. },
  33. ],
  34. messages: {
  35. unexpectedChain: "Unexpected chained assignment.",
  36. },
  37. },
  38. create(context) {
  39. const [{ ignoreNonDeclaration }] = context.options;
  40. const selectors = [
  41. "VariableDeclarator > AssignmentExpression.init",
  42. "PropertyDefinition > AssignmentExpression.value",
  43. ];
  44. if (!ignoreNonDeclaration) {
  45. selectors.push("AssignmentExpression > AssignmentExpression.right");
  46. }
  47. return {
  48. [selectors](node) {
  49. context.report({
  50. node,
  51. messageId: "unexpectedChain",
  52. });
  53. },
  54. };
  55. },
  56. };