UNPKG

1.57 kBJavaScriptView Raw
1/**
2 * disallow the `$http` methods `success()` and `error()`
3 *
4 * Disallow the $http success and error function.
5 * Instead the standard promise API should be used.
6 *
7 * @version 0.12.0
8 * @category deprecatedAngularFeature
9 * @sinceAngularVersion 1.x
10 */
11'use strict';
12
13module.exports = {
14 meta: {
15 schema: []
16 },
17 create: function(context) {
18 var httpMethods = [
19 'delete',
20 'get',
21 'head',
22 'jsonp',
23 'patch',
24 'post',
25 'put'
26 ];
27
28 function isHttpCall(node) {
29 if (node.callee.type === 'MemberExpression') {
30 return httpMethods.indexOf(node.callee.property.name) !== -1 ||
31 (node.callee.object.type === 'CallExpression' && isHttpCall(node.callee.object));
32 }
33 if (node.callee.type === 'Identifier') {
34 return node.callee.name === '$http';
35 }
36 }
37
38 return {
39 CallExpression: function(node) {
40 if (node.callee.type !== 'MemberExpression') {
41 return;
42 }
43 if (node.callee.property.name === 'success' && isHttpCall(node)) {
44 return context.report(node, '$http success is deprecated. Use then instead');
45 }
46 if (node.callee.property.name === 'error' && isHttpCall(node)) {
47 context.report(node, '$http error is deprecated. Use then or catch instead');
48 }
49 }
50 };
51 }
52};