UNPKG

1.92 kBJavaScriptView Raw
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 docs: {
14 description: "disallow specified syntax",
15 category: "Stylistic Issues",
16 recommended: false
17 },
18
19 schema: {
20 type: "array",
21 items: [{
22 oneOf: [
23 {
24 type: "string"
25 },
26 {
27 type: "object",
28 properties: {
29 selector: { type: "string" },
30 message: { type: "string" }
31 },
32 required: ["selector"],
33 additionalProperties: false
34 }
35 ]
36 }],
37 uniqueItems: true,
38 minItems: 0
39 }
40 },
41
42 create(context) {
43 return context.options.reduce((result, selectorOrObject) => {
44 const isStringFormat = (typeof selectorOrObject === "string");
45 const hasCustomMessage = !isStringFormat && Boolean(selectorOrObject.message);
46
47 const selector = isStringFormat ? selectorOrObject : selectorOrObject.selector;
48 const message = hasCustomMessage ? selectorOrObject.message : "Using '{{selector}}' is not allowed.";
49
50 return Object.assign(result, {
51 [selector](node) {
52 context.report({
53 node,
54 message,
55 data: hasCustomMessage ? {} : { selector }
56 });
57 }
58 });
59 }, {});
60
61 }
62};