JS: DRY hasPragma & insertPragma (#9002)
This commit is contained in:
1 parent
dc46155962
commit
7bf1803a11
5 files changed
+75
-20
No files matched your search
+25
-20
@@ -1,36 +1,41 @@
|
||||
"use strict";
|
||||
|
||||
const docblock = require("jest-docblock");
|
||||
const { parseWithComments, strip, extract, print } = require("jest-docblock");
|
||||
const { getShebang } = require("../common/util");
|
||||
|
||||
function hasPragma(text) {
|
||||
function parseDocBlock(text) {
|
||||
const shebang = getShebang(text);
|
||||
if (shebang) {
|
||||
text = text.slice(shebang.length + 1);
|
||||
}
|
||||
const pragmas = Object.keys(docblock.parse(docblock.extract(text)));
|
||||
|
||||
const docBlock = extract(text);
|
||||
const { pragmas, comments } = parseWithComments(docBlock);
|
||||
|
||||
return { shebang, text, pragmas, comments };
|
||||
}
|
||||
|
||||
function hasPragma(text) {
|
||||
const pragmas = Object.keys(parseDocBlock(text).pragmas);
|
||||
return pragmas.includes("prettier") || pragmas.includes("format");
|
||||
}
|
||||
|
||||
function insertPragma(text) {
|
||||
const shebang = getShebang(text);
|
||||
if (shebang) {
|
||||
text = text.slice(shebang.length + 1);
|
||||
}
|
||||
const parsedDocblock = docblock.parseWithComments(docblock.extract(text));
|
||||
const pragmas = { format: "", ...parsedDocblock.pragmas };
|
||||
const newDocblock = docblock
|
||||
.print({
|
||||
pragmas,
|
||||
comments: parsedDocblock.comments.replace(/^(\s+?\r?\n)+/, ""), // remove leading newlines
|
||||
})
|
||||
.replace(/(\r\n|\r)/g, "\n"); // normalise newlines (mitigate use of os.EOL by jest-docblock)
|
||||
const strippedText = docblock.strip(text);
|
||||
const separatingNewlines = strippedText.startsWith("\n") ? "\n" : "\n\n";
|
||||
function insertPragma(originalText) {
|
||||
const { shebang, text, pragmas, comments } = parseDocBlock(originalText);
|
||||
const strippedText = strip(text);
|
||||
|
||||
const docBlock = print({
|
||||
pragmas: {
|
||||
format: "",
|
||||
...pragmas,
|
||||
},
|
||||
comments: comments.trimStart(),
|
||||
}).replace(/\r\n?/g, "\n"); // normalise newlines (mitigate use of os.EOL by jest-docblock)
|
||||
|
||||
return (
|
||||
(shebang ? `${shebang}\n` : "") +
|
||||
newDocblock +
|
||||
separatingNewlines +
|
||||
docBlock +
|
||||
(strippedText.startsWith("\n") ? "\n" : "\n\n") +
|
||||
strippedText
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,19 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`empty.js - {"insertPragma":true} format 1`] = `
|
||||
====================================options=====================================
|
||||
insertPragma: true
|
||||
parsers: ["flow", "babel", "typescript"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
|
||||
=====================================output=====================================
|
||||
/** @format */
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`module-with-late-docblock.js - {"insertPragma":true} format 1`] = `
|
||||
====================================options=====================================
|
||||
insertPragma: true
|
||||
@@ -139,6 +153,39 @@ function foo(bar) {
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`shebang-only-with-pragma.js - {"insertPragma":true} format 1`] = `
|
||||
====================================options=====================================
|
||||
insertPragma: true
|
||||
parsers: ["flow", "babel", "typescript"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
#!/usr/bin/env node
|
||||
/** @format */
|
||||
|
||||
=====================================output=====================================
|
||||
#!/usr/bin/env node
|
||||
/** @format */
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`shebang-only-without-pragma.js - {"insertPragma":true} format 1`] = `
|
||||
====================================options=====================================
|
||||
insertPragma: true
|
||||
parsers: ["flow", "babel", "typescript"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
#!/usr/bin/env node
|
||||
|
||||
=====================================output=====================================
|
||||
#!/usr/bin/env node
|
||||
/** @format */
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`shebang-with-pragma.js - {"insertPragma":true} format 1`] = `
|
||||
====================================options=====================================
|
||||
insertPragma: true
|
||||
|
||||
Whitespace-only changes.
@@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env node
|
||||
/** @format */
|
||||
@@ -0,0 +1 @@
|
||||
#!/usr/bin/env node
|
||||
Reference in new issue
Block a user