Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using the "xmlFormatter()" VS code does not able to identify. #68

Closed
harshitgupta028 opened this issue Jan 12, 2024 · 16 comments
Closed

Comments

@harshitgupta028
Copy link

Hi There,

var xmlFormatter = require("xml-formatter");

console.log(xmlFormatter(.....XML code goes here............));

So, xmlFormatter() VS code is not able to recognise it.

Error:

This expression is not callable.
Type 'typeof import("c:/Users//Documents/Extension/xquery-formatter/node_modules/xml-formatter/dist/types/index")' has no call signatures.

Now, the interesting part is after running the code it is working i.e. I'm getting the formatted code.

@chrisbottin
Copy link
Owner

Hi @harshitg123, can you let me know how you are using xml-formatter inside VS code so I can replicate the issue?

@harshitgupta028
Copy link
Author

Hello @chrisbottin,

const xmlFormatter = require('xml-formatter');

xmlFormatter('<root>' + fetchXqueryData() + '</root>', {
			indentation: ' ',
			lineSeparator: '\n'
		})

fetchXqueryData() is a function which is fetching the XQuery. for sample you can use.

<xml>
  <person country="USA" name="John"/>
  <person country="USA" name="Jack"/>
  <person country="Germany" name="Johann"/>
 </xml>
Screenshot 2024-01-13 at 12 19 11 AM

@chrisbottin
Copy link
Owner

Thanks, I see what you mean now ... literally 😄.

I will take a look and get back to you.

@chrisbottin
Copy link
Owner

@harshitg123 I'm not able to replicate the issue yet, perhaps it's a VS code configuration thing.

Can you share your jsconfig.json and any other configs that might be needed (package.json, etc)?

@harshitgupta028
Copy link
Author

@chrisbottin

jsconfig.json file

{
	"compilerOptions": {
		"module": "Node16",
		"target": "ES2022",
		"checkJs": true,  /* Typecheck .js files. */
		"lib": [
			"ES2022"
		]
	},
	"exclude": [
		"node_modules"
	]
}

I guess it is something related to the typescript check, If I make "checkJs" to false the error is gone.

chrisbottin added a commit that referenced this issue Jan 18, 2024
@chrisbottin
Copy link
Owner

@harshitg123 I have made some changes in version 3.6.1 which should fix this problem.

The issue was the TS declaration file was intended for ES module type but you are using CommonJS and making use of the TS declaration file using jsconfig.json with checkJs.

Now the new TS declaration file should be compatible with both ES and CommonJS module types.

@harshitgupta028
Copy link
Author

Thanks @chrisbottin I've installed the new version it's working fine now  👍🏻

@udavx
Copy link

udavx commented Jan 19, 2024

Hi, @chrisbottin

I was wondering whether the new compile:types:post-hook script

  • (a) really works (the extra empty pair of quotes makes my sed here somewhat confused),
  • (b) really does what is needed, because the resulting dist/types/index.d.ts makes tsc hiss at me
node_modules/xml-formatter/dist/types/index.d.ts:57:1 - error TS2309: An export assignment cannot be used in a module with other exported elements.

57 export = formatXml;
   ~~~~~~~~~~~~~~~~~~~

I will continue scratching my head and looking for configuration switches, of course, but still.

@chrisbottin
Copy link
Owner

Hi @udavx,

Yes the new compile:types:post-hook script works as intended.

a) The sed command is targeted to Mac.
b) Yes, you're right, it makes the declaration file slightly incorrect as you pointed out but I went for it since it makes it work for commonJS and ES module project setup.

What's your tsconfig.json / project setup? You should not have tsc reporting errors from the node_modules folder.

If you share your setup, I can have a look.

@chrisbottin
Copy link
Owner

The changes made previously have been reverted as they caused other issues (#70)

@udavx, this should sort out your problem.

@harshitg123, we will need to come up with another solution.

Instead of using CommonJS modules, can you use ES modules?

You will need to change the require statements to import and set "type": "module" in your package.json

@chrisbottin chrisbottin reopened this Jan 20, 2024
@udavx
Copy link

udavx commented Jan 20, 2024

@chrisbottin, thank you for the prompt action (incl. the 3.6.2)

For the record or not, "exclude" of my tsconfig.json does have an entry for "node_modules".

I tried to create a minimal set to reproduce it, here it is: https://github.com/udavx/demo-for-issue-68-of-xml-formatter-3.6.1

Initially, it's npx tsc to reproduce, I will proceed with adding some npm scripts and stuff.

@harshitgupta028
Copy link
Author

Hi @chrisbottin,

#68 (comment)

Sure, Let me check and update it.

@harshitgupta028
Copy link
Author

Hi @chrisbottin,

I've used type: module in package.json but by doing this I cannot import my own exported functions in the extension.js file.

image

@chrisbottin
Copy link
Owner

@harshitg123, try with this jsconfig.json ...

{
  "compilerOptions": {
    "moduleResolution": "node",
    "checkJs": true
  },
  "exclude": ["node_modules"]
}

And you might need to add .js at the end of the filenames in the imports.

@harshitgupta028
Copy link
Author

Hi @chrisbottin, Really sorry for reply was occupied by some urgent work.

It worked for me, Thanks

@silverwind
Copy link

silverwind commented Aug 21, 2024

Noticed the same issue with the This expression is not callable error in tsc. It could be solved if this module would add a named export in addition to the existing default export. I certainly don't want to introduce a jsconfig.json specifically for this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants