/
prop-shorthand.js
53 lines (43 loc) · 1.71 KB
/
prop-shorthand.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* @fileoverview Rule to check if the property shorthand can be used
*/
'use strict';
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = function (context) {
var _ = require('lodash');
var aliasMap = require('../util/aliases');
var lodashUtil = require('../util/lodashUtil');
var astUtil = require('../util/astUtil');
var lodashPragma = require('../util/settingsUtil').getLodashPragma(context);
function canUseShorthand(func) {
return astUtil.isMemberExpOf(astUtil.getValueReturnedInFirstLine(func), astUtil.getFirstParamName(func), Number.MAX_VALUE, false);
}
function methodSupportsShorthand(node) {
return _.includes(aliasMap.supportsProp, astUtil.getMethodName(node));
}
function usesPropShorthand(node, iteratee) {
return iteratee && iteratee.type === 'Literal' && !node.arguments[node.arguments.indexOf(iteratee) + 1];
}
var callExpressionReporters = {
always: function (node, iteratee) {
if (methodSupportsShorthand(node) && canUseShorthand(iteratee)) {
context.report(iteratee, 'Prefer property shorthand syntax');
}
},
never: function (node, iteratee) {
if (usesPropShorthand(node, iteratee)) {
context.report(iteratee, 'Do not use property shorthand syntax');
}
}
};
return {
CallExpression: lodashUtil.getLodashMethodVisitor(lodashPragma, callExpressionReporters[context.options[0] || 'always'])
};
};
module.exports.schema = [
{
enum: ['always', 'never']
}
];