Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
#370 update no-http-string rule to read template strings
Browse files Browse the repository at this point in the history
closes #370
  • Loading branch information
HamletDRC committed May 12, 2017
1 parent 648f1fb commit f2faf05
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/noHttpStringRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,21 @@ export class Rule extends Lint.Rules.AbstractRule {

class NoHttpStringWalker extends ErrorTolerantWalker {
protected visitStringLiteral(node: ts.StringLiteral): void {
const stringText : string = (<ts.LiteralExpression>node).text;
this.visitLiteralExpression(node);
super.visitStringLiteral(node);
}

protected visitNode(node: ts.Node): void {
if (node.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral) {
this.visitLiteralExpression(<ts.NoSubstitutionTemplateLiteral>node);
} else if (node.kind === ts.SyntaxKind.TemplateHead) {
this.visitLiteralExpression(<ts.TemplateHead>node);
}
super.visitNode(node);
}

private visitLiteralExpression(node: ts.LiteralExpression | ts.LiteralLikeNode): void {
const stringText : string = node.text;
// tslint:disable no-http-string
if (stringText.indexOf('http:') === 0) {
if (!this.isSuppressed(stringText)) {
Expand All @@ -47,7 +61,6 @@ class NoHttpStringWalker extends ErrorTolerantWalker {
this.addFailure(failure);
}
}
super.visitStringLiteral(node);
}

private isSuppressed(stringText: string) : boolean {
Expand Down
24 changes: 24 additions & 0 deletions src/tests/NoHttpStringRuleTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ describe('noHttpStringRule', (): void => {
]);
});

it('should ban http template strings in variables', (): void => {
const inputScript: string = 'var x = `http://www.examples.com`';
TestHelper.assertViolations(ruleName, inputScript, [
{
"failure": "Forbidden http url in string: 'http://www.examples.com'",
"name": "file.ts",
"ruleName": ruleName,
"startPosition": {"character": 9, "line": 1}
}
]);
});

it('should ban http multipart template strings in variables', (): void => {
const inputScript: string = 'var x = `http://www.${example}.com`';
TestHelper.assertViolations(ruleName, inputScript, [
{
"failure": "Forbidden http url in string: 'http://www.'",
"name": "file.ts",
"ruleName": ruleName,
"startPosition": {"character": 9, "line": 1}
}
]);
});

it('should ban http strings in default values', (): void => {
const inputScript: string = 'function f(x : string = \'http://www.example.com/whatever\') {}';
TestHelper.assertViolations(ruleName, inputScript, [
Expand Down

0 comments on commit f2faf05

Please sign in to comment.