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
Update: Add consistent option to computed-property-spacing #12406
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -193,6 +193,16 @@ class SourceCode extends TokenStore { | |
Object.freeze(this.lines); | ||
} | ||
|
||
/** | ||
* Removes inline comments (`\/* *\/`) from specified text. | ||
* @param {string} text Text to process. | ||
* @returns {string} Text without inline comments. | ||
* @public | ||
*/ | ||
static removeInlineComments(text) { | ||
return text.replace(/\/\*.*?\*\//gu, ""); | ||
} | ||
|
||
/** | ||
* Split the source code into multiple lines based on the line delimiters. | ||
* @param {string} text Source code as a string. | ||
|
@@ -410,6 +420,28 @@ class SourceCode extends TokenStore { | |
return result; | ||
} | ||
|
||
/** | ||
* Returns text between two tokens. | ||
* @param {Token} first The token to get text after. | ||
* @param {Token} second The token to get text before. | ||
* @returns {string} Text between specified tokens. | ||
* @public | ||
*/ | ||
getTextBetweenTokens(first, second) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not opposed to adding this to our public API, but I think the other two added methods should be implemented in rules or, if shared, in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These methods may be useful for other rules too. |
||
return this.text.slice(first.range[1], second.range[0]); | ||
} | ||
|
||
/** | ||
* Returns text between two tokens without inline comments. | ||
* @param {Token} first The token to get text after. | ||
* @param {Token} second The token to get text before. | ||
* @returns {string} Text between specified tokens without inline comments. | ||
* @public | ||
*/ | ||
getCleanTextBetweenTokens(first, second) { | ||
return SourceCode.removeInlineComments(this.getTextBetweenTokens(first, second)); | ||
} | ||
|
||
/** | ||
* Determines if two tokens have at least one whitespace character | ||
* between them. This completely disregards comments in making the | ||
|
@@ -421,9 +453,7 @@ class SourceCode extends TokenStore { | |
* @public | ||
*/ | ||
isSpaceBetweenTokens(first, second) { | ||
const text = this.text.slice(first.range[1], second.range[0]); | ||
|
||
return /\s/u.test(text.replace(/\/\*.*?\*\//gu, "")); | ||
return /\s/u.test(this.getCleanTextBetweenTokens(first, second)); | ||
} | ||
|
||
/** | ||
|
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 don't think this it's a good idea to add this to the public API. This check only works when given the text between two adjacent tokens. If allowed any arbitrary text, a string containing this pattern could be mutated.
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.
As stated in name and comment this method is intended to remove inline comments i.e.
/* */
comments inside a line. So it is quite generic and can be helpful (at least in this module).But I can replace the method by private 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.
I understand. However, this would also mutate the following text:
becomes
I actually think the code this is being lifted from in
isSpaceBetweenTokens
is also buggy, and there is a PR open to fix it.