10000 Infer via shebang by haggholm · Pull Request #5149 · prettier/prettier · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Extract getInterpreter() to module scope. Add another minor test.
  • Loading branch information
Petter Häggholm committed Oct 1, 2018
commit 096495dadd0ba439b2b975fed9caf1d08dd37b6c
105 changes: 45 additions & 60 deletions src/main/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,70 +116,55 @@ function getPlugin(options) {
return printerPlugin;
}

function inferParser(filepath, plugins) {
const filepathParts = normalizePath(filepath).split("/");
const filename = filepathParts[filepathParts.length - 1].toLowerCase();
function getInterpreter(filepath) {
if (typeof filepath !== "string") {
return "";
}

let interpreter = null;
const getInterpreter = () => {
if (interpreter === null) {
if (typeof filepath !== "string") {
interpreter = "";
return interpreter;
}
let fd;
try {
fd = fs.openSync(filepath, "r");
} catch (err) {
return "";
}

let fd;
try {
fd = fs.openSync(filepath, "r");
} catch (err) {
interpreter = "";
return interpreter;
}
try {
const liner = new readlines(fd);
const firstLine = liner
.next()
.toString("utf8")
.trim();

// #!/bin/env node, #!/usr/bin/env node
const m1 = /^#!\/usr(?:\/bin)?\/env\s+(\S+).*/.exec(firstLine);
if (m1) {
return m1[1];
}

try {
const liner = new readlines(fd);
const firstLine = liner
.next()
.toString("utf8")
.trim();

// #!/bin/env node
// #!/usr/bin/env node
const m1 = /^#!\/usr(?:\/bin)?\/env\s+(\S+).*/.exec(firstLine);
if (m1) {
interpreter = m1[1];
return interpreter;
}

// #!/bin/node
// #!/usr/bin/node
// #!/usr/local/bin/node
const m2 = /^#!\/usr(?:\/local)?(?:\/bin)?\/(\S+).*/.exec(firstLine);
if (m2) {
interpreter = m2[1];
return interpreter;
}

interpreter = "";
return interpreter;
} catch (err) {
// There are some weird cases where paths are missing, causing Jest
// failures. It's unclear what these correspond to in the real world.
interpreter = "";
return interpreter;
} 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 (err) {
// nop
}
}
// #!/bin/node, #!/usr/bin/node, #!/usr/local/bin/node
const m2 = /^#!\/usr(?:\/local)?(?:\/bin)?\/(\S+).*/.exec(firstLine);
if (m2) {
return m2[1];
}
return "";
} catch (err) {
// There are some weird cases where paths are missing, causing Jest
// failures. It's unclear what these correspond to in the real world.
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 (err) {
// nop
}
}
}

return interpreter;
};
function inferParser(filepath, plugins) {
const filepathParts = normalizePath(filepath).split("/");
const filename = filepathParts[filepathParts.length - 1].toLowerCase();

// If the file has no extension, we can try to infer the language from the
// interpreter in the shebang line, if any; but since this requires FS access,
Expand All @@ -195,7 +180,7 @@ function inferParser(filepath, plugins) {
language.filenames.find(name => name.toLowerCase() === filename)) ||
(filename.indexOf(".") === -1 &&
language.interpreters &&
language.interpreters.indexOf(getInterpreter()) !== -1))
language.interpreters.indexOf(getInterpreter(filepath)) !== -1))
);

return language && language.parsers[0];
Expand Down
13 changes: 11 additions & 2 deletions tests_integration/__tests__/file-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,18 @@ test("API getFileInfo with withNodeModules", () => {
});
});

describe("extracts file-info for a file with no extension but a standard shebang", () => {
describe("extracts file-info for a JS file with no extension but a standard shebang", () => {
expect(
prettier.getFileInfo.sync("tests_integration/cli/shebang/sample-bin")
prettier.getFileInfo.sync("tests_integration/cli/shebang/sample-bin1")
).toMatchObject({
ignored: false,
inferredParser: "babylon"
});
});

describe("extracts file-info for a JS file with no extension but an env-based shebang", () => {
expect(
prettier.getFileInfo.sync("tests_integration/cli/shebang/sample-bin2")
).toMatchObject({
ignored: false,
inferredParser: "babylon"
Expand Down
2 changes: 2 additions & 0 deletions tests_integration/cli/shebang/sample-bin1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/node
'use strict';
1 change: 1 addition & 0 deletions tests_integration/cli/shebang/sample-bin3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#!/usr/bin/php
0