Skip to content
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

make -k other_keyword append to the defaults #82

Closed
wants to merge 4 commits into from

Conversation

jsut
Copy link

@jsut jsut commented Mar 26, 2021

  • This is how xgettext works, which is how the docs say it works.

Copy link
Collaborator

@smhg smhg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, this is a great idea. I've left some individual line comments.
On top of that, I think 2 things are still missing:

  • xgettext allows to reset the default keywords by passing -k (without a word). It would be a very good idea to add this behavior in this PR since this provides the easiest upgrade path from the old usage. Otherwise things will break much more for existing users.
  • It would be great to have tests for the actual differences in keywords passed to the parsers. Now we only test the resulting output. This would require to expose (a part of) the current getParser function somehow. I don't know what the best approach is; feedback on this is very much appreciated.

index.js Outdated
@@ -91,10 +91,9 @@ function xgettext (input, options, cb) {
if (!parsers[name]) {
const Parser = require(`gettext-${name}`);

if (Object.keys(keywordSpec).length > 0) {
if (Parser.keywordSpec) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the parser does not offer a default, no keywordSpec is passed to the parser.

index.js Outdated
@@ -91,10 +91,9 @@ function xgettext (input, options, cb) {
if (!parsers[name]) {
const Parser = require(`gettext-${name}`);

if (Object.keys(keywordSpec).length > 0) {
if (Parser.keywordSpec) {
keywordSpec = Object.assign(keywordSpec, Parser.keywordSpec);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parameter order of Object.assign always overwrites all custom keywords with their defaults.

{{$ "regex escaped keyword"}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the change here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vim being particular about whether their should be a newline at the end of the file. I'll fix it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange, as I don't see a newline at the end of the file in master either. Can it be a different line ending (should be Unix-style)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there was no newline before.

@jsut
Copy link
Author

jsut commented Mar 27, 2021

* `xgettext` allows to reset the default keywords by passing `-k` (without a word). It would be a very good idea to add this behavior in this PR since this provides the easiest upgrade path from the old usage. Otherwise things will break much more for existing users.

I haven't looked at the code for this yet, but it seems like another PR to me. There are other issues with -k as well. With gnu xgettext there doesn't have to be (and perhaps cannot be, i haven't tested it) a space after the -k before the identifier. I think another PR that adds this functionality and adds tests for createKeywordSpec could be the right thing to do.

* It would be great to have tests for the actual differences in keywords passed to the parsers. Now we only test the resulting output. This would require to expose (a part of) the current `getParser` function somehow. I don't know what the best approach is; feedback on this is very much appreciated.

Perhaps add more functionality to keyword-spec.js, a function that will take a bunch of specs and combine them. Then you could test that function directly, and the code currently in this PR gets even simpler.

@smhg
Copy link
Collaborator

smhg commented Mar 27, 2021

I haven't looked at the code for this yet, but it seems like another PR to me. There are other issues with -k as well. With gnu xgettext there doesn't have to be (and perhaps cannot be, i haven't tested it) a space after the -k before the identifier. I think another PR that adds this functionality and adds tests for createKeywordSpec could be the right thing to do.

I prefer this in the same PR, but separate is fine too. It's just that this functionality is crucial for this PR to move ahead.
Someone who would install a version with the current change and uses -k somekeyword would suddenly also get all default keywords and quite realistically a much different output. There would be no migration path. When you can disable the defaults with -k, you'd only need to change this to -k -k somekeyword. The same way xgettext handles this.

You don't need to worry about the exact way to specify parameters as that's something that is handled by the optstring parser (yargs). We stick to their format, not xgettext's, in that area.

Perhaps add more functionality to keyword-spec.js, a function that will take a bunch of specs and combine them. Then you could test that function directly, and the code currently in this PR gets even simpler.

Yes, this would be great to have. That can definitely be in a separate PR of course.

@smhg
Copy link
Collaborator

smhg commented Mar 28, 2021

@jsut please also run npm run lint during or after development (or make your editor run eslint). Please make sure that passes.

@jsut
Copy link
Author

jsut commented Mar 28, 2021

@jsut please also run npm run lint during or after development (or make your editor run eslint). Please make sure that passes.

done

@jsut
Copy link
Author

jsut commented Apr 1, 2021

@smhg do you need anything else here?

@smhg
Copy link
Collaborator

smhg commented Apr 8, 2021

@jsut there are some minor changes in master. Could you merge them into your branch?

I'll check things in the next few days and release this.

- adding -k with no value will tell the parser not to use the default keywords
- adding -k with a value will add a keyword to the list of keywords
- add tests
@jsut
Copy link
Author

jsut commented Apr 8, 2021

@smhg it's rebased

@smhg
Copy link
Collaborator

smhg commented Apr 8, 2021

@jsut thanks!
Running ./bin/xgettext-template with only -k (and no new keywords) throws an error.
Can you add a test for this and handle this scenario?

@jsut
Copy link
Author

jsut commented Apr 8, 2021

@smhg yup, maybe this afternoon.

@smhg
Copy link
Collaborator

smhg commented Apr 17, 2021

Thank you for your ongoing efforts @jsut! I've had some time to look at the actual changes to the code.

  • The implementation you proposed to enable this new behavior is not great. I understand why you chose this route. It does work. It however makes to code harder to interpret. For instance: adding a property to the keywordSpec to later delete that property again feels like a hack. Also, the -k case (reset) should probably not be passed on to createKeywordSpec but 'detected' early on.
  • The tests you added are great.
  • Don't force push changes. Just add new commits and in the end everything will be squashed into one commit into master. It is not important in how many steps you create this.

Would you be open to further improve the PR? I'm happy to add comments to the code if you're open to this.
If you don't, feel free to only keep the changes to the tests and fixtures. We can definitely use those.

@jsut
Copy link
Author

jsut commented Apr 17, 2021

If you have a cleaner way to implement it @smhg let me know. I just tried to make it work despite it seeming like a pretty pointless thing to implement since running the thing with only a -k will never result in any output other than an empty po file anyway.

I dug into how yargs works to try to find a cleaner way to handle this, but it seemed like a dead end.

- has 2 keys, noDefaults and spec.  spec is the old return value, noDefault is a boolean that
  indicates if they bare keyword was present indicating that the default keywords should not
  be applied
@jsut
Copy link
Author

jsut commented Apr 17, 2021

@smhg this seems cleaner, not loving all the spec.spec, particularly in the tests, but yeah...

smhg added a commit that referenced this pull request Apr 18, 2021
@smhg
Copy link
Collaborator

smhg commented Apr 18, 2021

@jsut thank you for your efforts. I don't think this is moving in the right direction. I'd love to offer more timely feedback, but I have to acknowledge I don't have the time for that kind of followup currently.

In general:

  • xgettext handles --keyword and --keyword="" as ways to disable the defaults. Currently the first one would work, but only in the right order (when another parameter follows). That's a hard problem as far as I can see, but we can't move ahead that way. I think an acceptable workaround would be to only support --keyword="" for now.
  • The way you implement the new feature is rather strange to me. I think it is better to 'filter' out a possible reset early on and not pass that to createKeywordSpec. So it does not need to handle that and so there is no need to return a wrapper object. I might be wrong about this.

@smhg
Copy link
Collaborator

smhg commented Apr 18, 2021

I've added an alternative solution to the v5 branch based on your tests.
This does not handle the -k or --keyword (boolean) use however. That would still need to be added for a v5 release.

@jsut
Copy link
Author

jsut commented Apr 18, 2021

Given your branched changes, i'm not sure if i should be doing anything else here or not.

* xgettext handles `--keyword` and `--keyword=""` as ways to disable the defaults. Currently the first one would work, but **only in the right order** (when another parameter follows). That's a hard problem as far as I can see, but we can't move ahead that way. I think an acceptable workaround would be to only support `--keyword=""` for now.

I'm not sure if you're talking about gnu xgettext here, or this package. In any case, in this branch, both of them support -k/--keyword, in any position in the parameter list on the CLI, with the one caveat that in xgettext-template, if you do -k , or --keyword then yargs will slurp up the filename as the 'keyword'. This is a yargs problem, and i think it's actually impossible to fix without a significant change to yargs, or some serious massaging of what you pass into yargs. Neither of which seem reasonable to me. What does seem reasonable though, is documenting this shortcoming of -k/--keyword. It all goes back to my original complaint about the space after -k. Maybe yargs does handle this, but i'm not that familiar with it and my dive into the docs was not fruitful.

* The way you implement the new feature is rather strange to me. I think it is better to 'filter' out a possible reset early on and _not_ pass that to `createKeywordSpec`. So it does not need to handle that and so there is no need to return a wrapper object. I might be wrong about this.

This seems like a reasonable improvement to me. Do you think that the code that does this filtering should live in index.js, or should keyword-spec.js be expanded to have more exports, and one of them is the function that does this?

@jsut jsut closed this Mar 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants