no-restricted-syntax.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "disallow specified syntax",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/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
  41. restrictedSyntax: "{{message}}"
  42. }
  43. },
  44. create(context) {
  45. return context.options.reduce((result, selectorOrObject) => {
  46. const isStringFormat = (typeof selectorOrObject === "string");
  47. const hasCustomMessage = !isStringFormat && Boolean(selectorOrObject.message);
  48. const selector = isStringFormat ? selectorOrObject : selectorOrObject.selector;
  49. const message = hasCustomMessage ? selectorOrObject.message : `Using '${selector}' is not allowed.`;
  50. return Object.assign(result, {
  51. [selector](node) {
  52. context.report({
  53. node,
  54. messageId: "restrictedSyntax",
  55. data: { message }
  56. });
  57. }
  58. });
  59. }, {});
  60. }
  61. };