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

Add a rule to require the @logger decorator #4

Merged
merged 1 commit into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,46 @@
module.exports.rules = {
"require-logger": {
meta: {
messages: {
loggerMissing:
"Each component's render method must be decorated with the @logger decorator."
},
schema: []
},
create: context => ({
MethodDefinition(node) {
const ancestors = context.getAncestors();
const isComponent = ancestors.some(a => {
const isClass =
a.type === "ExportNamedDeclaration" &&
a.declaration.type === "ClassDeclaration";

if (isClass) {
const decorators = a.declaration.decorators || [];
return decorators.some(
d => d.expression.callee.name === "Component"
);
}

return false;
});

if (isComponent && node.key.name === "render") {
const decorators = node.decorators || [];
const logger = decorators.find(
d => d.expression.callee.name === "logger"
);

if (!logger) {
context.report({
node,
messageId: "loggerMissing"
});
}
}
}
})
},
"restrict-required-props": {
meta: {
messages: {
Expand Down
51 changes: 51 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,54 @@ ruleTester.run("component-prefix", plugin.rules["component-prefix"], {
}
]
});

const isNotAComponent = `
export class ManifoldSelect {
@Prop() answer: number;

render() {
return null;
}
}`;

const componentMissingLogger = `
import { h, Component, Prop } from '@stencil/core';
Copy link

Choose a reason for hiding this comment

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

So this doesn’t get evaluated by JS, because @stencil/core isn’t a dependency? The ESLint parser supports decorators and stuff out-of-the-box?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, there's no evaluation here, only parsing the text into nodes. And yes, decorators are supported out of the box!


@Component({ tag: 'quux-component' })
export class ManifoldSelect {
@Prop() answer: number = 42;

render() {
return null;
}
}`;

const componentWithLogger = `
import { h, Component, Prop } from '@stencil/core';

@Component({ tag: 'quux-component' })
export class ManifoldSelect {
@Prop() answer: number = 42;

@logger()
render() {
return null;
}
}`;

ruleTester.run("require-logger", plugin.rules["require-logger"], {
valid: [
{
code: isNotAComponent
},
{
code: componentWithLogger
}
],
invalid: [
{
code: componentMissingLogger,
errors: [{ messageId: "loggerMissing" }]
}
]
Copy link

Choose a reason for hiding this comment

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

This is so great—testing valid and invalid examples. 💯

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah! In fact you have to include both valid and invalid examples, otherwise the test will throw an error.

});