【问题标题】:How do you ensure there are no non-ASCII characters in TypeScript files?如何确保 TypeScript 文件中没有非 ASCII 字符?
【发布时间】:2017-03-16 13:14:09
【问题描述】:

确保我的 TypeScript 和相应的 JavaScript 文件中只有 ASCII 字符的最佳方法是什么?

【问题讨论】:

  • 请澄清。 UTF-8 是 Unicode 字符集的编码。没有“UTF-8 字符”之类的东西。也许显示文件中的字节序列会有所帮助。
  • @TomBlodget 谢谢。我澄清了我的问题,我相信

标签: typescript tslint


【解决方案1】:

目前还没有规则。

自定义规则

发件人:https://github.com/palantir/tslint/#writing-custom-rules

以下是一个想法:

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);
}

这是一个足够好的示例,我留给您进行测试和调试。享受?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    相关资源
    最近更新 更多