]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-unsafe-finally.js
first commit
[pve-eslint.git] / eslint / tests / lib / rules / no-unsafe-finally.js
1 /**
2 * @fileoverview Tests for no-unsafe-finally
3 * @author Onur Temizkan
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-unsafe-finally"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("no-unsafe-finally", rule, {
22 valid: [
23 "var foo = function() {\n try { \n return 1; \n } catch(err) { \n return 2; \n } finally { \n console.log('hola!') \n } \n }",
24 "var foo = function() { try { return 1 } catch(err) { return 2 } finally { console.log('hola!') } }",
25 "var foo = function() { try { return 1 } catch(err) { return 2 } finally { function a(x) { return x } } }",
26 "var foo = function() { try { return 1 } catch(err) { return 2 } finally { var a = function(x) { if(!x) { throw new Error() } } } }",
27 "var foo = function() { try { return 1 } catch(err) { return 2 } finally { var a = function(x) { while(true) { if(x) { break } else { continue } } } } }",
28 "var foo = function() { try { return 1 } catch(err) { return 2 } finally { var a = function(x) { label: while(true) { if(x) { break label; } else { continue } } } } }",
29 "var foo = function() { try {} finally { while (true) break; } }",
30 "var foo = function() { try {} finally { while (true) continue; } }",
31 "var foo = function() { try {} finally { switch (true) { case true: break; } } }",
32 "var foo = function() { try {} finally { do { break; } while (true) } }",
33 {
34 code: "var foo = function() { try { return 1; } catch(err) { return 2; } finally { var bar = () => { throw new Error(); }; } };",
35 parserOptions: { ecmaVersion: 6 }
36 },
37 {
38 code: "var foo = function() { try { return 1; } catch(err) { return 2 } finally { (x) => x } }",
39 parserOptions: { ecmaVersion: 6 }
40 },
41 {
42 code: "var foo = function() { try { return 1; } finally { class bar { constructor() {} static ehm() { return 'Hola!'; } } } };",
43 parserOptions: { ecmaVersion: 6 }
44 }
45 ],
46 invalid: [
47 {
48 code: "var foo = function() { \n try { \n return 1; \n } catch(err) { \n return 2; \n } finally { \n return 3; \n } \n }",
49 errors: [{ messageId: "unsafeUsage", data: { nodeType: "ReturnStatement" }, type: "ReturnStatement", line: 7, column: 2 }]
50 },
51 {
52 code: "var foo = function() { try { return 1 } catch(err) { return 2 } finally { if(true) { return 3 } else { return 2 } } }",
53 errors: [
54 { messageId: "unsafeUsage", data: { nodeType: "ReturnStatement" }, type: "ReturnStatement", line: 1, column: 86 },
55 { messageId: "unsafeUsage", data: { nodeType: "ReturnStatement" }, type: "ReturnStatement", line: 1, column: 104 }
56 ]
57 },
58 {
59 code: "var foo = function() { try { return 1 } catch(err) { return 2 } finally { return 3 } }",
60 errors: [{ messageId: "unsafeUsage", data: { nodeType: "ReturnStatement" }, type: "ReturnStatement", line: 1, column: 75 }]
61 },
62 {
63 code: "var foo = function() { try { return 1 } catch(err) { return 2 } finally { return function(x) { return y } } }",
64 errors: [{ messageId: "unsafeUsage", data: { nodeType: "ReturnStatement" }, type: "ReturnStatement", line: 1, column: 75 }]
65 },
66 {
67 code: "var foo = function() { try { return 1 } catch(err) { return 2 } finally { return { x: function(c) { return c } } } }",
68 errors: [{ messageId: "unsafeUsage", data: { nodeType: "ReturnStatement" }, type: "ReturnStatement", line: 1, column: 75 }]
69 },
70 {
71 code: "var foo = function() { try { return 1 } catch(err) { return 2 } finally { throw new Error() } }",
72 errors: [{ messageId: "unsafeUsage", data: { nodeType: "ThrowStatement" }, type: "ThrowStatement", line: 1, column: 75 }]
73 },
74 {
75 code: "var foo = function() { try { foo(); } finally { try { bar(); } finally { return; } } };",
76 errors: [{ messageId: "unsafeUsage", data: { nodeType: "ReturnStatement" }, type: "ReturnStatement", line: 1, column: 74 }]
77 },
78 {
79 code: "var foo = function() { label: try { return 0; } finally { break label; } return 1; }",
80 errors: [{ messageId: "unsafeUsage", data: { nodeType: "BreakStatement" }, type: "BreakStatement", line: 1, column: 59 }]
81 },
82 {
83 code: "var foo = function() { \n a: try { \n return 1; \n } catch(err) { \n return 2; \n } finally { \n break a; \n } \n }",
84 errors: [{ messageId: "unsafeUsage", data: { nodeType: "BreakStatement" }, type: "BreakStatement", line: 7, column: 2 }]
85 },
86 {
87 code: "var foo = function() { while (true) try {} finally { break; } }",
88 errors: [{ messageId: "unsafeUsage", data: { nodeType: "BreakStatement" }, type: "BreakStatement", line: 1, column: 54 }]
89 },
90 {
91 code: "var foo = function() { while (true) try {} finally { continue; } }",
92 errors: [{ messageId: "unsafeUsage", data: { nodeType: "ContinueStatement" }, type: "ContinueStatement", line: 1, column: 54 }]
93 },
94 {
95 code: "var foo = function() { switch (true) { case true: try {} finally { break; } } }",
96 errors: [{ messageId: "unsafeUsage", data: { nodeType: "BreakStatement" }, type: "BreakStatement", line: 1, column: 68 }]
97 },
98 {
99 code: "var foo = function() { a: while (true) try {} finally { switch (true) { case true: break a; } } }",
100 errors: [{ messageId: "unsafeUsage", data: { nodeType: "BreakStatement" }, type: "BreakStatement", line: 1, column: 84 }]
101 },
102 {
103 code: "var foo = function() { a: while (true) try {} finally { switch (true) { case true: continue; } } }",
104 errors: [{ messageId: "unsafeUsage", data: { nodeType: "ContinueStatement" }, type: "ContinueStatement", line: 1, column: 84 }]
105 },
106 {
107 code: "var foo = function() { a: switch (true) { case true: try {} finally { switch (true) { case true: break a; } } } }",
108 errors: [{ messageId: "unsafeUsage", data: { nodeType: "BreakStatement" }, type: "BreakStatement", line: 1, column: 98 }]
109 }
110 ]
111 });