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

feat: Add support for config comments #27

Merged
merged 7 commits into from
Oct 2, 2024
Merged

feat: Add support for config comments #27

merged 7 commits into from
Oct 2, 2024

Conversation

mdjermanovic
Copy link
Member

Prerequisites checklist

What is the purpose of this pull request?

Adds support for rule configuration comments and disable directives.

What changes did you make? (Give an overview)

Added getInlineConfigNodes(), getDisableDirectives() and applyInlineConfig() methods to the JSONSourceCode class.

Related Issues

Fixes #21

Is there anything you'd like reviewers to focus on?

Unlike the JS language implementation (SourceCode in eslint/eslint), in this implementation rule configs, eslint-disable and eslint-enable can be Line comments. We wanted to implemented this in the JS language as well (eslint/rfcs#34), and eventually did but reverted the change due to backwards compatibility reasons (eslint/eslint#14960). Since there were no backwards compatibility concerns here, it seemed fine to allow this, but let me know if it would be better to have the same behavior as in the JS language (e.g., ignore comments like // eslint rule).

Comment on lines 233 to 236
} else if (parseResult.ok === false) {
problems.push({
ruleId: null,
message: parseResult.error.message,
Copy link
Member Author

Choose a reason for hiding this comment

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

I inserted if (parseResult.ok === false) here because I was getting this TypeScript error without it (with just else):

error TS2339: Property 'error' does not exist on type '{ ok: true; config: RulesConfig; } | { ok: false; error: { message: string; }; }'.
  Property 'error' does not exist on type '{ ok: true; config: RulesConfig; }'.

229       message: parseResult.error.message,

It seems that TypeScript doesn't infer that in else of if (parseResult.ok), parseResult must be { ok: false; error: { message: string; }; }. Please let me know if there's a better solution.

Copy link
Member

Choose a reason for hiding this comment

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

I think this is happening because we don't use the strictNullChecks option in tsconfig.json. Without that option, TypeScript assumes that the ok field in parseResult could be possibly nullish in both alternatives of the union type, so it cannot infer that the error property exists from the fact that ok is falsy.

You could add a JSDoc annotation like /** @type {*} */ above the line where parseResult is declared to disable type checking in this case, but maybe there's an even better solution.

Copy link
Member

Choose a reason for hiding this comment

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

Another way to do this is with a type guard, something like:

/**
 * @param {ParseResult} result
 * @returns {result is OkParseResult}
 */
function isOkParseResult(result) {
    return result.ok;
}

Then you can use isOkParseResult() and TypeScript can know that you're positively identifying the type.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, I fixed this with casting.

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

LGTM. Would like @fasttime to approve before merging.

Copy link
Member

@fasttime fasttime left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@fasttime fasttime merged commit 723ddf4 into main Oct 2, 2024
15 checks passed
@fasttime fasttime deleted the issue21 branch October 2, 2024 05:14
@github-actions github-actions bot mentioned this pull request Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

Change Request: Add support for config comments
3 participants