]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-restricted-syntax.js
cf8bc41236644d7c46433c5e154a87af085cc949
[pve-eslint.git] / eslint / tests / lib / rules / no-restricted-syntax.js
1 /**
2 * @fileoverview Tests for `no-restricted-syntax` rule
3 * @author Burak Yigit Kaya
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const rule = require("../../../lib/rules/no-restricted-syntax"),
12 { RuleTester } = require("../../../lib/rule-tester");
13
14 //------------------------------------------------------------------------------
15 // Tests
16 //------------------------------------------------------------------------------
17
18 const ruleTester = new RuleTester();
19
20 ruleTester.run("no-restricted-syntax", rule, {
21 valid: [
22
23 // string format
24 "doSomething();",
25 { code: "var foo = 42;", options: ["ConditionalExpression"] },
26 { code: "foo += 42;", options: ["VariableDeclaration", "FunctionExpression"] },
27 { code: "foo;", options: ["Identifier[name=\"bar\"]"] },
28 { code: "() => 5", options: ["ArrowFunctionExpression > BlockStatement"], parserOptions: { ecmaVersion: 6 } },
29 { code: "({ foo: 1, bar: 2 })", options: ["Property > Literal.key"] },
30 { code: "A: for (;;) break;", options: ["BreakStatement[label]"] },
31 { code: "function foo(bar, baz) {}", options: ["FunctionDeclaration[params.length>2]"] },
32
33 // object format
34 { code: "var foo = 42;", options: [{ selector: "ConditionalExpression" }] },
35 { code: "({ foo: 1, bar: 2 })", options: [{ selector: "Property > Literal.key" }] },
36 {
37 code: "({ foo: 1, bar: 2 })",
38 options: [{ selector: "FunctionDeclaration[params.length>2]", message: "custom error message." }]
39 },
40
41 // https://github.com/eslint/eslint/issues/8733
42 { code: "console.log(/a/);", options: ["Literal[regex.flags=/./]"] }
43 ],
44 invalid: [
45
46 // string format
47 {
48 code: "var foo = 41;",
49 options: ["VariableDeclaration"],
50 errors: [{ messageId: "restrictedSyntax", data: { message: "Using 'VariableDeclaration' is not allowed." }, type: "VariableDeclaration" }]
51 },
52 {
53 code: ";function lol(a) { return 42; }",
54 options: ["EmptyStatement"],
55 errors: [{ messageId: "restrictedSyntax", data: { message: "Using 'EmptyStatement' is not allowed." }, type: "EmptyStatement" }]
56 },
57 {
58 code: "try { voila(); } catch (e) { oops(); }",
59 options: ["TryStatement", "CallExpression", "CatchClause"],
60 errors: [
61 { messageId: "restrictedSyntax", data: { message: "Using 'TryStatement' is not allowed." }, type: "TryStatement" },
62 { messageId: "restrictedSyntax", data: { message: "Using 'CallExpression' is not allowed." }, type: "CallExpression" },
63 { messageId: "restrictedSyntax", data: { message: "Using 'CatchClause' is not allowed." }, type: "CatchClause" },
64 { messageId: "restrictedSyntax", data: { message: "Using 'CallExpression' is not allowed." }, type: "CallExpression" }
65 ]
66 },
67 {
68 code: "bar;",
69 options: ["Identifier[name=\"bar\"]"],
70 errors: [{ messageId: "restrictedSyntax", data: { message: "Using 'Identifier[name=\"bar\"]' is not allowed." }, type: "Identifier" }]
71 },
72 {
73 code: "bar;",
74 options: ["Identifier", "Identifier[name=\"bar\"]"],
75 errors: [
76 { messageId: "restrictedSyntax", data: { message: "Using 'Identifier' is not allowed." }, type: "Identifier" },
77 { messageId: "restrictedSyntax", data: { message: "Using 'Identifier[name=\"bar\"]' is not allowed." }, type: "Identifier" }
78 ]
79 },
80 {
81 code: "() => {}",
82 options: ["ArrowFunctionExpression > BlockStatement"],
83 parserOptions: { ecmaVersion: 6 },
84 errors: [{ messageId: "restrictedSyntax", data: { message: "Using 'ArrowFunctionExpression > BlockStatement' is not allowed." }, type: "BlockStatement" }]
85 },
86 {
87 code: "({ foo: 1, 'bar': 2 })",
88 options: ["Property > Literal.key"],
89 errors: [{ messageId: "restrictedSyntax", data: { message: "Using 'Property > Literal.key' is not allowed." }, type: "Literal" }]
90 },
91 {
92 code: "A: for (;;) break A;",
93 options: ["BreakStatement[label]"],
94 errors: [{ messageId: "restrictedSyntax", data: { message: "Using 'BreakStatement[label]' is not allowed." }, type: "BreakStatement" }]
95 },
96 {
97 code: "function foo(bar, baz, qux) {}",
98 options: ["FunctionDeclaration[params.length>2]"],
99 errors: [{ messageId: "restrictedSyntax", data: { message: "Using 'FunctionDeclaration[params.length>2]' is not allowed." }, type: "FunctionDeclaration" }]
100 },
101
102 // object format
103 {
104 code: "var foo = 41;",
105 options: [{ selector: "VariableDeclaration" }],
106 errors: [{ messageId: "restrictedSyntax", data: { message: "Using 'VariableDeclaration' is not allowed." }, type: "VariableDeclaration" }]
107 },
108 {
109 code: "function foo(bar, baz, qux) {}",
110 options: [{ selector: "FunctionDeclaration[params.length>2]" }],
111 errors: [{ messageId: "restrictedSyntax", data: { message: "Using 'FunctionDeclaration[params.length>2]' is not allowed." }, type: "FunctionDeclaration" }]
112 },
113 {
114 code: "function foo(bar, baz, qux) {}",
115 options: [{ selector: "FunctionDeclaration[params.length>2]", message: "custom error message." }],
116 errors: [{ messageId: "restrictedSyntax", data: { message: "custom error message." }, type: "FunctionDeclaration" }]
117 },
118
119 // with object format, the custom message may contain the string '{{selector}}'
120 {
121 code: "function foo(bar, baz, qux) {}",
122 options: [{ selector: "FunctionDeclaration[params.length>2]", message: "custom message with {{selector}}" }],
123 errors: [{ messageId: "restrictedSyntax", data: { message: "custom message with {{selector}}" }, type: "FunctionDeclaration" }]
124 },
125
126 // https://github.com/eslint/eslint/issues/8733
127 {
128 code: "console.log(/a/i);",
129 options: ["Literal[regex.flags=/./]"],
130 errors: [{ messageId: "restrictedSyntax", data: { message: "Using 'Literal[regex.flags=/./]' is not allowed." }, type: "Literal" }]
131 },
132
133 // Optional chaining
134 {
135 code: "var foo = foo?.bar?.();",
136 options: ["ChainExpression"],
137 parserOptions: { ecmaVersion: 2020 },
138 errors: [{ messageId: "restrictedSyntax", data: { message: "Using 'ChainExpression' is not allowed." }, type: "ChainExpression" }]
139 },
140 {
141 code: "var foo = foo?.bar?.();",
142 options: ["[optional=true]"],
143 parserOptions: { ecmaVersion: 2020 },
144 errors: [
145 { messageId: "restrictedSyntax", data: { message: "Using '[optional=true]' is not allowed." }, type: "CallExpression" },
146 { messageId: "restrictedSyntax", data: { message: "Using '[optional=true]' is not allowed." }, type: "MemberExpression" }
147 ]
148 }
149
150 /*
151 * TODO(mysticatea): fix https://github.com/estools/esquery/issues/110
152 * {
153 * code: "a?.b",
154 * options: [":nth-child(1)"],
155 * parserOptions: { ecmaVersion: 2020 },
156 * errors: [
157 * { messageId: "restrictedSyntax", data: { message: "Using ':nth-child(1)' is not allowed." }, type: "ExpressionStatement" }
158 * ]
159 * }
160 */
161 ]
162 });