fastify / fastify-plugin

Plugin helper for Fastify
MIT License
198 stars 43 forks source link

TS: Left side of comma operator is unused and has no side effects. #179

Closed wasmake closed 1 year ago

wasmake commented 2 years ago

Prerequisites

Fastify version

3.28.0

Plugin version

No response

Node.js version

16.10

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

Monterey 12.3.1

Description

When using Tap with TypeScript, ts-node fails to compile the Typescript code.

TS2695: Left side of comma operator is unused and has no side effects.
exports.default = (0, fastify_plugin_1.default)(async (fastify, _options) => {
    at createTSError (/node_modules/ts-node/src/index.ts:820:12)
    at reportTSError (/node_modules/ts-node/src/index.ts:824:19)
    at getOutput (/node_modules/ts-node/src/index.ts:1014:36)
    at Object.compile (/node_modules/ts-node/src/index.ts:1322:43)
    at Module.m._compile (/node_modules/ts-node/src/index.ts:1454:30)
    at Module.m._compile (/node_modules/ts-node/src/index.ts:1455:23)
    at module.exports (/node_modules/default-require-extensions/js.js:7:9)
    at /node_modules/append-transform/index.js:64:4
    at require.extensions.<computed> (/node_modules/ts-node/src/index.ts:1458:12)
    at /node_modules/append-transform/index.js:64:4 {
  diagnosticCodes: [ 2695 ]
}

Steps to Reproduce

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./",
    "newLine": "lf",
    "paths": {
      "@/*": ["../src/*"],
      "@/models/*": ["../src/models/*"]
    },
    "typeRoots": ["../node_modules/@types", "../types"],
    "types": ["node"],
    "allowSyntheticDefaultImports": true,
    "target": "ES2020",
    "lib": ["ESNext"],
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node",
    "module": "commonjs",
    "pretty": true,
    "sourceMap": false,
    "resolveJsonModule": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "allowUnusedLabels": true,
    "allowJs": true,
    "noEmit": false,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "noEmitHelpers": true,
    "importHelpers": true
  }
}

Plugin implementation

import mongoSanitize from 'express-mongo-sanitize'
import { FastifyInstance, FastifyPluginOptions, FastifyReply, FastifyRequest, HookHandlerDoneFunction } from 'fastify'
import fp from 'fastify-plugin'

export default fp(async (fastify: FastifyInstance, _options: FastifyPluginOptions): Promise<void> => {
  fastify.addHook('preHandler', function (request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) {
    const payload = request.body as Record<string, unknown> | unknown[]
    const isMongo = request.server.config.DB_CONNECTION === 'mongo'
    if (payload && isMongo) {
      mongoSanitize.sanitize(payload)
    }
    done()
  })
})

To register tsconfig with node-tap:

tsnode.register({
    project: './test/tsconfig.json',
    files: true
  })

Package.json

{
  "name": "Test app",
  "version": "0.0.1",
  "description": "test",
  "main": "./dist/index.js",
  "scripts": {
    "prebuild": "npm run build:clean",
    "build": "npm run tsc:build",
    "build:clean": "rm -f tsconfig.build.tsbuildinfo && rm -Rf ./dist/* ; exit 0",
    "start": "node ./dist/index.js",
    "test": "tap --ts --reporter=spec test/tests/*.test.ts",
  },
  "license": "MIT",
  "dependencies": {
    "@fastify/auth": "^2.0.0",
    "@fastify/autoload": "^4.0.1",
    "@fastify/compress": "^5.0.0",
    "@fastify/cookie": "^6.0.0",
    "@fastify/cors": "^7.0.0",
    "@fastify/env": "^3.0.0",
    "@fastify/formbody": "^6.0.0",
    "@fastify/helmet": "^8.0.0",
    "@fastify/jwt": "^5.0.1",
    "@fastify/multipart": "^6.0.0",
    "@fastify/passport": "^1.0.1",
    "@fastify/rate-limit": "^6.0.0",
    "@fastify/sensible": "^4.0.0",
    "@fastify/session": "^8.2.0",
    "@fastify/swagger": "^6.0.1",
    "@sentry/node": "^6.19.7",
    "@sentry/tracing": "^6.19.7",
    "@typegoose/typegoose": "^9.8.1",
    "axios": "^0.27.2",
    "express-mongo-sanitize": "^2.2.0",
    "fastify": "^3.28.0",
    "http-status": "^1.5.1",
    "joi": "^17.6.0",
    "json-joi-converter": "^17.2.1-f",
    "jsonwebtoken": "^8.5.1",
    "lint-staged": "^12.4.1",
    "luxon": "^2.3.2",
    "mongoose": "^6.3.2",
    "mongoose-autopopulate": "^0.16.1",
    "mongoose-findorcreate": "^3.0.0",
    "mongoose-lean-virtuals": "^0.9.1",
    "mongoose-slug-updater": "^3.3.0",
    "passport": "^0.5.2",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "pino": "^7.11.0",
    "pino-pretty": "^7.6.1",
    "qs": "^6.10.3",
    "reflect-metadata": "^0.1.13",
    "tsyringe": "^4.6.0"
  },
  "devDependencies": {
    "@commitlint/cli": "^16.2.4",
    "@commitlint/config-conventional": "^16.2.4",
    "@types/busboy": "^1.5.0",
    "@types/faker": "^6.6.8",
    "@types/http-errors": "^1.8.2",
    "@types/jsonwebtoken": "^8.5.8",
    "@types/luxon": "^2.3.2",
    "@types/mongoose-lean-virtuals": "^0.5.3",
    "@types/passport-jwt": "^3.0.6",
    "@types/passport-local": "^1.0.34",
    "@types/tap": "^15.0.7",
    "@typescript-eslint/eslint-plugin": "^5.22.0",
    "@typescript-eslint/parser": "^5.22.0",
    "eslint": "^8.14.0",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-prettier": "^4.0.0",
    "eslint-plugin-simple-import-sort": "^7.0.0",
    "faker": "^6.6.6",
    "husky": "^7.0.4",
    "mongodb-memory-server": "^8.5.2",
    "prettier": "^2.6.2",
    "tap": "^16.2.0",
    "ts-node": "^10.7.0",
    "ts-node-dev": "^1.1.8",
    "tsconfig-paths": "^4.0.0",
    "tscpaths": "^0.0.9",
    "tslint-config-prettier": "^1.18.0",
    "typescript": "^4.6.4"
  },
  "tap": {
    "before": "./test/index.ts"
  },
  "private": true,
  "engines": {
    "node": ">=16.0.0"
  },
  "optionalDependencies": {
    "npm-check-updates": "^12.5.11"
  }
}

Running npm run test

Expected Behavior

To pass, but it rather fails on the line of adding a hook with the already mentioned error.

mcollina commented 2 years ago

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

mcollina commented 2 years ago

cc @fastify/typescript

climba03003 commented 2 years ago

I assume the error comes from built code. Anyone testing with tap should not build the code beforehand. It will import the js version precedence than ts version when allowJS enabled.

fox1t commented 2 years ago

Can you try to remove allowJS and run the tests again?

Uzlopak commented 1 year ago

Closing due to inactivity