Code Transformation and Linting with ASTs Challenge 1: Solution
Transcript from the "Challenge 1: Solution" Lesson
>> Kent C. Dodds: Let's go ahead and write out our ESLint plugin. When you write a plugin for ESLint, you're generally spending as much time in AST Explorer as you can. And so, actually, I often won't even use any third party modules in my plugins until the very, very end because I can't use those in AST Explorer.
[00:00:22] So sometimes I'll even copy the module source and put it at the bottom of my plug-in in AST Explorer so that I can do most of my development here, cuz it's just that nice of an experience. But it's all trade-off, so here we're gonna find the offending nodes and this log is fine.
[00:00:42] But these consoles are not and the common thing across these is that they're identifiers with the name console. So we're going to use the identifier visitor and we'll accept a node here and we'll say if (node.name console I== 'console'), then we'll exit early, this node is fine. Otherwise we're gonna use context to report that our node has a problem, and now we're getting an undefined error message on all those.
[00:01:15] So, we'll say the message is 'Using console is not allowed'.
>> Kent C. Dodds: And so we're getting all of those error messages, which is great. So I'll copy that and paste it in here. We'll save and we get passing test, so we're good. And then we do this elaboration and feedback and change this to true.
>> Kent C. Dodds: And then we have all passing tests, which is great. Any questions?
>> Kent C. Dodds: Did anybody struggle with this? Was there any, what were the points that tripped you up, aside from typos?
>> Speaker 2: Which node type to use, or I use like call or express or something. I had to have conditions of if there was an object or something.
>> Kent C. Dodds: Yeah.
>> Speaker 2: I started at too high of a level. I didn't think of it as just using an identifier.
>> Kent C. Dodds: Yeah, yeah and eventually we're going to get to that because we don't want to warn on that. I mean, maybe that's a problem for us, but honestly we're not doing anything wrong there.
[00:02:26] Even this, it should be fine and so using something like a call expression ultimately would be a better solution. And we're gonna iterate and make this solution better. So yeah, knowing which node type to use can be a challenge. I'm glad that you brought that up, because generally you kind of want to start at a higher level node type just so that you have all of the context that you need.
[00:02:50] But the nice thing is, once you get your node you can go up and down the tree yourself and we will definitely be doing that. What were some of the key take-aways that you talked about in your elaboration? There hopefully was at least one. Yeah.
>> Speaker 3: Do you want us to do that for every exercise?
>> Kent C. Dodds: Yeah, we'll do one of these for every exercise. Yep. So any key takeaways from this exercise? Anything that you learned.
>> Speaker 4: Identifier as a node type.
>> Kent C. Dodds: Yeah, that's something to learn.
>> Speaker 4: [LAUGH]
>> Kent C. Dodds: Identifier's a node type, it's one that you'll get to know very much.