noOutputNativeRule.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var sprintf_js_1 = require("sprintf-js");
  17. var rules_1 = require("tslint/lib/rules");
  18. var ngWalker_1 = require("./angular/ngWalker");
  19. var utils_1 = require("./util/utils");
  20. var nativeEventNames = new Set([
  21. 'abort',
  22. 'afterprint',
  23. 'animationend',
  24. 'animationiteration',
  25. 'animationstart',
  26. 'appinstalled',
  27. 'audioprocess',
  28. 'audioend',
  29. 'audiostart',
  30. 'beforeprint',
  31. 'beforeunload',
  32. 'beginEvent',
  33. 'blocked',
  34. 'blur',
  35. 'boundary',
  36. 'cached',
  37. 'canplay',
  38. 'canplaythrough',
  39. 'change',
  40. 'chargingchange',
  41. 'chargingtimechange',
  42. 'checking',
  43. 'click',
  44. 'close',
  45. 'complete',
  46. 'compositionend',
  47. 'compositionstart',
  48. 'compositionupdate',
  49. 'contextmenu',
  50. 'copy',
  51. 'cut',
  52. 'dblclick',
  53. 'devicechange',
  54. 'devicelight',
  55. 'devicemotion',
  56. 'deviceorientation',
  57. 'deviceproximity',
  58. 'dischargingtimechange',
  59. 'DOMAttributeNameChanged',
  60. 'DOMAttrModified',
  61. 'DOMCharacterDataModified',
  62. 'DOMContentLoaded',
  63. 'DOMElementNameChanged',
  64. 'focus',
  65. 'focusin',
  66. 'focusout',
  67. 'DOMNodeInserted',
  68. 'DOMNodeInsertedIntoDocument',
  69. 'DOMNodeRemoved',
  70. 'DOMNodeRemovedFromDocument',
  71. 'DOMSubtreeModified',
  72. 'downloading',
  73. 'drag',
  74. 'dragend',
  75. 'dragenter',
  76. 'dragleave',
  77. 'dragover',
  78. 'dragstart',
  79. 'drop',
  80. 'durationchange',
  81. 'emptied',
  82. 'end',
  83. 'ended',
  84. 'endEvent',
  85. 'error',
  86. 'fullscreenchange',
  87. 'fullscreenerror',
  88. 'gamepadconnected',
  89. 'gamepaddisconnected',
  90. 'gotpointercapture',
  91. 'hashchange',
  92. 'lostpointercapture',
  93. 'input',
  94. 'invalid',
  95. 'keydown',
  96. 'keypress',
  97. 'keyup',
  98. 'languagechange',
  99. 'levelchange',
  100. 'load',
  101. 'loadeddata',
  102. 'loadedmetadata',
  103. 'loadend',
  104. 'loadstart',
  105. 'mark',
  106. 'message',
  107. 'messageerror',
  108. 'mousedown',
  109. 'mouseenter',
  110. 'mouseleave',
  111. 'mousemove',
  112. 'mouseout',
  113. 'mouseover',
  114. 'mouseup',
  115. 'nomatch',
  116. 'notificationclick',
  117. 'noupdate',
  118. 'obsolete',
  119. 'offline',
  120. 'online',
  121. 'open',
  122. 'orientationchange',
  123. 'pagehide',
  124. 'pageshow',
  125. 'paste',
  126. 'pause',
  127. 'pointercancel',
  128. 'pointerdown',
  129. 'pointerenter',
  130. 'pointerleave',
  131. 'pointerlockchange',
  132. 'pointerlockerror',
  133. 'pointermove',
  134. 'pointerout',
  135. 'pointerover',
  136. 'pointerup',
  137. 'play',
  138. 'playing',
  139. 'popstate',
  140. 'progress',
  141. 'push',
  142. 'pushsubscriptionchange',
  143. 'ratechange',
  144. 'readystatechange',
  145. 'repeatEvent',
  146. 'reset',
  147. 'resize',
  148. 'resourcetimingbufferfull',
  149. 'result',
  150. 'resume',
  151. 'scroll',
  152. 'seeked',
  153. 'seeking',
  154. 'select',
  155. 'selectstart',
  156. 'selectionchange',
  157. 'show',
  158. 'soundend',
  159. 'soundstart',
  160. 'speechend',
  161. 'speechstart',
  162. 'stalled',
  163. 'start',
  164. 'storage',
  165. 'submit',
  166. 'success',
  167. 'suspend',
  168. 'SVGAbort',
  169. 'SVGError',
  170. 'SVGLoad',
  171. 'SVGResize',
  172. 'SVGScroll',
  173. 'SVGUnload',
  174. 'SVGZoom',
  175. 'timeout',
  176. 'timeupdate',
  177. 'touchcancel',
  178. 'touchend',
  179. 'touchmove',
  180. 'touchstart',
  181. 'transitionend',
  182. 'unload',
  183. 'updateready',
  184. 'upgradeneeded',
  185. 'userproximity',
  186. 'voiceschanged',
  187. 'versionchange',
  188. 'visibilitychange',
  189. 'volumechange',
  190. 'waiting',
  191. 'wheel'
  192. ]);
  193. exports.getFailureMessage = function (failureParameters) {
  194. return sprintf_js_1.sprintf(Rule.FAILURE_STRING, failureParameters.className, failureParameters.propertyName);
  195. };
  196. var Rule = (function (_super) {
  197. __extends(Rule, _super);
  198. function Rule() {
  199. return _super !== null && _super.apply(this, arguments) || this;
  200. }
  201. Rule.prototype.apply = function (sourceFile) {
  202. var walker = new Walker(sourceFile, this.getOptions());
  203. return this.applyWithWalker(walker);
  204. };
  205. Rule.metadata = {
  206. description: 'Disallows naming directive outputs as standard DOM event.',
  207. options: null,
  208. optionsDescription: 'Not configurable.',
  209. rationale: 'Listeners subscribed to an output with such a name will also be invoked when the native event is raised.',
  210. ruleName: 'no-output-native',
  211. type: 'functionality',
  212. typescriptOnly: true
  213. };
  214. Rule.FAILURE_STRING = 'In the class "%s", the output property "%s" should not be named or renamed as a native event';
  215. return Rule;
  216. }(rules_1.AbstractRule));
  217. exports.Rule = Rule;
  218. var Walker = (function (_super) {
  219. __extends(Walker, _super);
  220. function Walker() {
  221. return _super !== null && _super.apply(this, arguments) || this;
  222. }
  223. Walker.prototype.visitNgOutput = function (property, output, args) {
  224. this.validateOutput(property, args);
  225. _super.prototype.visitNgOutput.call(this, property, output, args);
  226. };
  227. Walker.prototype.validateOutput = function (property, args) {
  228. var className = utils_1.getClassName(property);
  229. if (!className)
  230. return;
  231. var propertyName = property.name.getText();
  232. var outputName = args[0] || propertyName;
  233. if (!outputName || !nativeEventNames.has(outputName))
  234. return;
  235. var failure = exports.getFailureMessage({ className: className, propertyName: propertyName });
  236. this.addFailureAtNode(property, failure);
  237. };
  238. return Walker;
  239. }(ngWalker_1.NgWalker));