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
New: add a defineParser function #9321
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.
Left one question, thanks for contributing!
lib/parsers.js
Outdated
const parser = this._parsers[parserId]; | ||
|
||
if (!parser) { | ||
return require(parserId); |
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.
Should require(parserId)
be cached into the parser map in this case?
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.
Hmm, node already caches requires by default (https://nodejs.org/api/modules.html#modules_require_cache), but I could add it for explicitness.
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.
Thanks for the PR! I left a few suggestions.
docs/developer-guide/nodejs-api.md
Outdated
@@ -241,6 +241,26 @@ Map { | |||
*/ | |||
``` | |||
|
|||
### Linter#defineParser | |||
|
|||
Each instance of `Linter` holds a map of custom parsers. If you want to define a parser programmaticaly you can add this function |
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.
The word "programmatically" is spelled incorrectly here.
docs/developer-guide/nodejs-api.md
Outdated
} | ||
}); | ||
|
||
const results = linter.verify("// some source text", { parser: 'my-custom-parser' }); |
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.
Nitpick: For consistency, can you use double quotes for "my-custom-parser"
?
lib/parsers.js
Outdated
// Public Interface | ||
//------------------------------------------------------------------------------ | ||
|
||
class Parsers { |
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 having a separate module for this isn't necessary given that a regular Map
basically does the same thing (aside from using require
). Instead, I think this could just be included as part of the defineParser
method. However, I don't really have a strong objection to doing it like this.
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.
Agreed. This feels like a pretty generic store - I think it would actually be clearer to use a Map
as well.
lib/linter.js
Outdated
@@ -727,6 +709,7 @@ module.exports = class Linter { | |||
this.version = pkg.version; | |||
|
|||
this.rules = new Rules(); | |||
this.parsers = new Parsers(); |
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.
Can you prefix this property name with an underscore? (this._parsers
)
* @param {any} parserModule The parser object | ||
* @returns {void} | ||
*/ | ||
defineParser(parserId, parserModule) { |
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.
Can you add tests for defineParser
?
LGTM |
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.
This looks good to me aside from a few documentation nitpicks. Thanks!
docs/developer-guide/nodejs-api.md
Outdated
### Linter#defineParser | ||
|
||
Each instance of `Linter` holds a map of custom parsers. If you want to define a parser programmatically you can add this function | ||
with the name of the parser as first argument and the parser object (having either `parseForESLint` or `parse` as keys) as second argument. |
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 it would be good to link to the parser documentation here. For example:
...with the name of the parser as the first argument and the [parser object](/docs/developer-guide/working-with-plugins#working-with-custom-parsers) as the second argument.
docs/developer-guide/nodejs-api.md
Outdated
Each instance of `Linter` holds a map of custom parsers. If you want to define a parser programmatically you can add this function | ||
with the name of the parser as first argument and the parser object (having either `parseForESLint` or `parse` as keys) as second argument. | ||
|
||
If during linting the parser is not found it will fallback to `require(parserId)`. |
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.
Nitpick: I think a comma should be added after the word "found".
LGTM |
Good feedback! I committed the suggested changes. |
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.
Thanks for working on this!
lib/parsers.js
Outdated
// Public Interface | ||
//------------------------------------------------------------------------------ | ||
|
||
class Parsers { |
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.
Agreed. This feels like a pretty generic store - I think it would actually be clearer to use a Map
as well.
@CompuIves Friendly ping. |
Sorry for the delayed reply, I got caught up in some work. I just pushed a commit that moves the |
@CompuIves great work on this! Would be awesome to merge this and get a release going since it's been some time and having this as a git dependency will collide with other eslint dep in some cases. |
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, thanks!
Thanks for contributing! |
What is the purpose of this pull request? (put an "X" next to item)
[x] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[x] Add something to the core
[ ] Other, please explain:
What changes did you make? (Give an overview)
I added an
defineParser
function to the Node API, which allows you to define a custom parser thateslint
will use when defined in the config.Is there anything you'd like reviewers to focus on?
I'm not sure if the implementation is desired, returning the error object for errors doesn't feel right. I'm also not sure if I added enough tests.