UNPKG

1.88 kBJavaScriptView Raw
1const {docsUrl} = require('../utilities');
2
3function createSinonMatcher(aliases, injected) {
4 return function(object) {
5 return (
6 (injected && object.type === 'ThisExpression') ||
7 (object.type === 'Identifier' && aliases.indexOf(object.name) >= 0)
8 );
9 };
10}
11
12function createPropertyMatcher(properties) {
13 return function(property) {
14 return (
15 property.type === 'Identifier' && properties.indexOf(property.name) >= 0
16 );
17 };
18}
19
20module.exports = {
21 meta: {
22 docs: {
23 description: 'Restrict the use of specified sinon features.',
24 category: 'Best Practices',
25 recommended: false,
26 uri: docsUrl('sinon-no-restricted-features'),
27 },
28 schema: [
29 {
30 type: 'object',
31 properties: {
32 restricted: {
33 type: 'array',
34 items: {
35 type: 'string',
36 },
37 uniqueItems: true,
38 },
39
40 aliases: {
41 type: 'array',
42 items: {
43 type: 'string',
44 },
45 uniqueItems: true,
46 },
47
48 injected: {
49 type: 'boolean',
50 },
51 },
52 additionalProperties: false,
53 },
54 ],
55 },
56
57 create(context) {
58 const options = context.options[0] || {};
59 const restricted = options.restricted || [];
60 const aliases = options.aliases || ['sinon'];
61 const injected = options.injected || false;
62
63 const sinonMatcher = createSinonMatcher(aliases, injected);
64 const propertyMatcher = createPropertyMatcher(restricted);
65
66 function checkMemberExpression(node) {
67 if (sinonMatcher(node.object) && propertyMatcher(node.property)) {
68 context.report({
69 node,
70 message: `Unexpected use of sinon.${node.property.name}.`,
71 });
72 }
73 }
74
75 return {
76 MemberExpression: checkMemberExpression,
77 };
78 },
79};