no-multi-assign.js 1.8 KB

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