Exclude getInterpreter from 'standalone.js' (#12180)

This commit is contained in:
fisker Cheung authored and GitHub committed 2022-01-28 10:57:15 +08:00
1 parent 350c05330e
commit a407f9b5af
2 files changed
+60 -49

No files matched your search

+8 -49
View File
@@ -1,8 +1,6 @@
"use strict";
const fs = require("fs");
const path = require("path");
const readlines = require("n-readlines");
const { UndefinedParserError } = require("../common/errors.js");
const { getSupportInfo } = require("../main/support.js");
const normalizer = require("./options-normalizer.js");
@@ -118,52 +116,6 @@ function getPlugin(options) {
return printerPlugin;
}
function getInterpreter(filepath) {
/* istanbul ignore next */
if (typeof filepath !== "string") {
return "";
}
let fd;
try {
fd = fs.openSync(filepath, "r");
} catch {
// istanbul ignore next
return "";
}
try {
const liner = new readlines(fd);
const firstLine = liner.next().toString("utf8");
// #!/bin/env node, #!/usr/bin/env node
const m1 = firstLine.match(/^#!\/(?:usr\/)?bin\/env\s+(\S+)/);
if (m1) {
return m1[1];
}
// #!/bin/node, #!/usr/bin/node, #!/usr/local/bin/node
const m2 = firstLine.match(/^#!\/(?:usr\/(?:local\/)?)?bin\/(\S+)/);
if (m2) {
return m2[1];
}
return "";
} catch {
// There are some weird cases where paths are missing, causing Jest
// failures. It's unclear what these correspond to in the real world.
/* istanbul ignore next */
return "";
} finally {
try {
// There are some weird cases where paths are missing, causing Jest
// failures. It's unclear what these correspond to in the real world.
fs.closeSync(fd);
} catch {
// nop
}
}
}
function inferParser(filepath, plugins) {
const filename = path.basename(filepath).toLowerCase();
const languages = getSupportInfo({ plugins }).languages.filter(
@@ -183,7 +135,14 @@ function inferParser(filepath, plugins) {
language.filenames.some((name) => name.toLowerCase() === filename))
);
if (!language && !filename.includes(".")) {
if (
process.env.PRETTIER_TARGET !== "universal" &&
!language &&
!filename.includes(".")
) {
// `getInterpreter` requires file access, put `require()` in the `if` block,
// So we can easily remove this part during build
const getInterpreter = require("../utils/get-interpreter.js");
const interpreter = getInterpreter(filepath);
language = languages.find(
(language) =>
+52
View File
@@ -0,0 +1,52 @@
"use strict";
const fs = require("fs");
const readlines = require("n-readlines");
function getInterpreter(filepath) {
/* istanbul ignore next */
if (typeof filepath !== "string") {
return "";
}
let fd;
try {
fd = fs.openSync(filepath, "r");
} catch {
// istanbul ignore next
return "";
}
try {
const liner = new readlines(fd);
const firstLine = liner.next().toString("utf8");
// #!/bin/env node, #!/usr/bin/env node
const m1 = firstLine.match(/^#!\/(?:usr\/)?bin\/env\s+(\S+)/);
if (m1) {
return m1[1];
}
// #!/bin/node, #!/usr/bin/node, #!/usr/local/bin/node
const m2 = firstLine.match(/^#!\/(?:usr\/(?:local\/)?)?bin\/(\S+)/);
if (m2) {
return m2[1];
}
return "";
} catch {
// There are some weird cases where paths are missing, causing Jest
// failures. It's unclear what these correspond to in the real world.
/* istanbul ignore next */
return "";
} finally {
try {
// There are some weird cases where paths are missing, causing Jest
// failures. It's unclear what these correspond to in the real world.
fs.closeSync(fd);
} catch {
// nop
}
}
}
module.exports = getInterpreter;