Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslint-plugin-testing-library

Package Overview
Dependencies
Maintainers
3
Versions
164
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-testing-library - npm Package Compare versions

Comparing version 4.0.0-beta.2 to 4.0.0-beta.3

50

detect-testing-library-utils.js

@@ -13,2 +13,3 @@ "use strict";

return (context, optionsWithDefault) => {
var _a;
let importedTestingLibraryNode = null;

@@ -18,3 +19,3 @@ let importedCustomModuleNode = null;

const customModule = context.settings['testing-library/utils-module'];
const customRenders = context.settings['testing-library/custom-renders'];
const customRenders = (_a = context.settings['testing-library/custom-renders']) !== null && _a !== void 0 ? _a : [];
function isTestingLibraryUtil(node, isUtilCallback) {

@@ -26,2 +27,5 @@ if (!node) {

const referenceNodeIdentifier = node_utils_1.getPropertyIdentifierNode(referenceNode);
if (!referenceNodeIdentifier) {
return false;
}
const importedUtilSpecifier = getImportedUtilSpecifier(referenceNodeIdentifier);

@@ -83,3 +87,4 @@ const originalNodeName = node_utils_1.isImportSpecifier(importedUtilSpecifier) &&

return (validNames.includes(identifierNodeName) ||
validNames.includes(originalNodeName));
(!!originalNodeName &&
validNames.includes(originalNodeName)));
});

@@ -120,3 +125,5 @@ };

}
const parentMemberExpression = node_utils_1.isMemberExpression(node.parent) ? node.parent : undefined;
const parentMemberExpression = node.parent && node_utils_1.isMemberExpression(node.parent)
? node.parent
: undefined;
if (!parentMemberExpression) {

@@ -151,3 +158,5 @@ return false;

}
const parentMemberExpression = node_utils_1.isMemberExpression(node.parent) ? node.parent : undefined;
const parentMemberExpression = node.parent && node_utils_1.isMemberExpression(node.parent)
? node.parent
: undefined;
if (!parentMemberExpression) {

@@ -182,3 +191,9 @@ return false;

const isRenderVariableDeclarator = (node) => {
if (!node.init) {
return false;
}
const initIdentifierNode = node_utils_1.getDeepestIdentifierNode(node.init);
if (!initIdentifierNode) {
return false;
}
return isRenderUtil(initIdentifierNode);

@@ -215,3 +230,3 @@ };

if (!node) {
return null;
return undefined;
}

@@ -260,4 +275,7 @@ if (node_utils_1.isImportDeclaration(node)) {

const getImportedUtilSpecifier = (node) => {
const identifierName = node_utils_1.getPropertyIdentifierNode(node)
.name;
var _a;
const identifierName = (_a = node_utils_1.getPropertyIdentifierNode(node)) === null || _a === void 0 ? void 0 : _a.name;
if (!identifierName) {
return undefined;
}
return findImportedUtilSpecifier(identifierName);

@@ -269,2 +287,3 @@ };

const isNodeComingFromTestingLibrary = (node) => {
var _a;
const importNode = getImportedUtilSpecifier(node);

@@ -274,4 +293,6 @@ if (!importNode) {

}
const identifierName = node_utils_1.getPropertyIdentifierNode(node)
.name;
const identifierName = (_a = node_utils_1.getPropertyIdentifierNode(node)) === null || _a === void 0 ? void 0 : _a.name;
if (!identifierName) {
return false;
}
return node_utils_1.hasImportMatch(importNode, identifierName);

@@ -312,3 +333,4 @@ };

}
if (!importedCustomModuleNode &&
if (customModule &&
!importedCustomModuleNode &&
String(node.source.value).endsWith(customModule)) {

@@ -332,3 +354,4 @@ importedCustomModuleNode = node;

if (!importedCustomModuleNode &&
args.some((arg) => node_utils_1.isLiteral(arg) &&
args.some((arg) => customModule &&
node_utils_1.isLiteral(arg) &&
typeof arg.value === 'string' &&

@@ -351,7 +374,8 @@ arg.value.endsWith(customModule))) {

enhancedRuleInstructions[instruction] = (node) => {
var _a, _b;
if (instruction in detectionInstructions) {
detectionInstructions[instruction](node);
(_a = detectionInstructions[instruction]) === null || _a === void 0 ? void 0 : _a.call(detectionInstructions, node);
}
if (canReportErrors() && ruleInstructions[instruction]) {
return ruleInstructions[instruction](node);
return (_b = ruleInstructions[instruction]) === null || _b === void 0 ? void 0 : _b.call(ruleInstructions, node);
}

@@ -358,0 +382,0 @@ };

@@ -136,6 +136,6 @@ "use strict";

const parent = node.parent;
if (isCallExpression(parent)) {
if (isCallExpression(parent) && parent.parent) {
return hasThenProperty(parent.parent);
}
return hasThenProperty(parent);
return !!parent && hasThenProperty(parent);
}

@@ -166,3 +166,4 @@ exports.hasChainedThen = hasChainedThen;

}
return (isArrayExpression(closestCallExpression.parent) &&
return (!!closestCallExpression.parent &&
isArrayExpression(closestCallExpression.parent) &&
isCallExpression(closestCallExpression.parent.parent) &&

@@ -177,2 +178,5 @@ (isPromiseAll(closestCallExpression.parent.parent) ||

for (const node of suspiciousNodes) {
if (!node || !node.parent) {
continue;
}
if (experimental_utils_1.ASTUtils.isAwaitExpression(node.parent)) {

@@ -261,3 +265,4 @@ return true;

function getReferenceNode(node) {
if (isMemberExpression(node.parent) || isCallExpression(node.parent)) {
if (node.parent &&
(isMemberExpression(node.parent) || isCallExpression(node.parent))) {
return getReferenceNode(node.parent);

@@ -297,5 +302,6 @@ }

if (isNegated) {
matcher = isMemberExpression(node.parent)
? experimental_utils_1.ASTUtils.getPropertyName(node.parent)
: null;
matcher =
node.parent && isMemberExpression(node.parent)
? experimental_utils_1.ASTUtils.getPropertyName(node.parent)
: null;
}

@@ -311,2 +317,3 @@ if (!matcher) {

experimental_utils_1.ASTUtils.isIdentifier(node.callee) &&
node.parent &&
isMemberExpression(node.parent) &&

@@ -313,0 +320,0 @@ node.callee.name === 'expect') {

{
"name": "eslint-plugin-testing-library",
"version": "4.0.0-beta.2",
"version": "4.0.0-beta.3",
"description": "ESLint rules for Testing Library",

@@ -5,0 +5,0 @@ "keywords": [

@@ -21,3 +21,2 @@ "use strict";

},
fixable: null,
schema: [],

@@ -39,3 +38,3 @@ },

const closestCallExpressionNode = node_utils_1.findClosestCallExpressionNode(node, true);
if (!closestCallExpressionNode) {
if (!closestCallExpressionNode || !closestCallExpressionNode.parent) {
return;

@@ -42,0 +41,0 @@ }

@@ -20,3 +20,2 @@ "use strict";

},
fixable: null,
schema: [],

@@ -38,3 +37,3 @@ },

const closestCallExpression = node_utils_1.findClosestCallExpressionNode(node, true);
if (!closestCallExpression) {
if (!closestCallExpression || !closestCallExpression.parent) {
return;

@@ -41,0 +40,0 @@ }

@@ -20,3 +20,2 @@ "use strict";

},
fixable: null,
schema: [],

@@ -47,3 +46,3 @@ },

const closestCallExpression = node_utils_1.findClosestCallExpressionNode(node, true);
if (!closestCallExpression) {
if (!closestCallExpression || !closestCallExpression.parent) {
return;

@@ -50,0 +49,0 @@ }

@@ -21,3 +21,2 @@ "use strict";

},
fixable: null,
schema: [

@@ -61,4 +60,5 @@ {

function getFileNameData() {
var _a;
const splitPath = getFilename().split('/');
const fileNameWithExtension = splitPath.pop();
const fileNameWithExtension = (_a = splitPath.pop()) !== null && _a !== void 0 ? _a : '';
const parent = splitPath.pop();

@@ -74,2 +74,3 @@ const fileName = fileNameWithExtension.split('.').shift();

function isTestIdAttribute(name) {
var _a;
if (typeof attr === 'string') {

@@ -79,3 +80,3 @@ return attr === name;

else {
return attr.includes(name);
return (_a = attr === null || attr === void 0 ? void 0 : attr.includes(name)) !== null && _a !== void 0 ? _a : false;
}

@@ -85,3 +86,4 @@ }

JSXIdentifier: (node) => {
if (!node_utils_1.isJSXAttribute(node.parent) ||
if (!node.parent ||
!node_utils_1.isJSXAttribute(node.parent) ||
!node_utils_1.isLiteral(node.parent.value) ||

@@ -93,3 +95,3 @@ !isTestIdAttribute(node.name)) {

const { fileName } = getFileNameData();
const regex = getTestIdValidator(fileName);
const regex = getTestIdValidator(fileName !== null && fileName !== void 0 ? fileName : '');
if (value && typeof value === 'string' && !regex.test(value)) {

@@ -96,0 +98,0 @@ context.report({

@@ -21,3 +21,2 @@ "use strict";

},
fixable: null,
schema: [],

@@ -29,3 +28,7 @@ },

'AwaitExpression > CallExpression'(node) {
var _a;
const simulateEventFunctionIdentifier = node_utils_1.getDeepestIdentifierNode(node);
if (!simulateEventFunctionIdentifier) {
return;
}
const isSimulateEventMethod = helpers.isUserEventMethod(simulateEventFunctionIdentifier) ||

@@ -38,7 +41,10 @@ helpers.isFireEventMethod(simulateEventFunctionIdentifier);

const hasDelay = node_utils_1.isObjectExpression(lastArg) &&
lastArg.properties.some((property) => node_utils_1.isProperty(property) &&
experimental_utils_1.ASTUtils.isIdentifier(property.key) &&
property.key.name === 'delay' &&
node_utils_1.isLiteral(property.value) &&
property.value.value > 0);
lastArg.properties.some((property) => {
var _a;
return (_a = (node_utils_1.isProperty(property) &&
experimental_utils_1.ASTUtils.isIdentifier(property.key) &&
property.key.name === 'delay' &&
node_utils_1.isLiteral(property.value) &&
property.value.value)) !== null && _a !== void 0 ? _a : 0 > 0;
});
const simulateEventFunctionName = simulateEventFunctionIdentifier.name;

@@ -53,3 +59,3 @@ if (USER_EVENT_ASYNC_EXCEPTIONS.includes(simulateEventFunctionName) &&

data: {
name: `${node_utils_1.getPropertyIdentifierNode(node).name}.${simulateEventFunctionName}`,
name: `${(_a = node_utils_1.getPropertyIdentifierNode(node)) === null || _a === void 0 ? void 0 : _a.name}.${simulateEventFunctionName}`,
},

@@ -56,0 +62,0 @@ });

@@ -19,3 +19,2 @@ "use strict";

},
fixable: null,
schema: [],

@@ -22,0 +21,0 @@ },

@@ -20,3 +20,2 @@ "use strict";

},
fixable: null,
schema: [],

@@ -67,2 +66,5 @@ },

const callExpressionIdentifier = node_utils_1.getDeepestIdentifierNode(node);
if (!callExpressionIdentifier) {
return;
}
if (helpers.isRenderUtil(callExpressionIdentifier)) {

@@ -83,4 +85,10 @@ detectRenderWrapper(callExpressionIdentifier);

},
VariableDeclarator(node) {
VariableDeclarator: function (node) {
if (!node.init) {
return;
}
const initIdentifierNode = node_utils_1.getDeepestIdentifierNode(node.init);
if (!initIdentifierNode) {
return;
}
const isRenderWrapperVariableDeclarator = initIdentifierNode

@@ -98,2 +106,5 @@ ? renderWrapperNames.includes(initIdentifierNode.name)

const nodeValue = containerIndex !== -1 && node.id.properties[containerIndex].value;
if (!nodeValue) {
return;
}
if (experimental_utils_1.ASTUtils.isIdentifier(nodeValue)) {

@@ -109,4 +120,4 @@ containerName = nodeValue.name;

}
else {
renderResultVarName = experimental_utils_1.ASTUtils.isIdentifier(node.id) && node.id.name;
else if (experimental_utils_1.ASTUtils.isIdentifier(node.id)) {
renderResultVarName = node.id.name;
}

@@ -113,0 +124,0 @@ },

@@ -20,3 +20,2 @@ "use strict";

},
fixable: null,
schema: [],

@@ -37,3 +36,9 @@ },

VariableDeclarator(node) {
if (!node.init) {
return;
}
const initIdentifierNode = node_utils_1.getDeepestIdentifierNode(node.init);
if (!initIdentifierNode) {
return;
}
const isRenderWrapperVariableDeclarator = initIdentifierNode

@@ -51,3 +56,6 @@ ? renderWrapperNames.includes(initIdentifierNode.name)

property.key.name === 'debug') {
suspiciousDebugVariableNames.push(node_utils_1.getDeepestIdentifierNode(property.value).name);
const identifierNode = node_utils_1.getDeepestIdentifierNode(property.value);
if (identifierNode) {
suspiciousDebugVariableNames.push(identifierNode.name);
}
}

@@ -62,2 +70,5 @@ }

const callExpressionIdentifier = node_utils_1.getDeepestIdentifierNode(node);
if (!callExpressionIdentifier) {
return;
}
if (helpers.isRenderUtil(callExpressionIdentifier)) {

@@ -68,2 +79,5 @@ detectRenderWrapper(callExpressionIdentifier);

const referenceIdentifier = node_utils_1.getPropertyIdentifierNode(referenceNode);
if (!referenceIdentifier) {
return;
}
const isDebugUtil = helpers.isDebugUtil(callExpressionIdentifier);

@@ -70,0 +84,0 @@ const isDeclaredDebugVariable = suspiciousDebugVariableNames.includes(callExpressionIdentifier.name);

@@ -21,3 +21,2 @@ "use strict";

},
fixable: null,
schema: [],

@@ -28,5 +27,6 @@ },

function reportImportReferences(references) {
references.forEach((reference) => {
for (const reference of references) {
const utilsUsage = reference.identifier.parent;
if (node_utils_1.isMemberExpression(utilsUsage) &&
if (utilsUsage &&
node_utils_1.isMemberExpression(utilsUsage) &&
experimental_utils_1.ASTUtils.isIdentifier(utilsUsage.property) &&

@@ -39,3 +39,3 @@ utilsUsage.property.name === 'cleanup') {

}
});
}
}

@@ -42,0 +42,0 @@ function reportCandidateModule(moduleNode) {

@@ -20,3 +20,2 @@ "use strict";

},
fixable: null,
schema: [],

@@ -23,0 +22,0 @@ },

@@ -20,3 +20,2 @@ "use strict";

},
fixable: null,
schema: [],

@@ -40,2 +39,5 @@ },

const domElementIdentifier = node_utils_1.getDeepestIdentifierNode(node);
if (!domElementIdentifier) {
return;
}
if (helpers.isAsyncQuery(domElementIdentifier) ||

@@ -56,3 +58,5 @@ node_utils_1.isPromiseIdentifier(domElementIdentifier)) {

const variableDeclarator = definition.node;
checkSuspiciousNode(variableDeclarator.init, node);
if (variableDeclarator.init) {
checkSuspiciousNode(variableDeclarator.init, node);
}
}

@@ -59,0 +63,0 @@ }

@@ -18,3 +18,6 @@ "use strict";

}
return findClosestBeforeHook(node.parent, testingFrameworkSetupHooksToFilter);
if (node.parent) {
return findClosestBeforeHook(node.parent, testingFrameworkSetupHooksToFilter);
}
return null;
}

@@ -34,3 +37,2 @@ exports.findClosestBeforeHook = findClosestBeforeHook;

},
fixable: null,
schema: [

@@ -64,2 +66,5 @@ {

const callExpressionIdentifier = node_utils_1.getDeepestIdentifierNode(node);
if (!callExpressionIdentifier) {
return;
}
const isRenderIdentifier = helpers.isRenderUtil(callExpressionIdentifier);

@@ -66,0 +71,0 @@ if (isRenderIdentifier) {

@@ -20,3 +20,2 @@ "use strict";

},
fixable: null,
schema: [],

@@ -29,2 +28,5 @@ },

const parentIdentifier = node_utils_1.getPropertyIdentifierNode(parentCallExpression);
if (!parentIdentifier) {
return false;
}
return helpers.isAsyncUtil(parentIdentifier, [

@@ -31,0 +33,0 @@ 'waitFor',

@@ -19,3 +19,2 @@ "use strict";

},
fixable: null,
schema: [],

@@ -38,4 +37,10 @@ },

function reportMultipleAssertion(node) {
if (!node.parent) {
return;
}
const callExpressionNode = node.parent.parent;
const callExpressionIdentifier = node_utils_1.getPropertyIdentifierNode(callExpressionNode);
if (!callExpressionIdentifier) {
return;
}
if (!helpers.isAsyncUtil(callExpressionIdentifier, ['waitFor'])) {

@@ -42,0 +47,0 @@ return;

@@ -19,3 +19,2 @@ "use strict";

},
fixable: null,
schema: [],

@@ -39,4 +38,10 @@ },

function reportSideEffects(node) {
if (!node.parent) {
return;
}
const callExpressionNode = node.parent.parent;
const callExpressionIdentifier = node_utils_1.getPropertyIdentifierNode(callExpressionNode);
if (!callExpressionIdentifier) {
return;
}
if (!helpers.isAsyncUtil(callExpressionIdentifier, ['waitFor'])) {

@@ -43,0 +48,0 @@ return;

@@ -21,3 +21,2 @@ "use strict";

},
fixable: null,
schema: [],

@@ -31,2 +30,5 @@ },

const callExpression = node_utils_1.findClosestCallExpressionNode(n);
if (!callExpression) {
return null;
}
if (experimental_utils_1.ASTUtils.isIdentifier(callExpression.callee) &&

@@ -41,3 +43,5 @@ helpers.isAsyncUtil(callExpression.callee)) {

}
n = node_utils_1.findClosestCallExpressionNode(callExpression.parent);
if (callExpression.parent) {
n = node_utils_1.findClosestCallExpressionNode(callExpression.parent);
}
} while (n !== null);

@@ -44,0 +48,0 @@ return null;

@@ -9,3 +9,3 @@ "use strict";

exports.RULE_NAME = 'prefer-explicit-assert';
const isAtTopLevel = (node) => node.parent.parent.type === 'ExpressionStatement';
const isAtTopLevel = (node) => { var _a; return !!((_a = node === null || node === void 0 ? void 0 : node.parent) === null || _a === void 0 ? void 0 : _a.parent) && node.parent.parent.type === 'ExpressionStatement'; };
exports.default = create_testing_library_rule_1.createTestingLibraryRule({

@@ -24,3 +24,2 @@ name: exports.RULE_NAME,

},
fixable: null,
schema: [

@@ -27,0 +26,0 @@ {

@@ -14,2 +14,3 @@ "use strict";

function findRenderDefinitionDeclaration(scope, query) {
var _a;
if (!scope) {

@@ -20,6 +21,6 @@ return null;

if (variable) {
return variable.defs
return ((_a = variable.defs
.map(({ name }) => name)
.filter(experimental_utils_1.ASTUtils.isIdentifier)
.find(({ name }) => name === query);
.find(({ name }) => name === query)) !== null && _a !== void 0 ? _a : null);
}

@@ -92,3 +93,3 @@ return findRenderDefinitionDeclaration(scope.upper, query);

if (helpers.isCustomQuery(property)) {
return;
return null;
}

@@ -119,3 +120,3 @@ const newCode = `${caller}.${queryVariant}${queryMethod}(${callArguments

.callee)) {
return;
return null;
}

@@ -132,3 +133,4 @@ const findByMethod = `${queryVariant}${queryMethod}`;

}
if (node_utils_1.isObjectPattern(definition.parent.parent)) {
if (definition.parent &&
node_utils_1.isObjectPattern(definition.parent.parent)) {
const allVariableDeclarations = definition.parent.parent;

@@ -135,0 +137,0 @@ if (allVariableDeclarations.properties.some((p) => node_utils_1.isProperty(p) &&

@@ -21,3 +21,2 @@ "use strict";

type: 'suggestion',
fixable: null,
},

@@ -24,0 +23,0 @@ defaultOptions: [],

@@ -31,3 +31,2 @@ "use strict";

},
fixable: null,
schema: [],

@@ -48,8 +47,9 @@ },

if (node_utils_1.isObjectPattern(node.id)) {
const identifiers = node.id.properties
.filter((property) => node_utils_1.isProperty(property) &&
experimental_utils_1.ASTUtils.isIdentifier(property.key) &&
helpers.isQuery(property.key))
.map((property) => property.key.name);
safeDestructuredQueries.push(...identifiers);
for (const property of node.id.properties) {
if (node_utils_1.isProperty(property) &&
experimental_utils_1.ASTUtils.isIdentifier(property.key) &&
helpers.isQuery(property.key)) {
safeDestructuredQueries.push(property.key.name);
}
}
}

@@ -56,0 +56,0 @@ }

@@ -72,3 +72,2 @@ "use strict";

],
fixable: null,
},

@@ -75,0 +74,0 @@ defaultOptions: [{ allowedMethods: [] }],

@@ -52,5 +52,6 @@ "use strict";

const newImports = node.specifiers
.filter((specifier) => node_utils_1.isImportSpecifier(specifier) &&
!excludedImports.includes(specifier.imported.name))
.map((specifier) => specifier.imported.name);
.map((specifier) => node_utils_1.isImportSpecifier(specifier) &&
!excludedImports.includes(specifier.imported.name) &&
specifier.imported.name)
.filter(Boolean);
newImports.push('waitFor');

@@ -71,2 +72,5 @@ const newNode = `import { ${newImports.join(',')} } from '${node.source.value}';`;

const callExpressionNode = node_utils_1.findClosestCallExpressionNode(node);
if (!callExpressionNode) {
return null;
}
const [arg] = callExpressionNode.arguments;

@@ -129,3 +133,3 @@ const fixers = [];

}
else {
else if (testingLibraryNode) {
if (testingLibraryNode.specifiers.length === 1 &&

@@ -132,0 +136,0 @@ node_utils_1.isImportNamespaceSpecifier(testingLibraryNode.specifiers[0])) {

@@ -22,3 +22,2 @@ "use strict";

},
fixable: null,
schema: [],

@@ -38,2 +37,5 @@ },

const callExpressionIdentifier = node_utils_1.getDeepestIdentifierNode(node);
if (!callExpressionIdentifier) {
return;
}
if (helpers.isRenderUtil(callExpressionIdentifier)) {

@@ -44,2 +46,5 @@ detectRenderWrapper(callExpressionIdentifier);

VariableDeclarator(node) {
if (!node.init) {
return;
}
const initIdentifierNode = node_utils_1.getDeepestIdentifierNode(node.init);

@@ -57,2 +62,5 @@ if (!initIdentifierNode) {

const renderResultName = experimental_utils_1.ASTUtils.isIdentifier(node.id) && node.id.name;
if (!renderResultName) {
return;
}
const isAllowedRenderResultName = ALLOWED_VAR_NAMES.includes(renderResultName);

@@ -59,0 +67,0 @@ if (isAllowedRenderResultName) {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc