no-multi-assign.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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('../shared/types').Rule} */
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. defaultOptions: [{
  14. ignoreNonDeclaration: false
  15. }],
  16. docs: {
  17. description: "Disallow use of chained assignment expressions",
  18. recommended: false,
  19. url: "https://eslint.org/docs/latest/rules/no-multi-assign"
  20. },
  21. schema: [{
  22. type: "object",
  23. properties: {
  24. ignoreNonDeclaration: {
  25. type: "boolean"
  26. }
  27. },
  28. additionalProperties: false
  29. }],
  30. messages: {
  31. unexpectedChain: "Unexpected chained assignment."
  32. }
  33. },
  34. create(context) {
  35. const [{ ignoreNonDeclaration }] = context.options;
  36. const selectors = [
  37. "VariableDeclarator > AssignmentExpression.init",
  38. "PropertyDefinition > AssignmentExpression.value"
  39. ];
  40. if (!ignoreNonDeclaration) {
  41. selectors.push("AssignmentExpression > AssignmentExpression.right");
  42. }
  43. return {
  44. [selectors](node) {
  45. context.report({
  46. node,
  47. messageId: "unexpectedChain"
  48. });
  49. }
  50. };
  51. }
  52. };