-
Notifications
You must be signed in to change notification settings - Fork 6.8k
build: tslint rule to enforce html tags escaping in comments #5825
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const ts = require('typescript'); | ||
const utils = require('tsutils'); | ||
const Lint = require('tslint'); | ||
|
||
const ERROR_MESSAGE = | ||
'A HTML tag may only appear if it is escaped. ' + | ||
'This is meant to prevent failures in docs generation caused by a misinterpreted tag.'; | ||
|
||
/** | ||
* Rule that walks through all comments inside of the library and adds failures when it | ||
* detects unescaped HTML tags inside of multi-line comments. | ||
*/ | ||
class Rule extends Lint.Rules.AbstractRule { | ||
|
||
apply(sourceFile) { | ||
return this.applyWithWalker(new NoUnescapedHtmlTagWalker(sourceFile, this.getOptions())); | ||
} | ||
} | ||
|
||
class NoUnescapedHtmlTagWalker extends Lint.RuleWalker { | ||
|
||
visitSourceFile(sourceFile) { | ||
utils.forEachComment(sourceFile, (fullText, commentRange) => { | ||
|
||
let isEscapedHtmlTag = true; | ||
while (true) { | ||
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 think this would be much easier with a regex. A simple one like this would be effective for when either character is unescaped,
You could certainly extend it to look for unmatched pairs, but I'm not sure that's even necessary. 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. When I closed this I was just thinking it could be better to just look for unmatched |
||
const iOpenTag = fullText.indexOf('<'); | ||
const iCloseTag = fullText.indexOf('>'); | ||
if ((iOpenTag === -1) && (iCloseTag === -1)) { | ||
break; | ||
} | ||
if ((iCloseTag < iOpenTag) || (iCloseTag === -1)) { | ||
isEscapedHtmlTag = false; | ||
break; | ||
} | ||
let iTestTag = fullText.indexOf('<', iOpenTag + 1); | ||
if ((iTestTag > iOpenTag) && (iTestTag < iCloseTag)) { | ||
isEscapedHtmlTag = false; | ||
break; | ||
} | ||
iTestTag = fullText.indexOf('`<'); | ||
if (iTestTag !== (iOpenTag - 1)) { | ||
isEscapedHtmlTag = false; | ||
break; | ||
} | ||
iTestTag = fullText.indexOf('>`') | ||
if (iTestTag !== iCloseTag) { | ||
isEscapedHtmlTag = false; | ||
break; | ||
} | ||
if ((iCloseTag + 2) > fullText.length) { | ||
break; | ||
} | ||
fullText = fullText.substring(iCloseTag + 2, fullText.length) | ||
} | ||
|
||
if (commentRange.kind === ts.SyntaxKind.MultiLineCommentTrivia && !isEscapedHtmlTag) { | ||
this.addFailureAt(commentRange.pos, commentRange.end - commentRange.pos, ERROR_MESSAGE); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
exports.Rule = Rule; | ||
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. Newline at end of file |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
"no-unused-expression": true, | ||
"no-var-keyword": true, | ||
"no-exposed-todo": true, | ||
"no-unescaped-html-tag": true, | ||
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. Unindent |
||
"no-debugger": true, | ||
"no-unused-variable": [true, {"ignore-pattern": "^_"}], | ||
"no-rxjs-patch-imports": [ | ||
|
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.
Maybe,