]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/func-style.js
first commit
[pve-eslint.git] / eslint / tests / lib / rules / func-style.js
1 /**
2 * @fileoverview Tests for func-style rule.
3 * @author Nicholas C. Zakas
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/func-style"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("func-style", rule, {
22 valid: [
23 {
24 code: "function foo(){}\n function bar(){}",
25 options: ["declaration"]
26 },
27 {
28 code: "foo.bar = function(){};",
29 options: ["declaration"]
30 },
31 {
32 code: "(function() { /* code */ }());",
33 options: ["declaration"]
34 },
35 {
36 code: "var module = (function() { return {}; }());",
37 options: ["declaration"]
38 },
39 {
40 code: "var object = { foo: function(){} };",
41 options: ["declaration"]
42 },
43 {
44 code: "Array.prototype.foo = function(){};",
45 options: ["declaration"]
46 },
47 {
48 code: "foo.bar = function(){};",
49 options: ["expression"]
50 },
51 {
52 code: "var foo = function(){};\n var bar = function(){};",
53 options: ["expression"]
54 },
55 {
56 code: "var foo = () => {};\n var bar = () => {}",
57 options: ["expression"],
58 parserOptions: { ecmaVersion: 6 }
59 },
60
61 // https://github.com/eslint/eslint/issues/3819
62 {
63 code: "var foo = function() { this; }.bind(this);",
64 options: ["declaration"]
65 },
66 {
67 code: "var foo = () => { this; };",
68 options: ["declaration"],
69 parserOptions: { ecmaVersion: 6 }
70 },
71 {
72 code: "export default function () {};",
73 parserOptions: { ecmaVersion: 6, sourceType: "module" }
74 },
75 {
76 code: "var foo = () => {};",
77 options: ["declaration", { allowArrowFunctions: true }],
78 parserOptions: { ecmaVersion: 6 }
79 },
80 {
81 code: "var foo = () => { function foo() { this; } };",
82 options: ["declaration", { allowArrowFunctions: true }],
83 parserOptions: { ecmaVersion: 6 }
84 }
85 ],
86
87 invalid: [
88 {
89 code: "var foo = function(){};",
90 options: ["declaration"],
91 errors: [
92 {
93 messageId: "declaration",
94 type: "VariableDeclarator"
95 }
96 ]
97 },
98 {
99 code: "var foo = () => {};",
100 options: ["declaration"],
101 parserOptions: { ecmaVersion: 6 },
102 errors: [
103 {
104 messageId: "declaration",
105 type: "VariableDeclarator"
106 }
107 ]
108 },
109 {
110 code: "var foo = () => { function foo() { this; } };",
111 options: ["declaration"],
112 parserOptions: { ecmaVersion: 6 },
113 errors: [
114 {
115 messageId: "declaration",
116 type: "VariableDeclarator"
117 }
118 ]
119 },
120 {
121 code: "function foo(){}",
122 options: ["expression"],
123 errors: [
124 {
125 messageId: "expression",
126 type: "FunctionDeclaration"
127 }
128 ]
129 }
130 ]
131 });