Skip to content
Closed
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
64 changes: 64 additions & 0 deletions tools/tslint-rules/noUnscapedHtmlTagRule.js
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.';
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe,

An HTML tag may only appear in a JSDoc comment if it is escaped. This prevents failures in document 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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,

/[^`][<>]/g

You could certainly extend it to look for unmatched pairs, but I'm not sure that's even necessary.

Copy link
Contributor Author

@julianobrasil julianobrasil Oct 31, 2017

Choose a reason for hiding this comment

The 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 ` pairs. In fact it was my initial thought. Than I think I forgot about it and started looking for the `< and >` matching pairs. I'll review and reopen it, probably tomorow.

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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Newline at end of file

1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"no-unused-expression": true,
"no-var-keyword": true,
"no-exposed-todo": true,
"no-unescaped-html-tag": true,
Copy link
Contributor

Choose a reason for hiding this comment

The 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": [
Expand Down