]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-restricted-syntax.js
cf8bc41236644d7c46433c5e154a87af085cc949
2 * @fileoverview Tests for `no-restricted-syntax` rule
3 * @author Burak Yigit Kaya
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const rule
= require("../../../lib/rules/no-restricted-syntax"),
12 { RuleTester
} = require("../../../lib/rule-tester");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const ruleTester
= new RuleTester();
20 ruleTester
.run("no-restricted-syntax", rule
, {
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]"] },
34 { code
: "var foo = 42;", options
: [{ selector
: "ConditionalExpression" }] },
35 { code
: "({ foo: 1, bar: 2 })", options
: [{ selector
: "Property > Literal.key" }] },
37 code
: "({ foo: 1, bar: 2 })",
38 options
: [{ selector
: "FunctionDeclaration[params.length>2]", message
: "custom error message." }]
41 // https://github.com/eslint/eslint/issues/8733
42 { code
: "console.log(/a/);", options
: ["Literal[regex.flags=/./]"] }
48 code
: "var foo = 41;",
49 options
: ["VariableDeclaration"],
50 errors
: [{ messageId
: "restrictedSyntax", data
: { message
: "Using 'VariableDeclaration' is not allowed." }, type
: "VariableDeclaration" }]
53 code
: ";function lol(a) { return 42; }",
54 options
: ["EmptyStatement"],
55 errors
: [{ messageId
: "restrictedSyntax", data
: { message
: "Using 'EmptyStatement' is not allowed." }, type
: "EmptyStatement" }]
58 code
: "try { voila(); } catch (e) { oops(); }",
59 options
: ["TryStatement", "CallExpression", "CatchClause"],
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" }
69 options
: ["Identifier[name=\"bar\"]"],
70 errors
: [{ messageId
: "restrictedSyntax", data
: { message
: "Using 'Identifier[name=\"bar\"]' is not allowed." }, type
: "Identifier" }]
74 options
: ["Identifier", "Identifier[name=\"bar\"]"],
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" }
82 options
: ["ArrowFunctionExpression > BlockStatement"],
83 parserOptions
: { ecmaVersion
: 6 },
84 errors
: [{ messageId
: "restrictedSyntax", data
: { message
: "Using 'ArrowFunctionExpression > BlockStatement' is not allowed." }, type
: "BlockStatement" }]
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" }]
92 code
: "A: for (;;) break A;",
93 options
: ["BreakStatement[label]"],
94 errors
: [{ messageId
: "restrictedSyntax", data
: { message
: "Using 'BreakStatement[label]' is not allowed." }, type
: "BreakStatement" }]
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" }]
104 code
: "var foo = 41;",
105 options
: [{ selector
: "VariableDeclaration" }],
106 errors
: [{ messageId
: "restrictedSyntax", data
: { message
: "Using 'VariableDeclaration' is not allowed." }, type
: "VariableDeclaration" }]
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" }]
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" }]
119 // with object format, the custom message may contain the string '{{selector}}'
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" }]
126 // https://github.com/eslint/eslint/issues/8733
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" }]
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" }]
141 code
: "var foo = foo?.bar?.();",
142 options
: ["[optional=true]"],
143 parserOptions
: { ecmaVersion
: 2020 },
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" }
151 * TODO(mysticatea): fix https://github.com/estools/esquery/issues/110
154 * options: [":nth-child(1)"],
155 * parserOptions: { ecmaVersion: 2020 },
157 * { messageId: "restrictedSyntax", data: { message: "Using ':nth-child(1)' is not allowed." }, type: "ExpressionStatement" }