-
-
Notifications
You must be signed in to change notification settings - Fork 24
feat(fs-access-mode-constants): introduce #152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(fs-access-mode-constants): introduce #152
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Already onto the second one 😀 Thanks for this!
#NamingIsHard I think this should be called fs-access-mode-constants (it affects more than only files).
d5b73e4 to
d30ad09
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not too bad. test file should be something like file-x.js
| if (originalText.includes(", }") || originalText.includes(", ") || originalText.includes(" , ")) { | ||
| const newText = originalText.replace(" , ", "").replace(",,", ",").replace(", ", ", ").replace(", }", " }"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to detect this leveraging ast-grep? (I think that would be more robust)
If not, I think you probably want a regex instead of hard strings. And also, I think you don't need to first test whether there is a match before trying to replace that match: you can feed string.replace a regex as the first arg; when there is no match, it will just do nothing (aka "fail gracefully").
| if (originalText.includes(", }") || originalText.includes(", ") || originalText.includes(" , ")) { | |
| const newText = originalText.replace(" , ", "").replace(",,", ",").replace(", ", ", ").replace(", }", " }"); | |
| const newText = originalText | |
| .replace(/\s+,+\s+/, ' , ') | |
| .replace(/\{\s*,/, '{ ') | |
| .replace(/,\s*\}/, ' }'); | |
| if (newText !== originalText) { |
You should test those regexes though (I think they're right, but I didn't confirm)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brunocroh do we have a utility for that ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we have an utility function utils/src/ast-grep/remove-binding.ts that can be used here
let hasChange = false
for (const key of ['F_OK', 'R_OK', 'W_OK', 'X_OK']) {
const change = removeBinding(importStatement, key); //it will return a ast-grep.Edit to remove import if it exists
if(change) {
edits.push(change.edit)
hasChange = true
}
}
if(hasChange) {
// add constants to import
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An easier way to handle destructured objects or named imports is
// get items inside of objectPattern
const imports = objectPattern.find({
rule: {
any: [
{kind: 'import_specifier'},
{kind: 'shorthand_property_identifier_pattern'}
]
}
})
//now imported items will be in `imports` variable as a string array, that will be easier to transform, i.g
// imports.push('newImport')
// items.filter // remove import
// items.map // update import
// after transform just join the array with ", "
const newImportString = `{ ${newImports.join(", ")} }`
// Replace the entire object pattern with new object pattern
objectPattern.replace(newImportString)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An easier way to handle destructured objects or named imports is
// get items inside of objectPattern const imports = objectPattern.find({ rule: { any: [ {kind: 'import_specifier'}, {kind: 'shorthand_property_identifier_pattern'} ] } }) //now imported items will be in `imports` variable as a string array, that will be easier to transform, i.g // imports.push('newImport') // items.filter // remove import // items.map // update import // after transform just join the array with ", " const newImportString = `{ ${newImports.join(", ")} }` // Replace the entire object pattern with new object pattern objectPattern.replace(newImportString)
I tried getting the import specifiers in an array via the ruleset provided above but going off of
const objectPattern = statement.find({
rule:
{ kind: 'object_pattern' },
});that didn't yield any results so I took a different approach
cc2fabf to
92725a2
Compare
|
incorporated feedback and adjusted coded accordingly my current approach allows me to handle every require and import statements with arrays containing the specifiers, and conditional replacements instead of removal is more streamlined imo |
That's fine, don't worry! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGMT with @brunocroh suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is very close!
P.S. Sorry for my lag (been very busy).
63205d6 to
1ea440a
Compare
1ea440a to
33bbd6a
Compare
|
@nekojanai please stop force-pushing 🙏 Doing that makes it very difficult to review changes over time; instead of being able to see the subset of new changes, the reviewer has look at everything again because the periodic diff is broken. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
w00t! I think this is good to go (nits can be addressed in a follow-up if appropriate).
| @@ -0,0 +1,6 @@ | |||
| const fs = require('node:fs'); | |||
|
|
|||
| const mode = fs.R_OK | fs.W_OK; | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this deliberately using a binary OR (|) instead of a logical OR (||)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const fs = require('node:fs'); | ||
|
|
||
| const mode = fs.R_OK | fs.W_OK; | ||
| if (condition) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is condition supposed to be mode? (or vice versa)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nekojanai could you answer about this though? (I know it doesn't especially matter, so fine to merge without the answer)
| let objPatArr = objectPattern.findAll({ | ||
| rule: | ||
| { kind: 'import_specifier' }, | ||
| }).map((v) => v.text()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the formatting makes this a bit difficult to grok
| let objPatArr = objectPattern.findAll({ | |
| rule: | |
| { kind: 'import_specifier' }, | |
| }).map((v) => v.text()); | |
| let objPatArr = objectPattern.findAll({ | |
| rule: { kind: 'import_specifier' }, | |
| }).map((v) => v.text()); |
| } | ||
| } | ||
|
|
||
| for (const _OK of ['F_OK', 'R_OK', 'W_OK', 'X_OK']) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: this array is declared and used multiple times. It could instead be declared as a const in the root scope and then referenced.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, Awesome work, thank you!
|
uhhh, LGTM? @brunocroh @JakobJingleheimer @AugustinMauroy |
|
Yes, it's approved. I thought you might want to press the green button, but I can do. |
closes #130