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