1

所以我从这里改编了这段代码用于学习目的:

"use strict";
/// <reference path="./definitelyTyped/node/node.d.ts" />
import * as net from "net";

let sockets: net.Socket[] = [];

/*
 * Cleans the input of carriage return, newline
 */
function cleanInput(data: Buffer): string {
    return data.toString().replace(/(\r\n|\n|\r)/gm, "");
}

/*
 * Method executed when data is received from a socket
 */
function receiveData(socket: net.Socket, data: Buffer): void {
    let cleanData: string = cleanInput(data);
    if (cleanData === "@quit") {
        socket.end("Goodbye!\r\n");
    } else {
        for (var i: number = 0; i < sockets.length; i++) {
            if (sockets[i] !== socket) {
                sockets[i].write(data);
            }
        }
    }
}

/*
 * Method executed when a socket ends
 */
function closeSocket(socket: net.Socket): void {
    let i: number = sockets.indexOf(socket);
    if (i !== -1) {
        sockets.splice(i, 1);
    }
}

/*
 * Callback method executed when a new TCP socket is opened.
 */
function newSocket(socket: net.Socket): void {
    sockets.push(socket);
    socket.write("Welcome to the Telnet server!\r\n");
    socket.on("data", function(data: Buffer): void {
        receiveData(socket, data);
    });
    socket.on("end", function(): void {
        closeSocket(socket);
    });
}

// create a new server and provide a callback for when a connection occurs
let server: net.Server = net.createServer(newSocket);

// listen on port 4444
server.listen(4444);
console.log("started");

我正在使用这些tslint.json规则在此文件上运行tslint 。

这就是我得到的:

app.ts[10, 1]:缺少“使用严格”

app.ts[17, 1]:缺少“使用严格”

app.ts[33, 1]:缺少“使用严格”

app.ts[43, 1]:缺少“使用严格”

我的理解是,"use string"; 文件的第一行应该是整个文件的全局。然而,每个函数都会产生一条消息。我错过了什么吗?

4

2 回答 2

2

我的理解是“使用字符串”;文件的第一行应该是整个文件的全局。然而,每个函数都会产生一条消息

你的理解是正确的。但是,您要求 tslint 确保它适用于所有功能以及"check-function" 设置为 true。将其设置为 false,因为如果您已启用模块级别,则不需要它。

于 2015-08-18T10:28:21.897 回答
1

可以在全局范围或单个函数中启用严格模式:

// Enable strict mode globally
"use strict";
var v = "This script is in strict mode.";

或在本地:

function strict() {
  'use strict';

  function nested() {
    return "Nested function is also in strict mode.";
  }

  return "Function is in strict mode. " + nested();
}

function notStrict() {
  return "This function is not in strict mode.";
}
于 2015-08-18T10:26:19.850 回答