webhook-action/node_modules/eslint-plugin-jest/lib/rules/no-jasmine-globals.js
2022-11-10 20:43:16 +10:00

143 lines
4.4 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _utils = require("@typescript-eslint/utils");
var _utils2 = require("./utils");
var _default = (0, _utils2.createRule)({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Disallow Jasmine globals',
recommended: 'error'
},
messages: {
illegalGlobal: 'Illegal usage of global `{{ global }}`, prefer `{{ replacement }}`',
illegalMethod: 'Illegal usage of `{{ method }}`, prefer `{{ replacement }}`',
illegalFail: 'Illegal usage of `fail`, prefer throwing an error, or the `done.fail` callback',
illegalPending: 'Illegal usage of `pending`, prefer explicitly skipping a test using `test.skip`',
illegalJasmine: 'Illegal usage of jasmine global'
},
fixable: 'code',
schema: [],
type: 'suggestion'
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
const {
callee
} = node;
const calleeName = (0, _utils2.getNodeName)(callee);
if (!calleeName) {
return;
}
if (calleeName === 'spyOn' || calleeName === 'spyOnProperty' || calleeName === 'fail' || calleeName === 'pending') {
if ((0, _utils2.scopeHasLocalReference)(context.getScope(), calleeName)) {
// It's a local variable, not a jasmine global.
return;
}
switch (calleeName) {
case 'spyOn':
case 'spyOnProperty':
context.report({
node,
messageId: 'illegalGlobal',
data: {
global: calleeName,
replacement: 'jest.spyOn'
}
});
break;
case 'fail':
context.report({
node,
messageId: 'illegalFail'
});
break;
case 'pending':
context.report({
node,
messageId: 'illegalPending'
});
break;
}
return;
}
if (callee.type === _utils.AST_NODE_TYPES.MemberExpression && calleeName.startsWith('jasmine.')) {
const functionName = calleeName.replace('jasmine.', '');
if (functionName === 'any' || functionName === 'anything' || functionName === 'arrayContaining' || functionName === 'objectContaining' || functionName === 'stringMatching') {
context.report({
fix: fixer => [fixer.replaceText(callee.object, 'expect')],
node,
messageId: 'illegalMethod',
data: {
method: calleeName,
replacement: `expect.${functionName}`
}
});
return;
}
if (functionName === 'addMatchers') {
context.report({
node,
messageId: 'illegalMethod',
data: {
method: calleeName,
replacement: 'expect.extend'
}
});
return;
}
if (functionName === 'createSpy') {
context.report({
node,
messageId: 'illegalMethod',
data: {
method: calleeName,
replacement: 'jest.fn'
}
});
return;
}
context.report({
node,
messageId: 'illegalJasmine'
});
}
},
MemberExpression(node) {
if ((0, _utils2.isSupportedAccessor)(node.object, 'jasmine')) {
const {
parent,
property
} = node;
if (parent && parent.type === _utils.AST_NODE_TYPES.AssignmentExpression) {
if ((0, _utils2.isSupportedAccessor)(property, 'DEFAULT_TIMEOUT_INTERVAL')) {
const {
right
} = parent;
if (right.type === _utils.AST_NODE_TYPES.Literal) {
context.report({
fix: fixer => [fixer.replaceText(parent, `jest.setTimeout(${right.value})`)],
node,
messageId: 'illegalJasmine'
});
return;
}
}
context.report({
node,
messageId: 'illegalJasmine'
});
}
}
}
};
}
});
exports.default = _default;