'@' SyntaxError: Invalid or unexpected tok

2019-09-20 05:39发布

问题:

currently getting an error where @ is an unexpected token.

Not 100% sure how i would even go about fixing this to be honest. I've tried removing the @ but later found out its part of params and i need it. I have also tried installing npm install params but later found out that that is completely unrelated, maybe I need a break or something but nothing in this error makes sense to me right now.

The Error:

/root/shiba/BustabitClient.js:35
      @param {string} uname
      ^

SyntaxError: Invalid or unexpected token
    at new Script (vm.js:84:7)
    at createScript (vm.js:264:10)
    at Object.runInThisContext (vm.js:312:10)
    at Module._compile (internal/modules/cjs/loader.js:696:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:747:10)
    at Module.load (internal/modules/cjs/loader.js:628:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:568:12)
    at Function.Module._load (internal/modules/cjs/loader.js:560:3)
    at Module.require (internal/modules/cjs/loader.js:665:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/root/shiba/Shiba.js:12:25)
    at Module._compile (internal/modules/cjs/loader.js:736:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:747:10)
    at Module.load (internal/modules/cjs/loader.js:628:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:568:12)
    at Function.Module._load (internal/modules/cjs/loader.js:560:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:801:12)
    at executeUserCode (internal/bootstrap/node.js:526:15)
    at startMainThreadExecution (internal/bootstrap/node.js:439:3)

BustabitClient.JS:

const EventEmitter = require("events")
const PowerClient = require("power-client")


const debug     = require("debug")("shiba:client")
const debugchat = require("debug")("shiba:chat")
const debuggame = require("debug")("shiba:game")
const debugtick = require("debug")("verbose:tick")

module.exports = class BustabitClient extends EventEmitter {
    constructor(config) {
        super()

        this.config = config
        this.clearState()

        debug("Setting up connection to %s", config.API_SERVER)
        this.socket = new PowerClient(config.API_SERVER, config.API_KEY, config.SESSION)

        this.socket.on("connected", this.handleConnected.bind(this))
        this.socket.on("disconnected", this.handleDisconnected.bind(this))
        this.socket.on("error", error => console.error("onError", error))

        this.socket.on("gameStarting", this.handleGameStarting.bind(this))
        this.socket.on("betPlaced", this.handleBetPlaced.bind(this))
        this.socket.on("gameStarted", this.handleGameStarted.bind(this))
        this.socket.on("cashedOut", this.handleGameTick.bind(this))
        this.socket.on("gameEnded", this.handleGameEnded.bind(this))
    }

      //Attempt to mute a user and return whether he actually was. Users are not muted if they are on
      //the USER_WHITELIST in Config.js.
      @param {string} uname
      @param {string} [reason]
      @param {string} [channel] chat channel to which the broadcast the mute
      @param {number} [wagerRequirement] required wager volume before user can chat again, otherwise allowing the server to choose
      @returns {Promise<boolean>} true if the user was muted

    async doMute(uname, reason, channel, wagerRequirement = -1) {
        if (this.config.USER_WHITELIST.includes(uname.toLowerCase())) {
            debugchat("Not muting whitelisted user: %s amount: %s", uname, wagerRequirement)
            return false
        }

        debugchat("Muting user: %s amount: %s", uname, wagerRequirement)
        wagerRequirement = wagerRequirement || -1
        try {
            await this.socket.send("mute", { uname, reason, channel, wagerRequirement })
        } catch (error) {
            console.error("[BustabitClient.doMute]", error)
        }
        return true
    }

       //Send a public chat message to the given channel.
       @param {string} message
       @param {string} channel
    async doSay(message, channel) {
        debugchat("Saying:", message)
        try {
            await this.socket.send("say", { channel, message })
        } catch (error) {
            console.error("[BustabitClient.doSay]", error)
        }
    }

      // Fetch a game's information and reshape it into what Pg.putGame expects.
       @param {number} id game ID
       @returns {Promise}
    async getGameInfo(id) {
        const game = await this.socket.send("getGameInfo", id)
        game.created = new Date(game.created).getTime()
        game.startTime = game.created + 5000
        game.serverSeed = game.hash
        game.playing = {}
        game.cashedOut = []
        for (const { uname, wager, cashOut } of game.bets) {
            if (!cashOut) {
                game.playing[uname] = { wager }
            } else {
                game.cashedOut.push({ uname, wager, cashedAt: cashOut })
            }
        }
        return game
    }


    // event handlers

    handleConnected(data) {
        const { engineInfo, loggedIn } = data
        if (!loggedIn) {
            throw new Error("invalid session ID")
        }
        this.username = loggedIn.userInfo.uname
        debug("connected to server as", this.username)

        // TODO: game server sends incorrect/incomplete information for games that were forcibly ended

        // save current game state
        this.game = {
            id:        engineInfo.gameId,
            state:     engineInfo.gameState,
            created:   Date.now() - engineInfo.elapsed - 5000,
            startTime: Date.now() - engineInfo.elapsed,
            playing:   engineInfo.playing,
            cashedOut: engineInfo.cashedOut || [],
        }

        this.emit("connected", data)
    }

    handleDisconnected(reconnectIn) {
        debug(`disconnected from server. attempting to reconnect in ${reconnectIn/1000}s`)
        this.clearState()
    }

    handleGameStarting({ gameId }) {
        debuggame(`Game #${gameId} starting`)
        this.game = {
            id:        gameId,
            state:     "GAME_STARTING",
            created:   Date.now(),
            startTime: Date.now() + 5000,
            playing:   {},
            cashedOut: [],
        }
        this.emit("gameStarting")
    }

    handleBetPlaced({ uname, wager, payout }) {
        debuggame(`Player ${uname} wagered ${wager} @ ${payout.toFixed(2)}`)
        this.game.playing[uname] = { wager, payout }
    }

    handleGameStarted() {
        debuggame(`Game #${this.game.id} started`)
        this.game.state = "GAME_STARTED"
        this.emit("gameStarted")
    }

    handleGameTick([ multiplier, cashOuts ]) {
        debugtick(`Game #${this.game.id} ticked`, { multiplier, cashOuts: cashOuts.length })

        // players that were cashed out automatically
        for (const uname of Object.keys(this.game.playing)) {
            const { wager, payout } = this.game.playing[uname]
            if (multiplier >= payout) {
                this.game.cashedOut.push({ uname, wager, cashedAt: payout })
                delete this.game.playing[uname]
                debuggame(`Player ${uname} was automatically cashed out at ${payout.toFixed(2)}`)
            }
        }

        // players that cashed out manually
        for (const [ cashedAt, uname ] of cashOuts) {
            console.assert(this.game.playing[uname])
            const { wager } = this.game.playing[uname]
            this.game.cashedOut.push({ uname, wager, cashedAt })
            delete this.game.playing[uname]
            debuggame(`Player ${uname} manually cashed out at ${cashedAt.toFixed(2)}`)
        }

        this.emit("cashedOut")
    }
    handleGameEnded({ bust, forced, hash }) {
        debuggame(`Game #${this.game.id} crashed @${bust}`)
        this.game.state = "GAME_ENDED"
        this.game.bust = bust
        this.game.forced = forced
        this.game.serverSeed = hash

        if (forced) {
            for (const uname of Object.keys(this.game.playing)) {
                const { wager } = this.game.playing[uname]
                this.game.cashedOut.push({ uname, wager, cashedAt: forced })
                delete this.game.playing[uname]
                debuggame(`Player ${uname} was forced to cash out at ${forced.toFixed(2)}`)
            }
        }

        this.emit("gameEnded", this.game)
    }

    /** Reset the user and game state to its zero values. */
    clearState() {
        this.username = null
        this.game = null
    }
}

回答1:

I guess you have a typo in your code. @param is supposed to be in comments to document an API:

/**
 * Attempt to mute a user and return whether he actually was. Users are not muted if they are on
 * the USER_WHITELIST in Config.js.
 *
 * @param {string} uname
 * @param {string} [reason]
 * @param {string} [channel] chat channel to which the broadcast the mute
 * @param {number} [wagerRequirement] required wager volume before user can chat again, otherwise allowing the server to choose
 * @returns {Promise<boolean>} true if the user was muted
 */