123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- var _experimentalUtils = require("@typescript-eslint/experimental-utils");
- var _utils = require("./utils");
- var _default = (0, _utils.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, _utils.getNodeName)(callee);
- if (!calleeName) {
- return;
- }
- if (calleeName === 'spyOn' || calleeName === 'spyOnProperty' || calleeName === 'fail' || calleeName === 'pending') {
- if ((0, _utils.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 === _experimentalUtils.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, _utils.isSupportedAccessor)(node.object, 'jasmine')) {
- const {
- parent,
- property
- } = node;
- if (parent && parent.type === _experimentalUtils.AST_NODE_TYPES.AssignmentExpression) {
- if ((0, _utils.isSupportedAccessor)(property, 'DEFAULT_TIMEOUT_INTERVAL')) {
- const {
- right
- } = parent;
- if (right.type === _experimentalUtils.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;
|