forked from microsoft/tslint-microsoft-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement microsoft#95: new rule: no-unexternalized-strings
- Loading branch information
Showing
4 changed files
with
305 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// Place your settings in this file to overwrite default and user settings. | ||
{ | ||
"editor.insertSpaces": true, | ||
"editor.tabSize": 4 | ||
"editor.tabSize": 4, | ||
"files.trimTrailingWhitespace": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import * as ts from 'typescript'; | ||
import * as Lint from 'tslint/lib/lint'; | ||
|
||
import ErrorTolerantWalker = require('./utils/ErrorTolerantWalker'); | ||
import SyntaxKind = require('./utils/SyntaxKind'); | ||
|
||
/** | ||
* Implementation of the no-unexternalized-strings rule. | ||
*/ | ||
export class Rule extends Lint.Rules.AbstractRule { | ||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker(new NoUnexternalizedStringsRuleWalker(sourceFile, this.getOptions())); | ||
} | ||
} | ||
|
||
interface Map<V> { | ||
[key: string]: V; | ||
} | ||
|
||
interface UnexternalizedStringsOptions { | ||
signatures?: string[]; | ||
messageIndex?: number; | ||
ignores?: string[]; | ||
} | ||
|
||
class NoUnexternalizedStringsRuleWalker extends ErrorTolerantWalker { | ||
|
||
private static SINGLE_QUOTE: string = '\''; | ||
private static DOUBLE_QUOTE: string = '"'; | ||
|
||
private signatures: Map<boolean>; | ||
private messageIndex: number; | ||
private ignores: Map<boolean>; | ||
|
||
constructor(sourceFile: ts.SourceFile, opt: Lint.IOptions) { | ||
super(sourceFile, opt); | ||
this.signatures = Object.create(null); | ||
this.ignores = Object.create(null); | ||
this.messageIndex = undefined; | ||
let options: any[] = this.getOptions(); | ||
let first: UnexternalizedStringsOptions = options && options.length > 0 ? options[0] : null; | ||
if (first) { | ||
if (Array.isArray(first.signatures)) { | ||
first.signatures.forEach((signature: string) => this.signatures[signature] = true); | ||
} | ||
if (Array.isArray(first.ignores)) { | ||
first.ignores.forEach((ignore: string) => this.ignores[ignore] = true); | ||
} | ||
if (typeof first.messageIndex !== 'undefined') { | ||
this.messageIndex = first.messageIndex; | ||
} | ||
} | ||
} | ||
|
||
|
||
protected visitStringLiteral(node: ts.StringLiteral): void { | ||
this.checkStringLiteral(node); | ||
super.visitStringLiteral(node); | ||
} | ||
|
||
private checkStringLiteral(node: ts.StringLiteral): void { | ||
let text = node.getText(); | ||
// The string literal is enclosed in single quotes. Treat as OK. | ||
if (text.length >= 2 && text[0] === NoUnexternalizedStringsRuleWalker.SINGLE_QUOTE | ||
&& text[text.length - 1] === NoUnexternalizedStringsRuleWalker.SINGLE_QUOTE) { | ||
return; | ||
} | ||
let info = this.findDescribingParent(node); | ||
// Ignore strings in import and export nodes. | ||
if (info && info.ignoreUsage) { | ||
return; | ||
} | ||
let callInfo = info ? info.callInfo : null; | ||
if (callInfo && this.ignores[callInfo.callExpression.expression.getText()]) { | ||
return; | ||
} | ||
if (!callInfo || callInfo.argIndex === -1 || !this.signatures[callInfo.callExpression.expression.getText()]) { | ||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), `Unexternalized string found: ${node.getText()}`)); | ||
return; | ||
} | ||
// We have a string that is a direct argument into the localize call. | ||
let messageArg: ts.Expression = callInfo.argIndex === this.messageIndex | ||
? callInfo.callExpression.arguments[this.messageIndex] | ||
: null; | ||
if (messageArg && messageArg !== node) { | ||
this.addFailure(this.createFailure( | ||
messageArg.getStart(), messageArg.getWidth(), | ||
`Message argument to '${callInfo.callExpression.expression.getText()}' must be a string literal.`)); | ||
return; | ||
} | ||
} | ||
|
||
private findDescribingParent(node: ts.Node): | ||
{ callInfo?: { callExpression: ts.CallExpression, argIndex: number }, ignoreUsage?: boolean; } { | ||
let kinds = SyntaxKind.current(); | ||
let parent: ts.Node; | ||
while ((parent = node.parent)) { | ||
let kind = parent.kind; | ||
if (kind === kinds.CallExpression) { | ||
let callExpression = parent as ts.CallExpression; | ||
return { callInfo: { callExpression: callExpression, argIndex: callExpression.arguments.indexOf(<any>node) }}; | ||
} else if (kind === kinds.ImportEqualsDeclaration || kind === kinds.ImportDeclaration || kind === kinds.ExportDeclaration) { | ||
return { ignoreUsage: true }; | ||
} else if (kind === kinds.VariableDeclaration || kind === kinds.FunctionDeclaration || kind === kinds.PropertyDeclaration | ||
|| kind === kinds.MethodDeclaration || kind === kinds.VariableDeclarationList || kind === kinds.InterfaceDeclaration | ||
|| kind === kinds.ClassDeclaration || kind === kinds.EnumDeclaration || kind === kinds.ModuleDeclaration | ||
|| kind === kinds.TypeAliasDeclaration || kind === kinds.SourceFile) { | ||
return null; | ||
} | ||
node = parent; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/// <reference path="../typings/mocha.d.ts" /> | ||
/// <reference path="../typings/chai.d.ts" /> | ||
|
||
/* tslint:disable:quotemark */ | ||
/* tslint:disable:no-multiline-string */ | ||
|
||
import TestHelper = require('./TestHelper'); | ||
|
||
/** | ||
* Unit tests. | ||
*/ | ||
describe('noUnexternalizedStringsRule', () : void => { | ||
|
||
var ruleName : string = 'no-unexternalized-strings'; | ||
|
||
it('should pass on single quote', () : void => { | ||
var script : string = ` | ||
let str = 'Hello Worlds'; | ||
`; | ||
TestHelper.assertViolationsWithOptions(ruleName, [{ signatures: ['localize', 'nls.localize'], messageIndex: 1 }], script, [ ]); | ||
}); | ||
|
||
it('should pass on template expression', () : void => { | ||
var script : string = 'let str = `Hello ${var} Worlds`;'; | ||
TestHelper.assertViolationsWithOptions(ruleName, [{ signatures: ['localize', 'nls.localize'], messageIndex: 1 }], script, [ ]); | ||
}); | ||
|
||
it('should pass on localize', () : void => { | ||
var script : string = ` | ||
let str = localize("key", "Hello Worlds"); | ||
`; | ||
TestHelper.assertViolationsWithOptions(ruleName, [{ signatures: ['localize', 'nls.localize'], messageIndex: 1 }], script, [ ]); | ||
}); | ||
|
||
it('should pass on nls.localize', () : void => { | ||
var script : string = ` | ||
import nls = require('nls'); | ||
let str = nls.localize("Key", "Hello World"); | ||
`; | ||
TestHelper.assertViolationsWithOptions(ruleName, [{ signatures: ['localize', 'nls.localize'], messageIndex: 1 }], script, [ ]); | ||
}); | ||
|
||
it('should pass on import', () : void => { | ||
var script : string = ` | ||
import { localize } from "nls"; | ||
let str = localize("Key", "Hello World"); | ||
`; | ||
TestHelper.assertViolationsWithOptions(ruleName, [{ signatures: ['localize', 'nls.localize'], messageIndex: 1 }], script, [ ]); | ||
}); | ||
|
||
it('should pass on import equals', () : void => { | ||
var script : string = ` | ||
import nls = require("nls"); | ||
let str = nls.localize("Key", "Hello World"); | ||
`; | ||
TestHelper.assertViolationsWithOptions(ruleName, [{ signatures: ['localize', 'nls.localize'], messageIndex: 1 }], script, [ ]); | ||
}); | ||
|
||
it('should pass on ignores', () : void => { | ||
var script : string = ` | ||
var nls = require("nls"); | ||
let str = nls.localize("Key", "Hello World"); | ||
`; | ||
TestHelper.assertViolationsWithOptions(ruleName, | ||
[{ signatures: ['localize', 'nls.localize'], messageIndex: 1, ignores: ['require'] }], | ||
script, [ ] | ||
); | ||
}); | ||
|
||
it('should fail on my.localize', () : void => { | ||
var script : string = ` | ||
let str = my.localize('key', "Needs localization"); | ||
`; | ||
TestHelper.assertViolationsWithOptions(ruleName, [{ signatures: ['localize', 'nls.localize'], messageIndex: 1 }], script, [ | ||
{ | ||
"failure": "Unexternalized string found: \"Needs localization\"", | ||
"name": "file.ts", | ||
"ruleName": "no-unexternalized-strings", | ||
"startPosition": { | ||
"character": 42, | ||
"line": 2 | ||
} | ||
} | ||
]); | ||
}); | ||
|
||
it('should fail on function call inside localize', () : void => { | ||
var script : string = ` | ||
let str = localize('key', foo("Needs localization")); | ||
`; | ||
TestHelper.assertViolationsWithOptions(ruleName, [{ signatures: ['localize', 'nls.localize'], messageIndex: 1 }], script, [ | ||
{ | ||
"failure": "Unexternalized string found: \"Needs localization\"", | ||
"name": "file.ts", | ||
"ruleName": "no-unexternalized-strings", | ||
"startPosition": { | ||
"character": 43, | ||
"line": 2 | ||
} | ||
} | ||
]); | ||
}); | ||
|
||
it('should fail on method call inside localize', () : void => { | ||
var script : string = ` | ||
let str = localize('key', this.foo("Needs localization")); | ||
`; | ||
TestHelper.assertViolationsWithOptions(ruleName, [{ signatures: ['localize', 'nls.localize'], messageIndex: 1 }], script, [ | ||
{ | ||
"failure": "Unexternalized string found: \"Needs localization\"", | ||
"name": "file.ts", | ||
"ruleName": "no-unexternalized-strings", | ||
"startPosition": { | ||
"character": 48, | ||
"line": 2 | ||
} | ||
} | ||
]); | ||
}); | ||
|
||
it('should fail on variable declaration', () : void => { | ||
var script : string = ` | ||
let str = "Needs localization"; | ||
`; | ||
TestHelper.assertViolationsWithOptions(ruleName, [{ signatures: ['localize', 'nls.localize'], messageIndex: 1 }], script, [ | ||
{ | ||
"failure": "Unexternalized string found: \"Needs localization\"", | ||
"name": "file.ts", | ||
"ruleName": "no-unexternalized-strings", | ||
"startPosition": { | ||
"character": 23, | ||
"line": 2 | ||
} | ||
} | ||
]); | ||
}); | ||
|
||
it('should fail on function declaration', () : void => { | ||
var script : string = ` | ||
let str: string = undefined; | ||
function foo() { | ||
str = "Hello World"; | ||
} | ||
`; | ||
TestHelper.assertViolationsWithOptions(ruleName, [{ signatures: ['localize', 'nls.localize'], messageIndex: 1 }], script, [ | ||
{ | ||
"failure": "Unexternalized string found: \"Hello World\"", | ||
"name": "file.ts", | ||
"ruleName": "no-unexternalized-strings", | ||
"startPosition": { | ||
"character": 23, | ||
"line": 4 | ||
} | ||
} | ||
]); | ||
}); | ||
|
||
it('should fail on binary expression', () : void => { | ||
var script : string = ` | ||
localize('key', "Hello " + "World"); | ||
`; | ||
TestHelper.assertViolationsWithOptions(ruleName, [{ signatures: ['localize', 'nls.localize'], messageIndex: 1 }], script, [ | ||
{ | ||
"failure": "Message argument to 'localize' must be a string literal.", | ||
"name": "file.ts", | ||
"ruleName": "no-unexternalized-strings", | ||
"startPosition": { | ||
"character": 29, | ||
"line": 2 | ||
} | ||
} | ||
]); | ||
}); | ||
}); | ||
/* tslint:enable:quotemark */ | ||
/* tslint:enable:no-multiline-string */ |