如何确保TypeScript文件中没有非ASCII字符?
确保我的TypeScript和相应的JavaScript文件中只有ASCII字符的最佳方法是什么?目前还没有相关规则 习惯规则 发件人: 以下是一个想法:如何确保TypeScript文件中没有非ASCII字符?,typescript,tslint,Typescript,Tslint,确保我的TypeScript和相应的JavaScript文件中只有ASCII字符的最佳方法是什么?目前还没有相关规则 习惯规则 发件人: 以下是一个想法: import * as ts from "typescript"; import * as Lint from "tslint/lib/lint"; export class Rule extends Lint.Rules.AbstractRule { public static FAILURE_STRING = "unicode
import * as ts from "typescript";
import * as Lint from "tslint/lib/lint";
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "unicode forbidden";
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new SourcefileWalker(sourceFile, this.getOptions()));
}
}
// The walker takes care of all the work.
class SourceFileWalker extends Lint.RuleWalker {
public visitSourceFile(node: ts.SourceFile) {
// ACTUAL TODO:
const text = node.getFullText();
// Match ascii only
if (!isASCII(text)){
// create a failure at the current position
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
}
// call the base version of this visitor to actually parse this node
super.visitSourceFile(node);
}
}
function isASCII(str, extended) {
return (extended ? /^[\x00-\xFF]*$/ : /^[\x00-\x7F]*$/).test(str);
}
这是一个足够好的样本,我留给你测试和调试。享受现在还没有规则 习惯规则 发件人: 以下是一个想法:
import * as ts from "typescript";
import * as Lint from "tslint/lib/lint";
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "unicode forbidden";
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new SourcefileWalker(sourceFile, this.getOptions()));
}
}
// The walker takes care of all the work.
class SourceFileWalker extends Lint.RuleWalker {
public visitSourceFile(node: ts.SourceFile) {
// ACTUAL TODO:
const text = node.getFullText();
// Match ascii only
if (!isASCII(text)){
// create a failure at the current position
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
}
// call the base version of this visitor to actually parse this node
super.visitSourceFile(node);
}
}
function isASCII(str, extended) {
return (extended ? /^[\x00-\xFF]*$/ : /^[\x00-\x7F]*$/).test(str);
}
这是一个足够好的样本,我留给你测试和调试。请澄清。UTF-8是Unicode字符集的编码。没有“UTF-8字符”这样的东西。也许显示文件中的字节序列会有所帮助。@TomBlodget谢谢。我澄清了我的问题,我相信请澄清。UTF-8是Unicode字符集的编码。没有“UTF-8字符”这样的东西。也许显示文件中的字节序列会有所帮助。@TomBlodget谢谢。我想我澄清了我的问题