| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = void 0;
|
|---|
| 7 |
|
|---|
| 8 | var _experimentalUtils = require("@typescript-eslint/experimental-utils");
|
|---|
| 9 |
|
|---|
| 10 | var _utils = require("./utils");
|
|---|
| 11 |
|
|---|
| 12 | var _default = (0, _utils.createRule)({
|
|---|
| 13 | name: __filename,
|
|---|
| 14 | meta: {
|
|---|
| 15 | docs: {
|
|---|
| 16 | category: 'Best Practices',
|
|---|
| 17 | description: 'Disallow Jasmine globals',
|
|---|
| 18 | recommended: 'error'
|
|---|
| 19 | },
|
|---|
| 20 | messages: {
|
|---|
| 21 | illegalGlobal: 'Illegal usage of global `{{ global }}`, prefer `{{ replacement }}`',
|
|---|
| 22 | illegalMethod: 'Illegal usage of `{{ method }}`, prefer `{{ replacement }}`',
|
|---|
| 23 | illegalFail: 'Illegal usage of `fail`, prefer throwing an error, or the `done.fail` callback',
|
|---|
| 24 | illegalPending: 'Illegal usage of `pending`, prefer explicitly skipping a test using `test.skip`',
|
|---|
| 25 | illegalJasmine: 'Illegal usage of jasmine global'
|
|---|
| 26 | },
|
|---|
| 27 | fixable: 'code',
|
|---|
| 28 | schema: [],
|
|---|
| 29 | type: 'suggestion'
|
|---|
| 30 | },
|
|---|
| 31 | defaultOptions: [],
|
|---|
| 32 |
|
|---|
| 33 | create(context) {
|
|---|
| 34 | return {
|
|---|
| 35 | CallExpression(node) {
|
|---|
| 36 | const {
|
|---|
| 37 | callee
|
|---|
| 38 | } = node;
|
|---|
| 39 | const calleeName = (0, _utils.getNodeName)(callee);
|
|---|
| 40 |
|
|---|
| 41 | if (!calleeName) {
|
|---|
| 42 | return;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | if (calleeName === 'spyOn' || calleeName === 'spyOnProperty' || calleeName === 'fail' || calleeName === 'pending') {
|
|---|
| 46 | if ((0, _utils.scopeHasLocalReference)(context.getScope(), calleeName)) {
|
|---|
| 47 | // It's a local variable, not a jasmine global.
|
|---|
| 48 | return;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | switch (calleeName) {
|
|---|
| 52 | case 'spyOn':
|
|---|
| 53 | case 'spyOnProperty':
|
|---|
| 54 | context.report({
|
|---|
| 55 | node,
|
|---|
| 56 | messageId: 'illegalGlobal',
|
|---|
| 57 | data: {
|
|---|
| 58 | global: calleeName,
|
|---|
| 59 | replacement: 'jest.spyOn'
|
|---|
| 60 | }
|
|---|
| 61 | });
|
|---|
| 62 | break;
|
|---|
| 63 |
|
|---|
| 64 | case 'fail':
|
|---|
| 65 | context.report({
|
|---|
| 66 | node,
|
|---|
| 67 | messageId: 'illegalFail'
|
|---|
| 68 | });
|
|---|
| 69 | break;
|
|---|
| 70 |
|
|---|
| 71 | case 'pending':
|
|---|
| 72 | context.report({
|
|---|
| 73 | node,
|
|---|
| 74 | messageId: 'illegalPending'
|
|---|
| 75 | });
|
|---|
| 76 | break;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | return;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | if (callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && calleeName.startsWith('jasmine.')) {
|
|---|
| 83 | const functionName = calleeName.replace('jasmine.', '');
|
|---|
| 84 |
|
|---|
| 85 | if (functionName === 'any' || functionName === 'anything' || functionName === 'arrayContaining' || functionName === 'objectContaining' || functionName === 'stringMatching') {
|
|---|
| 86 | context.report({
|
|---|
| 87 | fix: fixer => [fixer.replaceText(callee.object, 'expect')],
|
|---|
| 88 | node,
|
|---|
| 89 | messageId: 'illegalMethod',
|
|---|
| 90 | data: {
|
|---|
| 91 | method: calleeName,
|
|---|
| 92 | replacement: `expect.${functionName}`
|
|---|
| 93 | }
|
|---|
| 94 | });
|
|---|
| 95 | return;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | if (functionName === 'addMatchers') {
|
|---|
| 99 | context.report({
|
|---|
| 100 | node,
|
|---|
| 101 | messageId: 'illegalMethod',
|
|---|
| 102 | data: {
|
|---|
| 103 | method: calleeName,
|
|---|
| 104 | replacement: 'expect.extend'
|
|---|
| 105 | }
|
|---|
| 106 | });
|
|---|
| 107 | return;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | if (functionName === 'createSpy') {
|
|---|
| 111 | context.report({
|
|---|
| 112 | node,
|
|---|
| 113 | messageId: 'illegalMethod',
|
|---|
| 114 | data: {
|
|---|
| 115 | method: calleeName,
|
|---|
| 116 | replacement: 'jest.fn'
|
|---|
| 117 | }
|
|---|
| 118 | });
|
|---|
| 119 | return;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | context.report({
|
|---|
| 123 | node,
|
|---|
| 124 | messageId: 'illegalJasmine'
|
|---|
| 125 | });
|
|---|
| 126 | }
|
|---|
| 127 | },
|
|---|
| 128 |
|
|---|
| 129 | MemberExpression(node) {
|
|---|
| 130 | if ((0, _utils.isSupportedAccessor)(node.object, 'jasmine')) {
|
|---|
| 131 | const {
|
|---|
| 132 | parent,
|
|---|
| 133 | property
|
|---|
| 134 | } = node;
|
|---|
| 135 |
|
|---|
| 136 | if (parent && parent.type === _experimentalUtils.AST_NODE_TYPES.AssignmentExpression) {
|
|---|
| 137 | if ((0, _utils.isSupportedAccessor)(property, 'DEFAULT_TIMEOUT_INTERVAL')) {
|
|---|
| 138 | const {
|
|---|
| 139 | right
|
|---|
| 140 | } = parent;
|
|---|
| 141 |
|
|---|
| 142 | if (right.type === _experimentalUtils.AST_NODE_TYPES.Literal) {
|
|---|
| 143 | context.report({
|
|---|
| 144 | fix: fixer => [fixer.replaceText(parent, `jest.setTimeout(${right.value})`)],
|
|---|
| 145 | node,
|
|---|
| 146 | messageId: 'illegalJasmine'
|
|---|
| 147 | });
|
|---|
| 148 | return;
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | context.report({
|
|---|
| 153 | node,
|
|---|
| 154 | messageId: 'illegalJasmine'
|
|---|
| 155 | });
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | };
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | });
|
|---|
| 164 |
|
|---|
| 165 | exports.default = _default; |
|---|