Fix empty files with shebang (#8414)

This commit is contained in:
fisker Cheung authored and GitHub committed 2020-05-26 16:58:00 +03:00
1 parent 786eb2afb3
commit 408bbd4cbb
5 files changed
+63 -17

No files matched your search

+7 -4
View File
@@ -1,15 +1,18 @@
"use strict";
const { getShebang } = require("./util");
function includeShebang(text, ast) {
if (!text.startsWith("#!")) {
const shebang = getShebang(text);
if (!shebang) {
return;
}
const index = shebang.length;
const index = text.indexOf("\n");
const shebang = text.slice(2, index);
const comment = {
type: "Line",
value: shebang,
value: shebang.slice(2),
range: [0, index],
loc: {
start: {
+12
View File
@@ -844,6 +844,17 @@ function isFrontMatterNode(node) {
return node && node.type === "front-matter";
}
function getShebang(text) {
if (!text.startsWith("#!")) {
return "";
}
const index = text.indexOf("\n");
if (index === -1) {
return text;
}
return text.slice(0, index);
}
module.exports = {
replaceEndOfLineWith,
getStringWidth,
@@ -889,4 +900,5 @@ module.exports = {
addTrailingComment,
isWithinParentArrayProperty,
isFrontMatterNode,
getShebang,
};
+1 -12
View File
@@ -1,18 +1,7 @@
"use strict";
const docblock = require("jest-docblock");
// Logic from `/src/common/parser-include-shebang.js`
function getShebang(text) {
if (!text.startsWith("#!")) {
return "";
}
const index = text.indexOf("\n");
if (index === -1) {
return "";
}
return text.slice(0, index);
}
const { getShebang } = require("../common/util");
function hasPragma(text) {
const shebang = getShebang(text);
@@ -33,3 +33,30 @@ function a() {}
================================================================================
`;
exports[`snippet: Empty file with shebang format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
#!/usr/bin/env node
=====================================output=====================================
#!/usr/bin/env node
================================================================================
`;
exports[`snippet: Empty file with shebang format 2`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
#!/usr/bin/env node
=====================================output=====================================
#!/usr/bin/env node
================================================================================
`;
+16 -1
View File
@@ -1 +1,16 @@
run_spec(__dirname, ["babel", "flow", "typescript"]);
run_spec(
{
dirname: __dirname,
snippets: [
{
name: "Empty file with shebang",
code: "#!/usr/bin/env node",
},
{
name: "Empty file with shebang",
code: "#!/usr/bin/env node\n",
},
],
},
["babel", "flow", "typescript"]
);