@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
66 lines (64 loc) • 2.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.name = exports.default = void 0;
var _eslintCodemodUtils = require("eslint-codemod-utils");
var _createLintRule = require("../utils/create-lint-rule");
var name = exports.name = 'no-to-match-snapshot';
var rule = (0, _createLintRule.createLintRule)({
meta: {
name: name,
type: 'problem',
docs: {
description: 'Disallow using toMatchSnapshot() and toMatchInlineSnapshot() in unit tests. Snapshot assertions should be replaced with explicit assertions.',
recommended: false,
severity: 'error'
},
messages: {
avoidSnapshots: 'Avoid snapshot matchers in unit tests. Replace toMatchSnapshot()/toMatchInlineSnapshot() with explicit assertions.'
}
},
create: function create(context) {
var snapshotMatchers = new Set(['toMatchSnapshot', 'toMatchInlineSnapshot']);
var _getStaticString = function getStaticString(node) {
if (!node) {
return null;
}
if (node.type === 'Literal' && typeof node.value === 'string') {
return node.value;
}
if (node.type === 'BinaryExpression' && node.operator === '+') {
var left = _getStaticString(node.left);
var right = _getStaticString(node.right);
return left !== null && right !== null ? left + right : null;
}
return null;
};
var isExpectCall = function isExpectCall(node) {
return (0, _eslintCodemodUtils.isNodeOfType)(node, 'CallExpression') && (0, _eslintCodemodUtils.isNodeOfType)(node.callee, 'Identifier') && node.callee.name === 'expect';
};
return {
CallExpression: function CallExpression(node) {
// Handle expect(x).toMatchSnapshot() and expect(x).toMatchInlineSnapshot()
if ((0, _eslintCodemodUtils.isNodeOfType)(node.callee, 'MemberExpression') && !node.callee.computed && (0, _eslintCodemodUtils.isNodeOfType)(node.callee.property, 'Identifier') && isExpectCall(node.callee.object) && snapshotMatchers.has(node.callee.property.name)) {
context.report({
node: node.callee.property,
messageId: 'avoidSnapshots'
});
}
// Handle expect(x)['toMatch' + 'Snapshot']() / ['toMatch' + 'InlineSnapshot']
if ((0, _eslintCodemodUtils.isNodeOfType)(node.callee, 'MemberExpression') && node.callee.computed && isExpectCall(node.callee.object)) {
var prop = _getStaticString(node.callee.property);
if (prop && snapshotMatchers.has(prop)) {
context.report({
node: node.callee.property,
messageId: 'avoidSnapshots'
});
}
}
}
};
}
});
var _default = exports.default = rule;