Skip to content

Commit

Permalink
Merge branch 'main' into prefer-jest-globals
Browse files Browse the repository at this point in the history
  • Loading branch information
MadeinFrance committed Jan 29, 2024
2 parents 1453eb8 + 541760c commit 13adf46
Show file tree
Hide file tree
Showing 2 changed files with 255 additions and 271 deletions.
236 changes: 118 additions & 118 deletions src/rules/prefer-importing-jest-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,131 +51,131 @@ export default createRule({
jestFunction => !importedJestFunctions.includes(jestFunction),
);

if (jestFunctionsToImport.length > 0) {
const node = context.getSourceCode().ast;
const jestFunctionsToImportFormatted =
jestFunctionsToImport.join(', ');

context.report({
node,
messageId: 'preferImportingJestGlobal',
data: { jestFunctions: jestFunctionsToImportFormatted },
fix(fixer) {
const sourceCode = context.getSourceCode();
const usesImport = sourceCode.ast.body.some(
node => node.type === 'ImportDeclaration',
);
const [firstNode] = sourceCode.ast.body;

let firstNodeValue;

if (firstNode.type === 'ExpressionStatement') {
const firstExpression = firstNode.expression as Literal;
const { value } = firstExpression;

firstNodeValue = value;
}

const useStrictDirectiveExists =
firstNode.type === 'ExpressionStatement' &&
firstNodeValue === 'use strict';

if (useStrictDirectiveExists) {
return fixer.insertTextAfter(
firstNode,
`\n${createFixerImports(usesImport, jestFunctionsToImport)}`,
);
}

const importNode = sourceCode.ast.body.find(
node =>
node.type === 'ImportDeclaration' &&
node.source.value === '@jest/globals',
if (!jestFunctionsToImport.length) {
return;
}
const node = context.getSourceCode().ast;
const jestFunctionsToImportFormatted = jestFunctionsToImport.join(', ');

context.report({
node,
messageId: 'preferImportingJestGlobal',
data: { jestFunctions: jestFunctionsToImportFormatted },
fix(fixer) {
const sourceCode = context.getSourceCode();
const usesImport = sourceCode.ast.body.some(
node => node.type === 'ImportDeclaration',
);
const [firstNode] = sourceCode.ast.body;

let firstNodeValue;

if (firstNode.type === 'ExpressionStatement') {
const firstExpression = firstNode.expression as Literal;
const { value } = firstExpression;

firstNodeValue = value;
}

const useStrictDirectiveExists =
firstNode.type === 'ExpressionStatement' &&
firstNodeValue === 'use strict';

if (useStrictDirectiveExists) {
return fixer.insertTextAfter(
firstNode,
`\n${createFixerImports(usesImport, jestFunctionsToImport)}`,
);

if (importNode && importNode.type === 'ImportDeclaration') {
const existingImports = importNode.specifiers.map(specifier => {
/* istanbul ignore else */
if (specifier.type === 'ImportSpecifier') {
return specifier.imported?.name;
}

// istanbul ignore next
return null;
});
const allImports = [
...new Set([
...existingImports.filter(
(imp): imp is string => imp !== null,
),
...jestFunctionsToImport,
]),
];

return fixer.replaceText(
importNode,
createFixerImports(usesImport, allImports),
);
}

const requireNode = sourceCode.ast.body.find(
node =>
node.type === 'VariableDeclaration' &&
node.declarations.some(
declaration =>
declaration.init &&
(declaration.init as any).callee &&
(declaration.init as any).callee.name === 'require' &&
(declaration.init as any).arguments?.[0]?.type ===
'Literal' &&
(declaration.init as any).arguments?.[0]?.value ===
'@jest/globals',
}

const importNode = sourceCode.ast.body.find(
node =>
node.type === 'ImportDeclaration' &&
node.source.value === '@jest/globals',
);

if (importNode && importNode.type === 'ImportDeclaration') {
const existingImports = importNode.specifiers.map(specifier => {
/* istanbul ignore else */
if (specifier.type === 'ImportSpecifier') {
return specifier.imported?.name;
}

// istanbul ignore next
return null;
});
const allImports = [
...new Set([
...existingImports.filter(
(imp): imp is string => imp !== null,
),
);
...jestFunctionsToImport,
]),
];

if (requireNode && requireNode.type === 'VariableDeclaration') {
const existingImports =
requireNode.declarations[0]?.id.type === 'ObjectPattern'
? requireNode.declarations[0]?.id.properties?.map(
property => {
return fixer.replaceText(
importNode,
createFixerImports(usesImport, allImports),
);
}

const requireNode = sourceCode.ast.body.find(
node =>
node.type === 'VariableDeclaration' &&
node.declarations.some(
declaration =>
declaration.init &&
(declaration.init as any).callee &&
(declaration.init as any).callee.name === 'require' &&
(declaration.init as any).arguments?.[0]?.type ===
'Literal' &&
(declaration.init as any).arguments?.[0]?.value ===
'@jest/globals',
),
);

if (requireNode && requireNode.type === 'VariableDeclaration') {
const existingImports =
requireNode.declarations[0]?.id.type === 'ObjectPattern'
? requireNode.declarations[0]?.id.properties?.map(
property => {
/* istanbul ignore else */
if (property.type === 'Property') {
/* istanbul ignore else */
if (property.type === 'Property') {
/* istanbul ignore else */
if (property.key.type === 'Identifier') {
return property.key.name;
}
if (property.key.type === 'Identifier') {
return property.key.name;
}
}

// istanbul ignore next
return null;
},
) ||
// istanbul ignore next
[]
: // istanbul ignore next
[];
const allImports = [
...new Set([
...existingImports.filter(
(imp): imp is string => imp !== null,
),
...jestFunctionsToImport,
]),
];

// istanbul ignore next
return null;
},
) ||
// istanbul ignore next
[]
: // istanbul ignore next
[];
const allImports = [
...new Set([
...existingImports.filter(
(imp): imp is string => imp !== null,
),
...jestFunctionsToImport,
]),
];

return fixer.replaceText(
requireNode,
`${createFixerImports(usesImport, allImports)}`,
);
}

return fixer.insertTextBefore(
node,
`${createFixerImports(usesImport, jestFunctionsToImport)}\n`,
return fixer.replaceText(
requireNode,
`${createFixerImports(usesImport, allImports)}`,
);
},
});
}
}

return fixer.insertTextBefore(
node,
`${createFixerImports(usesImport, jestFunctionsToImport)}\n`,
);
},
});
},
};
},
Expand Down
Loading

0 comments on commit 13adf46

Please sign in to comment.