Loads of warnings on this code
See original GitHub issueSo i have this thing about running code I want to use through linter’s and this code is rife with tons of problems… and I’m curious why you didn’t check it yourself… however, I have listed quite a few for you
line 65 col 34 'dirPaginateDirective' was used before it was defined.
line 73 col 9 Inner functions should be listed at the top of the outer function.
line 73 col 40 'dirPaginationCompileFn' was used before it was defined.
line 225 col 32 'noCompileDirective' was used before it was defined.
line 232 col 52 'dirPaginationControlsTemplateInstaller' was used before it was defined.
line 236 col 44 'dirPaginationControlsDirective' was used before it was defined.
line 253 col 9 Inner functions should be listed at the top of the outer function.
line 253 col 45 'dirPaginationControlsLinkFn' was used before it was defined.
line 293 col 31 Expected '!==' and instead saw '!='.
line 301 col 35 Expected '!==' and instead saw '!='.
line 433 col 32 'itemsPerPageFilter' was used before it was defined.
line 464 col 31 'paginationService' was used before it was defined.
line 469 col 9 Possible strict violation.
line 478 col 9 Possible strict violation.
line 482 col 9 Possible strict violation.
line 486 col 9 Possible strict violation.
line 490 col 9 Possible strict violation.
line 493 col 9 Possible strict violation.
line 498 col 9 Possible strict violation.
line 501 col 9 Possible strict violation.
line 505 col 9 Possible strict violation.
line 508 col 9 Possible strict violation.
line 512 col 9 Possible strict violation.
line 516 col 9 Possible strict violation.
line 524 col 40 'paginationTemplateProvider' was used before it was defined.
line 528 col 9 Possible strict violation.
line 532 col 9 Possible strict violation.
And through an online linter which indicates much of the code is never called at all:
(function() {
2
3 'use strict';
4
5 /**
6 * Config
7 */
8 var moduleName = 'clientApp.paginationServices';
9 var DEFAULT_ID = '__default';
10
11 /**
12 * Module
13 */
14 var module;
15 try {
16 module = angular.module(moduleName);
17 } catch(err) {
18 // named module does not exist, so create one
19 module = angular.module(moduleName, []);
20 }
21
22 module
23 .directive('dirPaginate', ['$compile', '$parse', 'paginationService', dirPaginateDirective])
========^
lint warning: unexpected end of line; it is ambiguous whether these lines are part of the same statement
24 .directive('dirPaginateNoCompile', noCompileDirective)
========^
lint warning: unexpected end of line; it is ambiguous whether these lines are part of the same statement
25 .directive('dirPaginationControls', ['paginationService', 'paginationTemplate', dirPaginationControlsDirective])
========^
lint warning: unexpected end of line; it is ambiguous whether these lines are part of the same statement
26 .filter('itemsPerPage', ['paginationService', itemsPerPageFilter])
========^
lint warning: unexpected end of line; it is ambiguous whether these lines are part of the same statement
27 .service('paginationService', paginationService)
========^
lint warning: unexpected end of line; it is ambiguous whether these lines are part of the same statement
28 .provider('paginationTemplate', paginationTemplateProvider)
========^
lint warning: unexpected end of line; it is ambiguous whether these lines are part of the same statement
29 .run(['$templateCache',dirPaginationControlsTemplateInstaller]);
========^
lint warning: unexpected end of line; it is ambiguous whether these lines are part of the same statement
30
31 function dirPaginateDirective($compile, $parse, paginationService) {
32
33 return {
34 terminal: true,
35 multiElement: true,
36 compile: dirPaginationCompileFn
37 };
38
39 function dirPaginationCompileFn(tElement, tAttrs){
========^
lint warning: unreachable code
40
41 var expression = tAttrs.dirPaginate;
42 // regex taken directly from https://github.com/angular/angular.js/blob/master/src/ng/directive/ngRepeat.js#L211
43 var match = expression.match(/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?\s*$/);
44
45 var filterPattern = /\|\s*itemsPerPage\s*:[^|]*/;
46 if (match[2].match(filterPattern) === null) {
47 throw 'pagination directive: the \'itemsPerPage\' filter must be set.';
48 }
49 var itemsPerPageFilterRemoved = match[2].replace(filterPattern, '');
50 var collectionGetter = $parse(itemsPerPageFilterRemoved);
51
52 addNoCompileAttributes(tElement);
53
54 // If any value is specified for paginationId, we register the un-evaluated expression at this stage for the benefit of any
55 // dir-pagination-controls directives that may be looking for this ID.
56 var rawId = tAttrs.paginationId || DEFAULT_ID;
57 paginationService.registerInstance(rawId);
58
59 return function dirPaginationLinkFn(scope, element, attrs){
60
61 // Now that we have access to the `scope` we can interpolate any expression given in the paginationId attribute and
62 // potentially register a new ID if it evaluates to a different value than the rawId.
63 var paginationId = $parse(attrs.paginationId)(scope) || attrs.paginationId || DEFAULT_ID;
64 paginationService.registerInstance(paginationId);
65
66 var repeatExpression = getRepeatExpression(expression, paginationId);
67 addNgRepeatToElement(element, attrs, repeatExpression);
68
69 removeTemporaryAttributes(element);
70 var compiled = $compile(element);
71
72 var currentPageGetter = makeCurrentPageGetterFn(scope, attrs, paginationId);
73 paginationService.setCurrentPageParser(paginationId, currentPageGetter, scope);
74
75 if (typeof attrs.totalItems !== 'undefined') {
76 paginationService.setAsyncModeTrue(paginationId);
77 scope.$watch(function() {
78 return $parse(attrs.totalItems)(scope);
79 }, function (result) {
80 if (0 <= result) {
81 paginationService.setCollectionLength(paginationId, result);
82 }
83 });
84 } else {
85 scope.$watchCollection(function() {
86 return collectionGetter(scope);
87 }, function(collection) {
88 if (collection) {
89 paginationService.setCollectionLength(paginationId, collection.length);
90 }
91 });
92 }
93
94 // Delegate to the link function returned by the new compilation of the ng-repeat
95 compiled(scope);
96 };
97 }
98
99 /**
100 * If a pagination id has been specified, we need to check that it is present as the second argument passed to
101 * the itemsPerPage filter. If it is not there, we add it and return the modified expression.
102 *
103 * @param expression
104 * @param paginationId
105 * @returns {*}
106 */
107 function getRepeatExpression(expression, paginationId) {
108 var repeatExpression,
109 idDefinedInFilter = !!expression.match(/(\|\s*itemsPerPage\s*:[^|]*:[^|]*)/);
110
111 if (paginationId !== DEFAULT_ID && !idDefinedInFilter) {
112 repeatExpression = expression.replace(/(\|\s*itemsPerPage\s*:[^|]*)/, "$1 : '" + paginationId + "'");
113 } else {
114 repeatExpression = expression;
115 }
116
117 return repeatExpression;
118 }
119
120 /**
121 * Adds the ng-repeat directive to the element. In the case of multi-element (-start, -end) it adds the
122 * appropriate multi-element ng-repeat to the first and last element in the range.
123 * @param element
124 * @param attrs
125 * @param repeatExpression
126 */
127 function addNgRepeatToElement(element, attrs, repeatExpression) {
128 if (element[0].hasAttribute('dir-paginate-start') || element[0].hasAttribute('data-dir-paginate-start')) {
129 // using multiElement mode (dir-paginate-start, dir-paginate-end)
130 attrs.$set('ngRepeatStart', repeatExpression);
131 element.eq(element.length - 1).attr('ng-repeat-end', true);
132 } else {
133 attrs.$set('ngRepeat', repeatExpression);
134 }
135 }
136
137 /**
138 * Adds the dir-paginate-no-compile directive to each element in the tElement range.
139 * @param tElement
140 */
141 function addNoCompileAttributes(tElement) {
142 angular.forEach(tElement, function(el) {
143 if (el.nodeType === Node.ELEMENT_NODE) {
144 angular.element(el).attr('dir-paginate-no-compile', true);
145 }
146 });
147 }
148
149 /**
150 * Removes the variations on dir-paginate (data-, -start, -end) and the dir-paginate-no-compile directives.
151 * @param element
152 */
153 function removeTemporaryAttributes(element) {
154 angular.forEach(element, function(el) {
155 if (el.nodeType === Node.ELEMENT_NODE) {
156 angular.element(el).removeAttr('dir-paginate-no-compile');
157 }
158 });
159 element.eq(0).removeAttr('dir-paginate-start').removeAttr('dir-paginate').removeAttr('data-dir-paginate-start').removeAttr('data-dir-paginate');
160 element.eq(element.length - 1).removeAttr('dir-paginate-end').removeAttr('data-dir-paginate-end');
161 }
162
163 /**
164 * Creates a getter function for the current-page attribute, using the expression provided or a default value if
165 * no current-page expression was specified.
166 *
167 * @param scope
168 * @param attrs
169 * @param paginationId
170 * @returns {*}
171 */
172 function makeCurrentPageGetterFn(scope, attrs, paginationId) {
173 var currentPageGetter;
174 if (attrs.currentPage) {
175 currentPageGetter = $parse(attrs.currentPage);
176 } else {
177 // if the current-page attribute was not set, we'll make our own
178 var defaultCurrentPage = paginationId + '__currentPage';
179 scope[defaultCurrentPage] = 1;
180 currentPageGetter = $parse(defaultCurrentPage);
181 }
182 return currentPageGetter;
183 }
184 }
====^
warning: function dirPaginateDirective does not always return a value
185
186 /**
187 * This is a helper directive that allows correct compilation when in multi-element mode (ie dir-paginate-start, dir-paginate-end).
188 * It is dynamically added to all elements in the dir-paginate compile function, and it prevents further compilation of
189 * any inner directives. It is then removed in the link function, and all inner directives are then manually compiled.
190 */
191 function noCompileDirective() {
192 return {
193 priority: 5000,
194 terminal: true
195 };
196 }
197
198 function dirPaginationControlsTemplateInstaller($templateCache) {
199 $templateCache.put('clientApp.paginationServices.template', '<ul class="pagination pagination-sm" data-ng-if="1 < pages.length"><li data-ng-if="boundaryLinks" data-ng-class="{ disabled : pagination.current == 1 }"><a href="" data-ng-click="setCurrent(1)">«</a></li><li data-ng-if="directionLinks" data-ng-class="{ disabled : pagination.current == 1 }"><a href="" data-ng-click="setCurrent(pagination.current - 1)">‹</a></li><li data-ng-repeat="pageNumber in pages track by $index" data-ng-class="{ active : pagination.current == pageNumber, disabled : pageNumber == \'...\' }"><a href="" data-ng-click="setCurrent(pageNumber)">{{ pageNumber }}</a></li><li data-ng-if="directionLinks" data-ng-class="{ disabled : pagination.current == pagination.last }"><a href="" data-ng-click="setCurrent(pagination.current + 1)">›</a></li><li data-ng-if="boundaryLinks" data-ng-class="{ disabled : pagination.current == pagination.last }"><a href="" data-ng-click="setCurrent(pagination.last)">»</a></li></ul>');
200 }
201
202 function dirPaginationControlsDirective(paginationService, paginationTemplate) {
203
204 var numberRegex = /^\d+$/;
205
206 return {
207 restrict: 'AE',
208 templateUrl: function(elem, attrs) {
209 return attrs.templateUrl || paginationTemplate.getPath();
210 },
211 scope: {
212 maxSize: '=?',
213 onPageChange: '&?',
214 paginationId: '=?'
215 },
216 link: dirPaginationControlsLinkFn
217 };
218
219 function dirPaginationControlsLinkFn(scope, element, attrs) {
========^
lint warning: unreachable code
220
221 // rawId is the un-interpolated value of the pagination-id attribute. This is only important when the corresponding dir-paginate directive has
222 // not yet been linked (e.g. if it is inside an ng-if block), and in that case it prevents this controls directive from assuming that there is
223 // no corresponding dir-paginate directive and wrongly throwing an exception.
224 var rawId = attrs.paginationId || DEFAULT_ID;
225 var paginationId = scope.paginationId || attrs.paginationId || DEFAULT_ID;
226
227 if (!paginationService.isRegistered(paginationId) && !paginationService.isRegistered(rawId)) {
228 var idMessage = (paginationId !== DEFAULT_ID) ? ' (id: ' + paginationId + ') ' : ' ';
229 throw 'pagination directive: the pagination controls' + idMessage + 'cannot be used without the corresponding pagination directive.';
230 }
231
232 if (!scope.maxSize) { scope.maxSize = 9; }
233 scope.directionLinks = angular.isDefined(attrs.directionLinks) ? scope.$parent.$eval(attrs.directionLinks) : true;
234 scope.boundaryLinks = angular.isDefined(attrs.boundaryLinks) ? scope.$parent.$eval(attrs.boundaryLinks) : false;
235
236 var paginationRange = Math.max(scope.maxSize, 5);
237 scope.pages = [];
238 scope.pagination = {
239 last: 1,
240 current: 1
241 };
242 scope.range = {
243 lower: 1,
244 upper: 1,
245 total: 1
246 };
247
248 scope.$watch(function() {
249 return (paginationService.getCollectionLength(paginationId) + 1) * paginationService.getItemsPerPage(paginationId);
250 }, function(length) {
251 if (0 < length) {
252 generatePagination();
253 }
254 });
255
256 scope.$watch(function() {
257 return (paginationService.getItemsPerPage(paginationId));
258 }, function(current, previous) {
259 if (current != previous && typeof previous !== 'undefined') {
260 goToPage(scope.pagination.current);
261 }
262 });
263
264 scope.$watch(function() {
265 return paginationService.getCurrentPage(paginationId);
266 }, function(currentPage, previousPage) {
267 if (currentPage != previousPage) {
268 goToPage(currentPage);
269 }
270 });
271
272 scope.setCurrent = function(num) {
273 if (isValidPageNumber(num)) {
274 num = parseInt(num, 10);
275 paginationService.setCurrentPage(paginationId, num);
276 }
277 };
278
279 function goToPage(num) {
280 if (isValidPageNumber(num)) {
281 scope.pages = generatePagesArray(num, paginationService.getCollectionLength(paginationId), paginationService.getItemsPerPage(paginationId), paginationRange);
282 scope.pagination.current = num;
283 updateRangeValues();
284
285 // if a callback has been set, then call it with the page number as an argument
286 if (scope.onPageChange) {
287 scope.onPageChange({ newPageNumber : num });
288 }
289 }
290 }
291
292 function generatePagination() {
293 var page = parseInt(paginationService.getCurrentPage(paginationId)) || 1;
==================================================================================^
lint warning: parseInt missing radix parameter
294
295 scope.pages = generatePagesArray(page, paginationService.getCollectionLength(paginationId), paginationService.getItemsPerPage(paginationId), paginationRange);
296 scope.pagination.current = page;
297 scope.pagination.last = scope.pages[scope.pages.length - 1];
298 if (scope.pagination.last < scope.pagination.current) {
299 scope.setCurrent(scope.pagination.last);
300 } else {
301 updateRangeValues();
302 }
303 }
304
305 /**
306 * This function updates the values (lower, upper, total) of the `scope.range` object, which can be used in the pagination
307 * template to display the current page range, e.g. "showing 21 - 40 of 144 results";
308 */
309 function updateRangeValues() {
310 var currentPage = paginationService.getCurrentPage(paginationId),
311 itemsPerPage = paginationService.getItemsPerPage(paginationId),
312 totalItems = paginationService.getCollectionLength(paginationId);
313
314 scope.range.lower = (currentPage - 1) * itemsPerPage + 1;
315 scope.range.upper = Math.min(currentPage * itemsPerPage, totalItems);
316 scope.range.total = totalItems;
317 }
318
319 function isValidPageNumber(num) {
320 return (numberRegex.test(num) && (0 < num && num <= scope.pagination.last));
321 }
322 }
323
324 /**
325 * Generate an array of page numbers (or the '...' string) which is used in an ng-repeat to generate the
326 * links used in pagination
327 *
328 * @param currentPage
329 * @param rowsPerPage
330 * @param paginationRange
331 * @param collectionLength
332 * @returns {Array}
333 */
334 function generatePagesArray(currentPage, collectionLength, rowsPerPage, paginationRange) {
335 var pages = [];
336 var totalPages = Math.ceil(collectionLength / rowsPerPage);
337 var halfWay = Math.ceil(paginationRange / 2);
338 var position;
339
340 if (currentPage <= halfWay) {
341 position = 'start';
342 } else if (totalPages - halfWay < currentPage) {
343 position = 'end';
344 } else {
345 position = 'middle';
346 }
347
348 var ellipsesNeeded = paginationRange < totalPages;
349 var i = 1;
350 while (i <= totalPages && i <= paginationRange) {
351 var pageNumber = calculatePageNumber(i, currentPage, paginationRange, totalPages);
352
353 var openingEllipsesNeeded = (i === 2 && (position === 'middle' || position === 'end'));
354 var closingEllipsesNeeded = (i === paginationRange - 1 && (position === 'middle' || position === 'start'));
355 if (ellipsesNeeded && (openingEllipsesNeeded || closingEllipsesNeeded)) {
356 pages.push('...');
357 } else {
358 pages.push(pageNumber);
359 }
360 i ++;
361 }
362 return pages;
363 }
364
365 /**
366 * Given the position in the sequence of pagination links [i], figure out what page number corresponds to that position.
367 *
368 * @param i
369 * @param currentPage
370 * @param paginationRange
371 * @param totalPages
372 * @returns {*}
373 */
374 function calculatePageNumber(i, currentPage, paginationRange, totalPages) {
375 var halfWay = Math.ceil(paginationRange/2);
376 if (i === paginationRange) {
377 return totalPages;
378 } else if (i === 1) {
379 return i;
380 } else if (paginationRange < totalPages) {
381 if (totalPages - halfWay < currentPage) {
382 return totalPages - paginationRange + i;
383 } else if (halfWay < currentPage) {
384 return currentPage - halfWay + i;
385 } else {
386 return i;
387 }
388 } else {
389 return i;
390 }
391 }
392 }
====^
warning: function dirPaginationControlsDirective does not always return a value
393
394 /**
395 * This filter slices the collection into pages based on the current page number and number of items per page.
396 * @param paginationService
397 * @returns {Function}
398 */
399 function itemsPerPageFilter(paginationService) {
400
401 return function(collection, itemsPerPage, paginationId) {
402 if (typeof (paginationId) === 'undefined') {
403 paginationId = DEFAULT_ID;
404 }
405 if (!paginationService.isRegistered(paginationId)) {
406 throw 'pagination directive: the itemsPerPage id argument (id: ' + paginationId + ') does not match a registered pagination-id.';
407 }
408 var end;
409 var start;
410 if (collection instanceof Array) {
411 itemsPerPage = parseInt(itemsPerPage) || 9999999999;
====================================================^
lint warning: parseInt missing radix parameter
412 if (paginationService.isAsyncMode(paginationId)) {
413 start = 0;
414 } else {
415 start = (paginationService.getCurrentPage(paginationId) - 1) * itemsPerPage;
416 }
417 end = start + itemsPerPage;
418 paginationService.setItemsPerPage(paginationId, itemsPerPage);
419
420 return collection.slice(start, end);
421 } else {
422 return collection;
423 }
424 };
425 }
426
427 /**
428 * This service allows the various parts of the module to communicate and stay in sync.
429 */
430 function paginationService() {
431
432 var instances = {};
433 var lastRegisteredInstance;
434
435 this.registerInstance = function(instanceId) {
436 if (typeof instances[instanceId] === 'undefined') {
437 instances[instanceId] = {
438 asyncMode: false
439 };
440 lastRegisteredInstance = instanceId;
441 }
442 };
443
444 this.isRegistered = function(instanceId) {
445 return (typeof instances[instanceId] !== 'undefined');
446 };
447
448 this.getLastInstanceId = function() {
449 return lastRegisteredInstance;
450 };
451
452 this.setCurrentPageParser = function(instanceId, val, scope) {
453 instances[instanceId].currentPageParser = val;
454 instances[instanceId].context = scope;
455 };
456 this.setCurrentPage = function(instanceId, val) {
457 instances[instanceId].currentPageParser.assign(instances[instanceId].context, val);
458 };
459 this.getCurrentPage = function(instanceId) {
460 var parser = instances[instanceId].currentPageParser;
461 return parser ? parser(instances[instanceId].context) : 1;
462 };
463
464 this.setItemsPerPage = function(instanceId, val) {
465 instances[instanceId].itemsPerPage = val;
466 };
467 this.getItemsPerPage = function(instanceId) {
468 return instances[instanceId].itemsPerPage;
469 };
470
471 this.setCollectionLength = function(instanceId, val) {
472 instances[instanceId].collectionLength = val;
473 };
474 this.getCollectionLength = function(instanceId) {
475 return instances[instanceId].collectionLength;
476 };
477
478 this.setAsyncModeTrue = function(instanceId) {
479 instances[instanceId].asyncMode = true;
480 };
481
482 this.isAsyncMode = function(instanceId) {
483 return instances[instanceId].asyncMode;
484 };
485 }
486
487 /**
488 * This provider allows global configuration of the template path used by the dir-pagination-controls directive.
489 */
490 function paginationTemplateProvider() {
491
492 var templatePath = 'clientApp.paginationServices.template';
493
494 this.setPath = function(path) {
495 templatePath = path;
496 };
497
498 this.$get = function() {
499 return {
500 getPath: function() {
501 return templatePath;
502 }
503 };
504 };
505 }
506 })();
Issue Analytics
- State:
- Created 8 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Pylint shows a lot of warnings (and only warnings) #634 - GitHub
When I run Pylint on my project, everything is shown as a warning, while some problems are obviously errors (e.g. missing parameter) and ......
Read more >Event viewer has lots of warnings and errors
Hello dear, I was looking at my event viewer because one of my friend's laptop got hacked due to some malware software.
Read more >A lot of warnings from the code, please help - WordPress.org
It works but when I turn on debug, there are a lot of warnings. Code function custom_get_view_post( $attrs, $content ) { if(rehub_option('post_view_disable') !=...
Read more >Manage warnings about unsafe sites - Google Chrome Help
On the page where you see a warning, tap Details. Tap Visit this unsafe site. The page will load. Turn off warnings about...
Read more >Fix All Warnings And Errors: what is the history of this code ...
Eliminate the causes of all compiler errors and warnings. Pay attention to what the compiler tells you about your code. A lot of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m sorry, but I genuinely don’t understand most of what you wrote above. Perhaps that is a result of your 25+ years experience vs. my few. If you are so inclined and are able to reproduce a specific behaviour in a plunker demo, that would make it clear to me and also should be an actionable item for my growling list of open issues 😉
The reason the open issues list is longer than I’d like is a combination of a) lack of free time to spend triaging them b) waiting for information from the originator and b) some are genuinely very difficult and would require an inordinate amount of time to resolve. But also note that there are 219 closed vs 34 open at this writing.
If you choose not to open another specific issue regarding the faulty behaviour you outlined above, then I bid you farewell and good luck with your rewrite. 👍
Basically this needs to be rewritten from scratch. I’m going to rewrite it. it is disturbing to me that people write code - don’t test it, clean it and make sure it is 100% error free - before releasing it to the public.