Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support unassessed #45

Merged
merged 6 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
251 changes: 160 additions & 91 deletions lib/applyFixes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ const detectIndent = require('detect-indent');
const SourceCodeFixer = require('eslint/lib/linter/source-code-fixer');
const indentString = require('./indentString');
const maybeApplyPrettier = require('./maybeApplyPrettier');
const camelCase = require('lodash.camelcase');

function stringify(obj, indentationWidth, inspect) {
function stringify(obj, indentationWidth, inspect, expectName = 'expect') {
if (obj.includes('\n') && !/^\n*[ \t]/.test(obj)) {
return `expect.unindent\`${indentString(
return `${expectName}.unindent\`${indentString(
obj.replace(/`/g, '\\`'),
indentationWidth
)}\``;
Expand Down Expand Up @@ -45,18 +46,142 @@ function getNodeIndent(node, sourceCode, byLastLine) {
}
}

function fixUnexpected(
node,
sourceCode,
indentationWidth,
{ status, subject, inspect, assertionName }
) {
// Find the indentation of the literal being replaced,
// falling back to that of 'to equal snapshot'
let indent;
if (node.arguments.length >= 3) {
indent = getNodeIndent(node.arguments[2], sourceCode);
} else {
indent = getNodeIndent(node.arguments[1], sourceCode);
}
const fixes = [];
let stringifiedSubject;
let newAssertionName = assertionName;
if (typeof subject === 'string' && assertionName === 'to equal snapshot') {
stringifiedSubject = stringify(subject, indentationWidth, inspect).replace(
/\n^(?=[^\n])/gm,
`\n${indent}`
);
} else if (
isSimpleObjectTree(subject) &&
assertionName === 'to equal snapshot'
) {
stringifiedSubject = inspect(subject, indentationWidth);
} else {
newAssertionName = 'to inspect as snapshot';
stringifiedSubject = stringify(
inspect(subject, indentationWidth),
indentationWidth,
inspect
).replace(/\n^(?=[^\n])/gm, `\n${indent}`);
}
// Take compound assertions into account:
const assertionArgument =
node.arguments[node.arguments.length - (status === 'missing' ? 1 : 2)];
const newAssertionNameWithPrefix = assertionArgument.value.replace(
assertionName,
newAssertionName
);

if (status === 'missing') {
if (newAssertionName === assertionName) {
fixes.unshift(
ruleFixer.insertTextAfter(assertionArgument, `, ${stringifiedSubject}`)
);
} else {
fixes.unshift(
ruleFixer.replaceText(
assertionArgument,
`'${newAssertionNameWithPrefix}', ${stringifiedSubject}`
)
);
}
} else if (status === 'mismatch') {
if (newAssertionName !== assertionName) {
fixes.unshift(
ruleFixer.replaceText(
assertionArgument,
`'${newAssertionNameWithPrefix}'`
)
);
}
fixes.unshift(
ruleFixer.replaceText(
node.arguments[node.arguments.length - 1],
stringifiedSubject
)
);
}
return fixes;
}

function fixUnassessed(
node,
sourceCode,
indentationWidth,
{ status, subject, inspect, assertionName }
) {
// Find the indentation of the literal being replaced,
// falling back to that of assess(...)
let indent;
if (node.arguments.length > 0) {
indent = getNodeIndent(node.arguments[0], sourceCode);
} else {
// FIXME: Probably needs to be +1?
indent = getNodeIndent(node, sourceCode);
}
const fixes = [];
let stringifiedSubject;
let newAssertionName = assertionName;
if (typeof subject === 'string' && assertionName === 'to equal snapshot') {
stringifiedSubject = stringify(
subject,
indentationWidth,
inspect,
node.callee.object.callee.name
).replace(/\n^(?=[^\n])/gm, `\n${indent}`);
} else if (
isSimpleObjectTree(subject) &&
assertionName === 'to equal snapshot'
) {
stringifiedSubject = inspect(subject, indentationWidth);
} else {
newAssertionName = 'to inspect as snapshot';
stringifiedSubject = stringify(
inspect(subject, indentationWidth),
indentationWidth,
inspect,
node.callee.object.callee.name
).replace(/\n^(?=[^\n])/gm, `\n${indent}`);
}
if (newAssertionName !== assertionName) {
fixes.unshift(
ruleFixer.replaceText(node.callee.property, camelCase(newAssertionName))
);
}

if (status === 'missing') {
const endParenToken = sourceCode.getLastToken(node);
fixes.unshift(
ruleFixer.insertTextBefore(endParenToken, stringifiedSubject)
);
} else if (status === 'mismatch') {
fixes.unshift(ruleFixer.replaceText(node.arguments[0], stringifiedSubject));
}
return fixes;
}

function applyFixes(fixes) {
const fixesByFileName = {};
let numFixedExpects = 0;
for (const {
fileName,
lineNumber,
columnNumber,
status,
subject,
inspect,
assertionName
} of fixes) {
for (const fix of fixes) {
const { fileName, lineNumber, columnNumber } = fix;
const sourceCode = getSourceCodeMemoized(fileName);
if (!sourceCode) {
// Probably a parse error, give up.
Expand All @@ -69,7 +194,8 @@ function applyFixes(fixes) {
}

new Traverser().traverse(sourceCode.ast, {
enter(node, parent) {
enter(node) {
let appliedFixes;
if (
node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
Expand All @@ -80,86 +206,29 @@ function applyFixes(fixes) {
) {
numFixedExpects += 1;

// Find the indentation of the literal being replaced,
// falling back to that of 'to equal snapshot'
let indent;
if (node.arguments.length >= 3) {
indent = getNodeIndent(node.arguments[2], sourceCode);
} else {
indent = getNodeIndent(node.arguments[1], sourceCode);
}
const fixes = [];
let stringifiedSubject;
let newAssertionName = assertionName;
if (
typeof subject === 'string' &&
assertionName === 'to equal snapshot'
) {
stringifiedSubject = stringify(
subject,
indentationWidth,
inspect
).replace(/\n^(?=[^\n])/gm, `\n${indent}`);
} else if (
isSimpleObjectTree(subject) &&
assertionName === 'to equal snapshot'
) {
stringifiedSubject = inspect(subject, indentationWidth);
} else {
newAssertionName = 'to inspect as snapshot';
stringifiedSubject = stringify(
inspect(subject, indentationWidth),
indentationWidth,
inspect
).replace(/\n^(?=[^\n])/gm, `\n${indent}`);
}
// Take compound assertions into account:
const assertionArgument =
node.arguments[
node.arguments.length - (status === 'missing' ? 1 : 2)
];
const newAssertionNameWithPrefix = assertionArgument.value.replace(
assertionName,
newAssertionName
);
appliedFixes = fixUnexpected(node, sourceCode, indentationWidth, fix);
} else if (
node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'CallExpression' &&
node.callee.object.callee.type === 'Identifier' &&
['expect', 'assess'].includes(node.callee.object.callee.name) &&
node.callee.property.type === 'Identifier' &&
['toEqualSnapshot', 'toInspectAsSnapshot'].includes(
node.callee.property.name
) &&
node.callee.property.loc.start.line === lineNumber &&
node.callee.property.loc.start.column + 1 === columnNumber
) {
numFixedExpects += 1;

appliedFixes = fixUnassessed(node, sourceCode, indentationWidth, fix);
}

if (status === 'missing') {
if (newAssertionName === assertionName) {
fixes.unshift(
ruleFixer.insertTextAfter(
assertionArgument,
`, ${stringifiedSubject}`
)
);
} else {
fixes.unshift(
ruleFixer.replaceText(
assertionArgument,
`'${newAssertionNameWithPrefix}', ${stringifiedSubject}`
)
);
}
} else if (status === 'mismatch') {
if (newAssertionName !== assertionName) {
fixes.unshift(
ruleFixer.replaceText(
assertionArgument,
`'${newAssertionNameWithPrefix}'`
)
);
}
fixes.unshift(
ruleFixer.replaceText(
node.arguments[node.arguments.length - 1],
stringifiedSubject
)
);
}
if (fixes.length > 0) {
(fixesByFileName[fileName] = fixesByFileName[fileName] || []).push(
...fixes
);
}
if (appliedFixes && appliedFixes.length > 0) {
(fixesByFileName[fileName] = fixesByFileName[fileName] || []).push(
...appliedFixes
);
}
}
});
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"detect-indent": "^6.0.0",
"eslint": "^6.0.0",
"is-node": "^1.0.2",
"lodash.camelcase": "^4.3.0",
"memoizesync": "^1.1.1"
},
"devDependencies": {
Expand Down Expand Up @@ -55,6 +56,7 @@
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.0.0",
"rollup-plugin-terser": "^5.0.0",
"unassessed": "^0.2.1",
"unexpected": "^11.1.1"
},
"peerDependencies": {
Expand Down
Loading