dsherret / ts-nameof

nameof in TypeScript
MIT License
492 stars 23 forks source link

babel-plugin-ts-nameof : Call expression must have one argument or type argument: nameof() #110

Closed ericaskari closed 3 years ago

ericaskari commented 3 years ago

I had a rollup setup while using ts-nameof.

and I could use ts-nameof like: nameof<PageContainer>() which would turn to PageContainer

{
    "ts-nameof": "^5.0.0",
    "@types/ts-nameof": "^4.2.1",
    "rollup": "^2.39.0",
    "ttypescript": "^1.5.12",
}
export default {
    input: "src/index.ts",
    output: [...],
    plugins: [
        typescript({
            typescript: ttypescript,
            clean: true,
            useTsconfigDeclarationDir: true,
            transformers: [() => ({
                before: [ tsNameOf ],
                after: [  ]
            })],
            tsconfigOverride: {
                compilerOptions: {
                    sourceMap: true,
                    inlineSourceMap: true,
                    plugins: [],
                }
            }
        }),
    ],
};

recently I changed to nx monorepo setup and I installed

{
"@types/ts-nameof": "^4.2.1",
"babel-plugin-ts-nameof": "^4.2.1"
"ts-nameof": "^5.0.0"
}

and .babelrc:

{
  "presets": ["@nrwl/react/babel"],
      "plugins": [
          "babel-plugin-ts-nameof"
      ] 
}

I don't know why but global nameof wasn't avaiable so I added ts-nameof to types in tsconfig as following

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../../dist/out-tsc",
    "types": ["ts-nameof", "node"],
    "module": "ESNext",
    "target": "es2015"
  }
}

and when I build the project I get:

Call expression must have one argument or type argument: nameof()

seems like a bug because in rollup it works.

and if I select a method from interface it works fine but empty it doesn't

console.log(nameof<Provider>(x => x.Something)) prints Something

ericaskari commented 3 years ago

So I created an empty ts project with babel with this plugin and it worked fine! I guess it has something to do with nx or maybe other plugins that are used there.

Yogoort commented 3 years ago

I faced with the same error: In this part of code nameof<SomeEntityModel>()

Call expression must have one argument or type argument: nameof()

      at Object.throwErrorForSourceFile (node_modules/@ts-nameof/common/dist/errors.js:9:11)
      at PluginPass.CallExpression (node_modules/@ts-nameof/transforms-babel/dist/index.js:19:33)
      at newFn (node_modules/@babel/traverse/lib/visitors.js:171:21)
      at NodePath._call (node_modules/@babel/traverse/lib/path/context.js:53:20)
      at NodePath.call (node_modules/@babel/traverse/lib/path/context.js:40:17)
      at NodePath.visit (node_modules/@babel/traverse/lib/path/context.js:90:31)
      at TraversalContext.visitQueue (node_modules/@babel/traverse/lib/context.js:99:16)
      at TraversalContext.visitMultiple (node_modules/@babel/traverse/lib/context.js:68:17)
      at TraversalContext.visit (node_modules/@babel/traverse/lib/context.js:125:19)
      at Function.traverse.node (node_modules/@babel/traverse/lib/index.js:76:17) 

My package.json

{
        "@types/ts-nameof": "^4.2.1",
        "babel-plugin-ts-nameof": "^4.2.1",
        "ts-nameof": "^5.0.0",
}

babel.config

module.exports = {
    presets: [
        [
            "@babel/preset-env",
            {
                modules: false,
                useBuiltIns: "entry",
                corejs: {
                    version: "3.8",
                    proposals: true,
                },
            },
        ],
        "@babel/preset-flow",
        [
            "@babel/preset-typescript",
            {
                allowDeclareFields: true,
            },
        ],
        "@vue/cli-plugin-babel/preset",
    ],
    plugins: ["@babel/plugin-transform-typescript", "babel-plugin-ts-nameof"],
    env: {
        test: {
            plugins: ["require-context-hook"],
        },
    },
};

jest.config

transform: {
        ".*\\.vue$": "vue-jest",
        ".(ts|tsx)": "babel-jest",
    }