Skip to content

build: tslint rule to enforce html tags escaping in comments #8200

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 6 commits into from
Jan 26, 2018
Merged
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/noUnescapedHtmlTagRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';
import * as utils from 'tsutils';

const ERROR_MESSAGE =
'An HTML tag delimiter (< or >) 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.
*/
export class Rule extends Lint.Rules.AbstractRule {

apply(sourceFile: ts.SourceFile) {
return this.applyWithWalker(new NoUnescapedHtmlTagWalker(sourceFile, this.getOptions()));
}
}

class NoUnescapedHtmlTagWalker extends Lint.RuleWalker {

visitSourceFile(sourceFile: ts.SourceFile) {
utils.forEachComment(sourceFile, (fullText, commentRange) => {
const htmlIsEscaped =
this._parseForHtml(fullText.substring(commentRange.pos, commentRange.end));
if (commentRange.kind === ts.SyntaxKind.MultiLineCommentTrivia && !htmlIsEscaped) {
this.addFailureAt(commentRange.pos, commentRange.end - commentRange.pos, ERROR_MESSAGE);
}
});

super.visitSourceFile(sourceFile);
}

/** Gets whether the comment's HTML, if any, is properly escaped */
private _parseForHtml(fullText: string): boolean {
const matches = /[<>]/;
const backtickCount = fullText.split('`').length - 1;

// An odd number of backticks or html without backticks is invalid
if (backtickCount % 2 || (!backtickCount && matches.test(fullText))) {
return false;
}

// Text without html is valid
if (!matches.test(fullText)) {
return true;
}

// < and > must always be between two matching backticks.

// Whether an opening backtick has been found without a closing pair
let openBacktick = false;

for (const char of fullText) {
if (char === '`') {
openBacktick = !openBacktick;
} else if (matches.test(char) && !openBacktick) {
return false;
}
}

return true;
}
}
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
true,
"./tools/package-tools/rollup-globals.ts",
"src/+(lib|cdk|material-examples|material-experimental|cdk-experimental)/**/*.ts"
]
],
"no-unescaped-html-tag": true
}
}