no-restricted-syntax.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @fileoverview Rule to flag use of certain node types
  3. * @author Burak Yigit Kaya
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../types').Rule.RuleModule} */
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "Disallow specified syntax",
  15. recommended: false,
  16. url: "https://eslint.org/docs/latest/rules/no-restricted-syntax",
  17. },
  18. schema: {
  19. type: "array",
  20. items: {
  21. oneOf: [
  22. {
  23. type: "string",
  24. },
  25. {
  26. type: "object",
  27. properties: {
  28. selector: { type: "string" },
  29. message: { type: "string" },
  30. },
  31. required: ["selector"],
  32. additionalProperties: false,
  33. },
  34. ],
  35. },
  36. uniqueItems: true,
  37. minItems: 0,
  38. },
  39. messages: {
  40. // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
  41. restrictedSyntax: "{{message}}",
  42. },
  43. },
  44. create(context) {
  45. return context.options.reduce((result, selectorOrObject) => {
  46. const isStringFormat = typeof selectorOrObject === "string";
  47. const hasCustomMessage =
  48. !isStringFormat && Boolean(selectorOrObject.message);
  49. const selector = isStringFormat
  50. ? selectorOrObject
  51. : selectorOrObject.selector;
  52. const message = hasCustomMessage
  53. ? selectorOrObject.message
  54. : `Using '${selector}' is not allowed.`;
  55. return Object.assign(result, {
  56. [selector](node) {
  57. context.report({
  58. node,
  59. messageId: "restrictedSyntax",
  60. data: { message },
  61. });
  62. },
  63. });
  64. }, {});
  65. },
  66. };