no-aArgs.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * @fileoverview warns against using hungarian notation in function arguments
  3. * (i.e. aArg).
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. */
  9. "use strict";
  10. function isPrefixed(name) {
  11. return name.length >= 2 && /^a[A-Z]/.test(name);
  12. }
  13. function deHungarianize(name) {
  14. return name.substring(1, 2).toLowerCase() + name.substring(2, name.length);
  15. }
  16. module.exports = {
  17. meta: {
  18. docs: {
  19. url:
  20. "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/no-aArgs.html",
  21. },
  22. type: "layout",
  23. },
  24. create(context) {
  25. function checkFunction(node) {
  26. for (var i = 0; i < node.params.length; i++) {
  27. var param = node.params[i];
  28. if (param.name && isPrefixed(param.name)) {
  29. var errorObj = {
  30. name: param.name,
  31. suggestion: deHungarianize(param.name),
  32. };
  33. context.report(
  34. param,
  35. "Parameter '{{name}}' uses Hungarian Notation, " +
  36. "consider using '{{suggestion}}' instead.",
  37. errorObj
  38. );
  39. }
  40. }
  41. }
  42. return {
  43. FunctionDeclaration: checkFunction,
  44. ArrowFunctionExpression: checkFunction,
  45. FunctionExpression: checkFunction,
  46. };
  47. },
  48. };