本文整理匯總了TypeScript中tslint/lib/language/rule/rule.RuleFailure.getFileName方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript RuleFailure.getFileName方法的具體用法?TypeScript RuleFailure.getFileName怎麽用?TypeScript RuleFailure.getFileName使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tslint/lib/language/rule/rule.RuleFailure
的用法示例。
在下文中一共展示了RuleFailure.getFileName方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: super
super('no-unused-imports', (failure: RuleFailure): void => {
const fileName: string = failure.getFileName();
const start: RuleFailurePosition = failure.getStartPosition();
const end: RuleFailurePosition = failure.getEndPosition();
let fileContents: string = this.readFile(fileName);
let startOfViolation: number = fileContents.lastIndexOf('\n', start.getPosition());
if (startOfViolation === -1) {
startOfViolation = 0; // make sure to handle the first line in the file
}
const endOfViolation: number = fileContents.indexOf('\n', end.getPosition());
let line: string = fileContents.substring(startOfViolation, endOfViolation);
line = line.trim(); // trim off any new lines, handle those in regular expression
line = line.replace(/\(/, '\\(').replace(/\)/, '\\)');
const regex: RegExp = new RegExp('\\n*' + line + '\\n*', 'mg');
if (startOfViolation === 0) {
fileContents = fileContents.replace(regex, '');
} else {
fileContents = fileContents.replace(regex, '\n');
}
this.writeFile(fileName, fileContents);
/* tslint:disable:no-console */
console.log('Automatically removing unused import. Please re-compile and re-lint: ' + fileName);
/* tslint:enable:no-console */
});
示例2: super
super('no-require-imports', (failure: RuleFailure): void => {
const fileName: string = failure.getFileName();
const fileContents: string = this.readFile(fileName);
const start: number = failure.getStartPosition().getPosition();
const end: number = failure.getEndPosition().getPosition();
let importStartIndex = fileContents.lastIndexOf('\n', start) + 1;
if (importStartIndex === -1) {
importStartIndex = 0;
}
const importEndIndex = fileContents.indexOf('\n', end);
const leftSide: string = fileContents.substring(0, importStartIndex);
const middle: string = fileContents.substring(importStartIndex, importEndIndex).trim();
const rightSide: string = fileContents.substring(importEndIndex);
let newImport: string = middle.replace(
/import\s+(.*)\s+=\s*require\(((.|\s)*)\);/m,
'import {$1} from $2;');
newImport = newImport.replace(/from \n/, 'from\n'); // clean up some spacing
const newContent: string = leftSide + newImport + rightSide;
this.writeFile(fileName, newContent);
/* tslint:disable:no-console */
console.log('Automatically converting require-style import to an ES6 import. Please re-compile and re-lint: ' + fileName);
/* tslint:enable:no-console */
});
示例3: formatFailure
private formatFailure(failure: RuleFailure): string {
const fileName: string = failure.getFileName();
const failureString: string = failure.getFailure();
const ruleName: string = failure.getRuleName();
const lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
const positionTuple = '[' + (lineAndCharacter.line + 1) + ', ' + (lineAndCharacter.character + 1) + ']';
return '(' + ruleName + ') ' + fileName + positionTuple + ': ' + failureString;
}
示例4: super
super('no-var-keyword', (failure: RuleFailure): void => {
const fileName: string = failure.getFileName();
const start: RuleFailurePosition = failure.getStartPosition();
const fileContents: string = this.readFile(fileName);
const leftSide: string = fileContents.substring(0, start.getPosition());
const rightSide: string = fileContents.substring(start.getPosition() + 3);
const newContent: string = leftSide + 'let' + rightSide;
this.writeFile(fileName, newContent);
/* tslint:disable:no-console */
console.log('Automatically converting var to let. Please re-compile and re-lint: ' + fileName);
/* tslint:enable:no-console */
});
示例5: super
super('prefer-const', (failure: RuleFailure): void => {
const fileName: string = failure.getFileName();
const fileContents: string = this.readFile(fileName);
const start: number = failure.getStartPosition().getPosition();
let leftSide: string = fileContents.substring(0, start);
leftSide = leftSide.replace(/let\s?$/, 'const ');
const rightSide: string = fileContents.substring(start);
const newContent: string = leftSide + rightSide;
this.writeFile(fileName, newContent);
/* tslint:disable:no-console */
console.log('Automatically converting var/let to const. Please re-compile and re-lint: ' + fileName);
/* tslint:enable:no-console */
});