Skip to content

Commit

Permalink
prettier --write '**/*.{js,md}'
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jan 23, 2024
1 parent ff29624 commit 7a23319
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 123 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function Person(name) {
expect(
new Person('Eigil'),
'to inspect as snapshot',
"Person({ name: 'Eigil' })"
"Person({ name: 'Eigil' })",
);
```

Expand Down
46 changes: 23 additions & 23 deletions lib/applyFixes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function stringify(obj, indentationWidth, inspect, expectName = 'expect') {
if (obj.includes('\n') && !/^\n*[ \t]/.test(obj)) {
return `${expectName}.unindent\`${indentString(
obj.replace(/[`$]/g, '\\$&'),
indentationWidth
indentationWidth,
)}\``;
} else {
return inspect(obj, indentationWidth);
Expand All @@ -20,7 +20,7 @@ function stringify(obj, indentationWidth, inspect, expectName = 'expect') {

// Only parse each file once, even though multiple fixes have to be applied:
const getSourceCodeMemoized = require('memoizesync')(
require('./getSourceCode')
require('./getSourceCode'),
);

function getNodeIndent(node, sourceCode, byLastLine) {
Expand All @@ -32,7 +32,7 @@ function getNodeIndent(node, sourceCode, byLastLine) {
.split('');
const indentChars = srcCharsBeforeNode.slice(
0,
srcCharsBeforeNode.findIndex((char) => char !== ' ' && char !== '\t')
srcCharsBeforeNode.findIndex((char) => char !== ' ' && char !== '\t'),
);
const spaces = indentChars.filter((char) => char === ' ').length;
const tabs = indentChars.filter((char) => char === '\t').length;
Expand All @@ -50,7 +50,7 @@ function fixUnexpected(
node,
sourceCode,
indentationWidth,
{ status, subject, inspect, assertionName }
{ status, subject, inspect, assertionName },
) {
// Take compound assertions into account:
const assertionArgument =
Expand All @@ -60,7 +60,7 @@ function fixUnexpected(
!assertionArgument.value.endsWith(assertionName) // Allow compound
) {
throw new Error(
`unexpected-snapshot could not find '${assertionName}' assertion to patch up. Note that expect.it is not supported`
`unexpected-snapshot could not find '${assertionName}' assertion to patch up. Note that expect.it is not supported`,
);
}
// Find the indentation of the literal being replaced,
Expand All @@ -77,7 +77,7 @@ function fixUnexpected(
if (typeof subject === 'string' && assertionName === 'to equal snapshot') {
stringifiedSubject = stringify(subject, indentationWidth, inspect).replace(
/\n^(?=[^\n])/gm,
`\n${indent}`
`\n${indent}`,
);
} else if (
isSimpleObjectTree(subject) &&
Expand All @@ -89,41 +89,41 @@ function fixUnexpected(
stringifiedSubject = stringify(
inspect(subject, indentationWidth),
indentationWidth,
inspect
inspect,
).replace(/\n^(?=[^\n])/gm, `\n${indent}`);
}
const newAssertionNameWithPrefix = assertionArgument.value.replace(
assertionName,
newAssertionName
newAssertionName,
);

if (status === 'missing') {
if (newAssertionName === assertionName) {
fixes.unshift(
ruleFixer.insertTextAfter(assertionArgument, `, ${stringifiedSubject}`)
ruleFixer.insertTextAfter(assertionArgument, `, ${stringifiedSubject}`),
);
} else {
fixes.unshift(
ruleFixer.replaceText(
assertionArgument,
`'${newAssertionNameWithPrefix}', ${stringifiedSubject}`
)
`'${newAssertionNameWithPrefix}', ${stringifiedSubject}`,
),
);
}
} else if (status === 'mismatch') {
if (newAssertionName !== assertionName) {
fixes.unshift(
ruleFixer.replaceText(
assertionArgument,
`'${newAssertionNameWithPrefix}'`
)
`'${newAssertionNameWithPrefix}'`,
),
);
}
fixes.unshift(
ruleFixer.replaceText(
node.arguments[node.arguments.length - 1],
stringifiedSubject
)
stringifiedSubject,
),
);
}
return fixes;
Expand All @@ -133,7 +133,7 @@ function fixUnassessed(
node,
sourceCode,
indentationWidth,
{ status, subject, inspect, assertionName }
{ status, subject, inspect, assertionName },
) {
// Find the indentation of the literal being replaced,
// falling back to that of assess(...)
Expand All @@ -152,7 +152,7 @@ function fixUnassessed(
subject,
indentationWidth,
inspect,
node.callee.object.callee.name
node.callee.object.callee.name,
).replace(/\n^(?=[^\n])/gm, `\n${indent}`);
} else if (
isSimpleObjectTree(subject) &&
Expand All @@ -165,19 +165,19 @@ function fixUnassessed(
inspect(subject, indentationWidth),
indentationWidth,
inspect,
node.callee.object.callee.name
node.callee.object.callee.name,
).replace(/\n^(?=[^\n])/gm, `\n${indent}`);
}
if (newAssertionName !== assertionName) {
fixes.unshift(
ruleFixer.replaceText(node.callee.property, camelCase(newAssertionName))
ruleFixer.replaceText(node.callee.property, camelCase(newAssertionName)),
);
}

if (status === 'missing') {
const endParenToken = sourceCode.getLastToken(node);
fixes.unshift(
ruleFixer.insertTextBefore(endParenToken, stringifiedSubject)
ruleFixer.insertTextBefore(endParenToken, stringifiedSubject),
);
} else if (status === 'mismatch') {
fixes.unshift(ruleFixer.replaceText(node.arguments[0], stringifiedSubject));
Expand Down Expand Up @@ -223,7 +223,7 @@ async function applyFixes(fixes) {
['expect', 'assess'].includes(node.callee.object.callee.name) &&
node.callee.property.type === 'Identifier' &&
['toEqualSnapshot', 'toInspectAsSnapshot'].includes(
node.callee.property.name
node.callee.property.name,
) &&
node.callee.property.loc.start.line === lineNumber &&
node.callee.property.loc.start.column + 1 === columnNumber
Expand All @@ -235,7 +235,7 @@ async function applyFixes(fixes) {

if (appliedFixes && appliedFixes.length > 0) {
(fixesByFileName[fileName] = fixesByFileName[fileName] || []).push(
...appliedFixes
...appliedFixes,
);
}
},
Expand All @@ -246,7 +246,7 @@ async function applyFixes(fixes) {
for (const fileName of Object.keys(fixesByFileName)) {
const fixResult = SourceCodeFixer.applyFixes(
getSourceCodeMemoized(fileName).text,
fixesByFileName[fileName].map((fix) => ({ fix }))
fixesByFileName[fileName].map((fix) => ({ fix })),
);

if (fixResult.fixed) {
Expand Down
4 changes: 2 additions & 2 deletions lib/createInspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ const createInspector = (expect) => {
if (value.length > 32) {
return output.code(
`Buffer.from('${value.toString('base64')}', 'base64')`,
'javascript'
'javascript',
);
} else {
return this.baseType.inspect.call(
this,
value,
depth,
output,
inspect
inspect,
);
}
},
Expand Down
2 changes: 1 addition & 1 deletion lib/getSourceCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function getSourceCode(fileName) {
return `//${captured}`;
}),
{ parser: 'espree' },
fileName
fileName,
);

let ast;
Expand Down
2 changes: 1 addition & 1 deletion lib/isSimpleObjectTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function isSimpleObjectTree(obj) {
const result =
obj.constructor === Object &&
Object.getOwnPropertyNames(obj).every((name) =>
isSimple(obj[name])
isSimple(obj[name]),
) &&
Object.getOwnPropertySymbols(obj).length === 0;
seen.delete(obj);
Expand Down
2 changes: 1 addition & 1 deletion lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = function run(command, args, options) {
err,
bufferedLinesByStreamName.stdout,
bufferedLinesByStreamName.stderr,
])
]),
)
.on('exit', (exitCode) => {
resolve([
Expand Down
4 changes: 2 additions & 2 deletions lib/unexpected-snapshot-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = {
withSnapshotMessageAppended(expect, () => {
expect(subject, 'to equal', value);
});
}
},
);

expect.addAssertion('<any> to inspect as snapshot', (expect) => {
Expand All @@ -52,7 +52,7 @@ module.exports = {
withSnapshotMessageAppended(expect, () => {
expect(inspect(subject), 'to equal', value);
});
}
},
);
},
};
19 changes: 9 additions & 10 deletions lib/unexpected-snapshot-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ function ensureAfterHookIsRegistered() {
}
afterBlockRegistered = true;
after(async function () {
const { numFixedExpects, fixedSourceTextByFileName } = await applyFixes(
topLevelFixes
);
const { numFixedExpects, fixedSourceTextByFileName } =
await applyFixes(topLevelFixes);

const fileNames = Object.keys(fixedSourceTextByFileName);

Expand All @@ -30,7 +29,7 @@ function ensureAfterHookIsRegistered() {
numFixedExpects === 1 ? '' : 's'
} in ${fileNames.length} source file${
fileNames.length === 1 ? '' : 's'
}`
}`,
);

const Promise = require('bluebird');
Expand All @@ -41,13 +40,13 @@ function ensureAfterHookIsRegistered() {
fileNames,
(fileName) =>
fs.writeFileAsync(fileName, fixedSourceTextByFileName[fileName]),
{ concurrency: 5 }
{ concurrency: 5 },
);

console.log(
`unexpected-snapshot: Wrote ${results.length} file${
results.length === 1 ? '' : 's'
}`
}`,
);
}
// Replay the calls that were made while were writing files:
Expand Down Expand Up @@ -98,10 +97,10 @@ module.exports = {
ensureAfterHookIsRegistered();
} else {
throw new Error(
'unexpected-snapshot: Could not figure out the location of the expect() call to patch up'
'unexpected-snapshot: Could not figure out the location of the expect() call to patch up',
);
}
}
},
);

expect.addAssertion(
Expand Down Expand Up @@ -131,9 +130,9 @@ module.exports = {
} else {
throw err;
}
}
},
);
}
},
);

expect.unindent = require('@gustavnikolaj/string-utils').deindent;
Expand Down
8 changes: 4 additions & 4 deletions test/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('unexpected-snapshot', function () {
'-abc\n' +
'+def\n' +
'\n' +
'Rerun the tests with UNEXPECTED_SNAPSHOT=yes in Node to update the snapshots'
'Rerun the tests with UNEXPECTED_SNAPSHOT=yes in Node to update the snapshots',
);
});
});
Expand All @@ -34,7 +34,7 @@ describe('unexpected-snapshot', function () {
expect(
new Person('Eigil'),
'to inspect as snapshot',
"Person({ name: 'Eigil' })"
"Person({ name: 'Eigil' })",
);
});

Expand All @@ -44,13 +44,13 @@ describe('unexpected-snapshot', function () {
expect(
new Person('Eigil'),
'to equal snapshot',
"Person({ name: 'Preben' })"
"Person({ name: 'Preben' })",
);
},
'to throw',
"expected Person({ name: 'Eigil' }) to equal snapshot 'Person({ name: \\'Preben\\' })'\n" +
'\n' +
'Rerun the tests with UNEXPECTED_SNAPSHOT=yes in Node to update the snapshots'
'Rerun the tests with UNEXPECTED_SNAPSHOT=yes in Node to update the snapshots',
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/indentString.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('stringSnapshot', () => {
expect(
indentString('foo\n \nbar', 2),
'to equal',
'\n foo\n \n bar\n'
'\n foo\n \n bar\n',
);
});

Expand Down
Loading

0 comments on commit 7a23319

Please sign in to comment.