forked from mysticatea/eslint-plugin-eslint-comments
-
-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add support for language plugins #257
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
de328c8
feat: add support for language plugins
ota-meshi 95bd95d
fix
ota-meshi 28cc291
add test and fix bugs
ota-meshi 95bb51b
add tests and fix bugs
ota-meshi 31506f2
add tests
ota-meshi 4fd4745
refactor
ota-meshi 2e9a12b
fix typo
ota-meshi 8a9ad95
refactor
ota-meshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
"use strict" | ||
|
||
const utils = require("./utils") | ||
|
||
/** | ||
* @typedef {object} DirectiveComment | ||
* @property {string} kind The kind of directive comment. | ||
* @property {string} [value] The directive value if it is `eslint-` comment. | ||
* @property {string} description The description of the directive comment. | ||
* @property {object} node The node of the directive comment. | ||
* @property {import("@eslint/core").SourceRange} range The range of the directive comment. | ||
* @property {import("@eslint/core").SourceLocation} loc The location of the directive comment. | ||
*/ | ||
|
||
const pool = new WeakMap() | ||
|
||
/** | ||
* @param {import('eslint').SourceCode} sourceCode - The source code to scan. | ||
* @returns {DirectiveComment[]} The directive comments. | ||
*/ | ||
function getAllDirectiveCommentsFromAllComments(sourceCode) { | ||
return sourceCode | ||
.getAllComments() | ||
.map((comment) => ({ | ||
comment, | ||
directiveComment: utils.parseDirectiveComment(comment), | ||
})) | ||
.filter(({ directiveComment }) => Boolean(directiveComment)) | ||
.map( | ||
({ comment, directiveComment }) => | ||
/** @type {DirectiveComment} */ ({ | ||
kind: directiveComment.kind, | ||
value: directiveComment.value, | ||
description: directiveComment.description, | ||
node: comment, | ||
range: comment.range, | ||
loc: comment.loc, | ||
}) | ||
) | ||
} | ||
|
||
/** | ||
* @param {import('@eslint/core').TextSourceCode} sourceCode - The source code to scan. | ||
* @returns {DirectiveComment[]} The directive comments. | ||
*/ | ||
function getAllDirectiveCommentsFromInlineConfigNodes(sourceCode) { | ||
const result = sourceCode.getDisableDirectives().directives.map( | ||
(directive) => | ||
/** @type {DirectiveComment} */ ({ | ||
kind: `eslint-${directive.type}`, | ||
value: directive.value, | ||
description: directive.justification, | ||
node: directive.node, | ||
range: sourceCode.getRange(directive.node), | ||
get loc() { | ||
return sourceCode.getLoc(directive.node) | ||
}, | ||
}) | ||
) | ||
|
||
return result.concat( | ||
sourceCode | ||
.getInlineConfigNodes() | ||
.map((node) => ({ | ||
node, | ||
range: sourceCode.getRange(node), | ||
})) | ||
.filter( | ||
({ range }) => | ||
// The node has intersection of the directive comment. | ||
// So, we need to skip it. | ||
!result.some( | ||
(comment) => | ||
comment.range[0] <= range[1] && | ||
range[0] <= comment.range[1] | ||
) | ||
) | ||
.map(({ node, range }) => { | ||
const nodeText = sourceCode.text.slice(range[0], range[1]) | ||
const commentValue = extractCommentContent(nodeText) | ||
const directiveComment = utils.parseDirectiveText(commentValue) | ||
|
||
return { | ||
directiveComment, | ||
node, | ||
range, | ||
} | ||
}) | ||
.filter( | ||
({ directiveComment }) => | ||
directiveComment != null && | ||
![ | ||
"eslint-disable", | ||
"eslint-disable-line", | ||
"eslint-disable-next-line", | ||
"eslint-enable", | ||
].includes(directiveComment.kind) | ||
) | ||
.map( | ||
({ directiveComment, node, range }) => | ||
/** @type {DirectiveComment} */ ({ | ||
kind: directiveComment.kind, | ||
value: directiveComment.value, | ||
description: directiveComment.description, | ||
node, | ||
range, | ||
get loc() { | ||
return sourceCode.getLoc(node) | ||
}, | ||
}) | ||
) | ||
) | ||
} | ||
|
||
function extractCommentContent(text) { | ||
// Extract comment content from the comment text. | ||
// The comment format was based on the language comment definition in vscode-eslint. | ||
// See https://github.com/microsoft/vscode-eslint/blob/c0e753713ea9935667e849d91e549adbff213e7e/server/src/languageDefaults.ts#L14 | ||
return text.startsWith("/*") && text.endsWith("*/") | ||
? text.slice(2, -2) | ||
: text.startsWith("//") | ||
? text.slice(2) | ||
: text.startsWith("<!--") && text.endsWith("-->") | ||
? text.slice(4, -3) | ||
: text.startsWith("###") && text.endsWith("###") | ||
? text.slice(3, -3) | ||
: text.startsWith("#") | ||
? text.slice(1) | ||
: text | ||
} | ||
|
||
module.exports = { | ||
/** | ||
* Get all directive comments for the given rule context. | ||
* | ||
* @param {import("@eslint/core").RuleContext} context - The rule context to get. | ||
* @returns {DirectiveComment[]} The all directive comments object for the rule context. | ||
*/ | ||
getAllDirectiveComments(context) { | ||
const sourceCode = context.sourceCode || context.getSourceCode() | ||
let result = pool.get(sourceCode.ast) | ||
|
||
if (result == null) { | ||
result = | ||
typeof sourceCode.getInlineConfigNodes === "function" && | ||
typeof sourceCode.getDisableDirectives === "function" | ||
? getAllDirectiveCommentsFromInlineConfigNodes(sourceCode) | ||
: getAllDirectiveCommentsFromAllComments(sourceCode) | ||
pool.set(sourceCode.ast, result) | ||
} | ||
|
||
return result | ||
}, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.