catapult-camelcase.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. /* eslint-disable */
  5. /**
  6. * @fileoverview Tests for camelcase rule.
  7. * @author Nicholas C. Zakas
  8. */
  9. 'use strict';
  10. //------------------------------------------------------------------------------
  11. // Requirements
  12. //------------------------------------------------------------------------------
  13. var rule = require("../rules/catapult-camelcase"),
  14. RuleTester = require("../../node_runner/node_runner/node_modules/eslint/lib/testers/rule-tester");
  15. //------------------------------------------------------------------------------
  16. // Tests
  17. //------------------------------------------------------------------------------
  18. var ruleTester = new RuleTester();
  19. ruleTester.run("camelcase", rule, {
  20. valid: [
  21. "firstName = \"Nicholas\"",
  22. "FIRST_NAME = \"Nicholas\"",
  23. "__myPrivateVariable = \"Patrick\"",
  24. "myPrivateVariable_ = \"Patrick\"",
  25. "function doSomething(){}",
  26. "do_something()",
  27. "foo.do_something()",
  28. "var foo = bar.baz_boom;",
  29. "var foo = bar.baz_boom.something;",
  30. "foo.boom_pow.qux = bar.baz_boom.something;",
  31. "if (bar.baz_boom) {}",
  32. "var obj = { key: foo.bar_baz };",
  33. "var arr = [foo.bar_baz];",
  34. "[foo.bar_baz]",
  35. "var arr = [foo.bar_baz.qux];",
  36. "[foo.bar_baz.nesting]",
  37. "if (foo.bar_baz === boom.bam_pow) { [foo.baz_boom] }",
  38. // These tests are for Catapult-specific exceptions.
  39. "opt_firstName = \"Nicholas\"",
  40. "g_firstName = \"Nicholas\"",
  41. "sizeInBytes_smallerIsBetter = \"Nicholas\"",
  42. "sizeInBytes_biggerIsBetter = \"Nicholas\"",
  43. {
  44. code: "var o = {key: 1}",
  45. options: [{properties: "always"}]
  46. },
  47. {
  48. code: "var o = {bar_baz: 1}",
  49. options: [{properties: "never"}]
  50. },
  51. {
  52. code: "obj.a_b = 2;",
  53. options: [{properties: "never"}]
  54. },
  55. {
  56. code: "var obj = {\n a_a: 1 \n};\n obj.a_b = 2;",
  57. options: [{properties: "never"}]
  58. },
  59. {
  60. code: "obj.foo_bar = function(){};",
  61. options: [{properties: "never"}]
  62. },
  63. {
  64. code: "var { category_id: category } = query;",
  65. parserOptions: { ecmaVersion: 6 }
  66. },
  67. {
  68. code: "var { category_id: category } = query;",
  69. parserOptions: { ecmaVersion: 6 },
  70. options: [{properties: "never"}]
  71. },
  72. {
  73. code: "import { camelCased } from \"external module\";",
  74. parserOptions: { ecmaVersion: 6, sourceType: "module" }
  75. },
  76. {
  77. code: "import { no_camelcased as camelCased } from \"external-module\";",
  78. parserOptions: { ecmaVersion: 6, sourceType: "module" }
  79. },
  80. {
  81. code: "import { no_camelcased as camelCased, anoterCamelCased } from \"external-module\";",
  82. parserOptions: { ecmaVersion: 6, sourceType: "module" }
  83. }
  84. ],
  85. invalid: [
  86. {
  87. code: "first_name = \"Nicholas\"",
  88. errors: [
  89. {
  90. message: "Identifier 'first_name' is not in camel case.",
  91. type: "Identifier"
  92. }
  93. ]
  94. },
  95. {
  96. code: "__private_first_name = \"Patrick\"",
  97. errors: [
  98. {
  99. message: "Identifier '__private_first_name' is not in camel case.",
  100. type: "Identifier"
  101. }
  102. ]
  103. },
  104. {
  105. code: "function foo_bar(){}",
  106. errors: [
  107. {
  108. message: "Identifier 'foo_bar' is not in camel case.",
  109. type: "Identifier"
  110. }
  111. ]
  112. },
  113. {
  114. code: "obj.foo_bar = function(){};",
  115. errors: [
  116. {
  117. message: "Identifier 'foo_bar' is not in camel case.",
  118. type: "Identifier"
  119. }
  120. ]
  121. },
  122. {
  123. code: "bar_baz.foo = function(){};",
  124. errors: [
  125. {
  126. message: "Identifier 'bar_baz' is not in camel case.",
  127. type: "Identifier"
  128. }
  129. ]
  130. },
  131. {
  132. code: "[foo_bar.baz]",
  133. errors: [
  134. {
  135. message: "Identifier 'foo_bar' is not in camel case.",
  136. type: "Identifier"
  137. }
  138. ]
  139. },
  140. {
  141. code: "if (foo.bar_baz === boom.bam_pow) { [foo_bar.baz] }",
  142. errors: [
  143. {
  144. message: "Identifier 'foo_bar' is not in camel case.",
  145. type: "Identifier"
  146. }
  147. ]
  148. },
  149. {
  150. code: "foo.bar_baz = boom.bam_pow",
  151. errors: [
  152. {
  153. message: "Identifier 'bar_baz' is not in camel case.",
  154. type: "Identifier"
  155. }
  156. ]
  157. },
  158. {
  159. code: "var foo = { bar_baz: boom.bam_pow }",
  160. errors: [
  161. {
  162. message: "Identifier 'bar_baz' is not in camel case.",
  163. type: "Identifier"
  164. }
  165. ]
  166. },
  167. {
  168. code: "foo.qux.boom_pow = { bar: boom.bam_pow }",
  169. errors: [
  170. {
  171. message: "Identifier 'boom_pow' is not in camel case.",
  172. type: "Identifier"
  173. }
  174. ]
  175. },
  176. {
  177. code: "var o = {bar_baz: 1}",
  178. options: [{properties: "always"}],
  179. errors: [
  180. {
  181. message: "Identifier 'bar_baz' is not in camel case.",
  182. type: "Identifier"
  183. }
  184. ]
  185. },
  186. {
  187. code: "obj.a_b = 2;",
  188. options: [{properties: "always"}],
  189. errors: [
  190. {
  191. message: "Identifier 'a_b' is not in camel case.",
  192. type: "Identifier"
  193. }
  194. ]
  195. },
  196. {
  197. code: "obj.a_b = 2;",
  198. options: [{properties: "always"}],
  199. errors: [
  200. {
  201. message: "Identifier 'a_b' is not in camel case.",
  202. type: "Identifier"
  203. }
  204. ]
  205. },
  206. {
  207. code: "var { category_id: category_id } = query;",
  208. parserOptions: { ecmaVersion: 6 },
  209. errors: [
  210. {
  211. message: "Identifier 'category_id' is not in camel case.",
  212. type: "Identifier"
  213. }
  214. ]
  215. },
  216. {
  217. code: "var { category_id } = query;",
  218. parserOptions: { ecmaVersion: 6 },
  219. errors: [
  220. {
  221. message: "Identifier 'category_id' is not in camel case.",
  222. type: "Identifier"
  223. }
  224. ]
  225. },
  226. {
  227. code: "import no_camelcased from \"external-module\";",
  228. parserOptions: { ecmaVersion: 6, sourceType: "module" },
  229. errors: [
  230. {
  231. message: "Identifier 'no_camelcased' is not in camel case.",
  232. type: "Identifier"
  233. }
  234. ]
  235. },
  236. {
  237. code: "import * as no_camelcased from \"external-module\";",
  238. parserOptions: { ecmaVersion: 6, sourceType: "module" },
  239. errors: [
  240. {
  241. message: "Identifier 'no_camelcased' is not in camel case.",
  242. type: "Identifier"
  243. }
  244. ]
  245. },
  246. {
  247. code: "import { no_camelcased } from \"external-module\";",
  248. parserOptions: { ecmaVersion: 6, sourceType: "module" },
  249. errors: [
  250. {
  251. message: "Identifier 'no_camelcased' is not in camel case.",
  252. type: "Identifier"
  253. }
  254. ]
  255. },
  256. {
  257. code: "import { no_camelcased as no_camel_cased } from \"external module\";",
  258. parserOptions: { ecmaVersion: 6, sourceType: "module" },
  259. errors: [
  260. {
  261. message: "Identifier 'no_camel_cased' is not in camel case.",
  262. type: "Identifier"
  263. }
  264. ]
  265. },
  266. {
  267. code: "import { camelCased as no_camel_cased } from \"external module\";",
  268. parserOptions: { ecmaVersion: 6, sourceType: "module" },
  269. errors: [
  270. {
  271. message: "Identifier 'no_camel_cased' is not in camel case.",
  272. type: "Identifier"
  273. }
  274. ]
  275. },
  276. {
  277. code: "import { camelCased, no_camelcased } from \"external-module\";",
  278. parserOptions: { ecmaVersion: 6, sourceType: "module" },
  279. errors: [
  280. {
  281. message: "Identifier 'no_camelcased' is not in camel case.",
  282. type: "Identifier"
  283. }
  284. ]
  285. },
  286. {
  287. code: "import { no_camelcased as camelCased, another_no_camelcased } from \"external-module\";",
  288. parserOptions: { ecmaVersion: 6, sourceType: "module" },
  289. errors: [
  290. {
  291. message: "Identifier 'another_no_camelcased' is not in camel case.",
  292. type: "Identifier"
  293. }
  294. ]
  295. },
  296. {
  297. code: "import camelCased, { no_camelcased } from \"external-module\";",
  298. parserOptions: { ecmaVersion: 6, sourceType: "module" },
  299. errors: [
  300. {
  301. message: "Identifier 'no_camelcased' is not in camel case.",
  302. type: "Identifier"
  303. }
  304. ]
  305. },
  306. {
  307. code: "import no_camelcased, { another_no_camelcased as camelCased } from \"external-module\";",
  308. parserOptions: { ecmaVersion: 6, sourceType: "module" },
  309. errors: [
  310. {
  311. message: "Identifier 'no_camelcased' is not in camel case.",
  312. type: "Identifier"
  313. }
  314. ]
  315. }
  316. ]
  317. });