Update helpers.expandTildePath to handle receiving an os object without homedir, address minor @ts-check confusion in same file.

This commit is contained in:
David Anson 2022-07-24 12:22:32 -07:00
parent 95466e29be
commit 5b0588f378
3 changed files with 6 additions and 5 deletions

View file

@ -1077,7 +1077,7 @@ module.exports.getNextChildToken = getNextChildToken;
* @returns {string} Absolute path (or original path). * @returns {string} Absolute path (or original path).
*/ */
function expandTildePath(file, os) { function expandTildePath(file, os) {
const homedir = os && os.homedir(); const homedir = os && os.homedir && os.homedir();
return homedir ? file.replace(/^~($|\/|\\)/, `${homedir}$1`) : file; return homedir ? file.replace(/^~($|\/|\\)/, `${homedir}$1`) : file;
} }
module.exports.expandTildePath = expandTildePath; module.exports.expandTildePath = expandTildePath;

View file

@ -1126,7 +1126,7 @@ module.exports.getNextChildToken = getNextChildToken;
* @returns {string} Absolute path (or original path). * @returns {string} Absolute path (or original path).
*/ */
function expandTildePath(file, os) { function expandTildePath(file, os) {
const homedir = os && os.homedir(); const homedir = os && os.homedir && os.homedir();
return homedir ? file.replace(/^~($|\/|\\)/, `${homedir}$1`) : file; return homedir ? file.replace(/^~($|\/|\\)/, `${homedir}$1`) : file;
} }
module.exports.expandTildePath = expandTildePath; module.exports.expandTildePath = expandTildePath;

View file

@ -471,7 +471,7 @@ test("applyFix", (t) => {
const [ line, fixInfo, lineEnding, expected ] = testCase; const [ line, fixInfo, lineEnding, expected ] = testCase;
// @ts-ignore // @ts-ignore
const actual = helpers.applyFix(line, fixInfo, lineEnding); const actual = helpers.applyFix(line, fixInfo, lineEnding);
t.is(actual, expected, "Incorrect fix applied."); t.is(actual, String(expected), "Incorrect fix applied.");
} }
}); });
@ -949,7 +949,7 @@ test("applyFixes", (t) => {
const [ input, errors, expected ] = testCase; const [ input, errors, expected ] = testCase;
// @ts-ignore // @ts-ignore
const actual = helpers.applyFixes(input, errors); const actual = helpers.applyFixes(input, errors);
t.is(actual, expected, "Incorrect fix applied."); t.is(actual, String(expected), "Incorrect fix applied.");
} }
}); });
@ -1277,9 +1277,10 @@ test("htmlElementRanges", (t) => {
}); });
test("expandTildePath", (t) => { test("expandTildePath", (t) => {
t.plan(16); t.plan(17);
const homedir = os.homedir(); const homedir = os.homedir();
t.is(helpers.expandTildePath("", os), ""); t.is(helpers.expandTildePath("", os), "");
t.is(helpers.expandTildePath("", {}), "");
t.is(helpers.expandTildePath("", null), ""); t.is(helpers.expandTildePath("", null), "");
t.is( t.is(
path.resolve(helpers.expandTildePath("~", os)), path.resolve(helpers.expandTildePath("~", os)),