fix: Improve cleanPath platform compatability#249
fix: Improve cleanPath platform compatability#249hyperupcall wants to merge 2 commits intothlorenz:masterfrom
cleanPath platform compatability#249Conversation
doctoc.js
Outdated
|
|
||
| function cleanPath(path) { | ||
| var homeExpanded = (path.indexOf('~') === 0) ? process.env.HOME + path.substr(1) : path; | ||
| var homeExpanded = (path.indexOf('~') === 0) ? path.join(os.homedir() + path.substr(1)) : path; |
There was a problem hiding this comment.
While using os.homedir() indeed improves cross-platform compatibility, I noticed that the introduction of path.join() deviates slightly from the existing style. I'm unsure if this stylistic change was intended for this PR.
Additionally, if path.join() is used, it should accept multiple arguments (e.g., path.join(os.homedir(), path.substr(1))) rather than a single concatenated string.
Let me know your thoughts on this!
There was a problem hiding this comment.
Agreed! Embarrassingly, when I first tested these changes I must have forgotten to quote:
$ ./doctoc.js ~/Documents/...$ ./doctoc.js '~/Documents/...'I re-ran and it works as expected. I also fixed up the code to allow for using path.join by renaming the parameter in the function. path.join is also more cross platform.
| function cleanPath(path) { | ||
| var homeExpanded = (path.indexOf('~') === 0) ? process.env.HOME + path.substr(1) : path; | ||
| function cleanPath(filepath) { | ||
| var homeExpanded = (filepath.indexOf('~') === 0) ? path.join(os.homedir(), filepath.substr(1)) : filepath; |
There was a problem hiding this comment.
Is renaming path to filepath a mistake because it's unrelated to the topic?
There was a problem hiding this comment.
Nope it's intentional; I renamed path to filepath because the path parameter in the cleanPath function was shadowing the path variable defined at the top of the file (var path = require('path')). And this caused a bug, since path.join is a function exported from the path module - String.prototype.join() does not exist. A mistake in my testing prevented me from catching this earlier
As I understand both files and folders can be passed to the cleanPath function, maybe you prefer a different rename?
There was a problem hiding this comment.
Oh, okay, I thought it was about the rewrite of process.env.HOME only, will take a deeper look at it.
There was a problem hiding this comment.
Hi @hyperupcall,
I rechecked the change, and it looks like the naming change is helpful but out of the platform compatibility scope, so I'll respectfully suggest two solutions:
- Revert the naming change for now, and once this PR is merged, we can have another pull request to handle it.
- Keep them in this pull request, but rewrite the subject and description to properly and fully cover the naming issue.
It'll be great to ensure the commit history is clean and clear, not just to help debug and understand the code but also to make sure each change is atomic. So, I'll also encourage you to open an issue for the second change and revise its commit message to mention your thoughts. What do you think?
There was a problem hiding this comment.
Just a friendly example of how the second commit message might look like:
Rename
pathparameter to avoid shadowingpathmoduleThe
pathparameter in thecleanPathfunction was shadowing
thepathmodule imported at the top of the file. This caused
issues when callingpath.join(), as it was mistakenly treated
as a string method. Renaming the parameter tofilepathresolves
this issue and improves code clarity.
Or a shorter version could be:
Rename
pathto avoid conflict withpathmoduleRenaming the
pathparameter tofilepathprevents shadowing
the importedpathmodule and fixes related issues.
I hope this could be helpful 😄
There was a problem hiding this comment.
Copilot reviewed 1 out of 1 changed files in this pull request and generated no suggestions.
Comments skipped due to low confidence (1)
doctoc.js:13
- [nitpick] The parameter name 'filepath' should be consistent with the rest of the codebase. Ensure that 'filepath' is the appropriate name and is used consistently.
function cleanPath(filepath) {
Uses
os.homedir()since that works better on non-POSIXy platforms.I know this code uses old conventions and methods, so if NodeJS version compatability is a concern (even though they have been unsupported for decades), these are when these functions were added to NodeJS:
v2.3.0v0.1.16