UPDATE (2017-02)
Node Tools for Visual Studio (NTVS) use the Salsa parsing engine since v1.2, and using NTVS is most likely the least resistance to support JSX.
https://github.com/Microsoft/nodejstools
Read (and confirm) this answer for more details: stack overflow
ORIGINAL RESPONSE
I ran into the same problem and found two solutions: one using ReSharper and one modifying the external Visual Studio web tools.
SOLUTION 1
In ReSharper 10:
- open the Options dialog box
- Code Editing> JavaScript> Inspections, select ECMAScript 6 at the JavaScript level .
- make sure that the Enable JSX syntax in .JS files is selected (provided that you use the JSX syntax).
- you also need to disable javascript syntax errors in Visual Studio as follows:
- go to Tools> Options> Text Editor> JavaScript> IntelliSense
- clear the Show syntax errors check box and click OK.
- reload VS solution
After a reboot, the red squigglies disappeared for me. However, syntax highlighting for JSX does not work. The start segment of any elements declared in the render function does not have the correct coloring - but this is easy to ignore.
I should also mention that the .js extension is required for javascript files. If you give them the .jsx extension, you will get red squigglies in your first import statement. The error message will be JSX Parser: illegal import declaration . (This can be eliminated using solution No. 2 below)
UPDATE: thanks to @SntsDev for this workaround There is a way to avoid naming .jsx files as .js :
- in Visual Studio go to Options> Text Editor> File Extension
- add jsx and assign it to the JavaScript Editor
SOLUTION 2
Curiosity got better than me, and I wanted to explore if there was a solution without a ReSharper solution. Visual Studio uses a locally managed node server called react-server to parse JSX. This module can be found here:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Web Tools\External\react-server
UPDATE to upgrade Visual Studio 2015 3 Thanks to @TheQuickBrownFox for comment / update. For update 3, the location of the react-server commands is now in this file:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Web\External\vs-task-server\react-commands.js
Further checking the server.js or react-commands.js file from the above folders (s), there is a function called transformJsxFromPost or transformJsx . This method contains the following line: var transformed = reactTools.transformWithDetails(code, { elementMap: true }); . This is a link to the react-tools module ( https://www.npmjs.com/package/react-tools ), which has more options for parsing ES6.
In this way:
replace this line:
var transformed = reactTools.transformWithDetails(code, { elementMap: true });
with the following:
var transformed = reactTools.transformWithDetails(code, { elementMap: true, es6module: "--es6module", harmony: "--harmony" });
Note the addition of the --es6module and --harmony , which instruct react-tools process incoming code as ES6.
disable javascript syntax errors in Visual Studio as follows:
- in Visual Studio go to Tools> Options> Text Editor> JavaScript> IntelliSense
- uncheck Show syntax errors and click OK
IMPORTANT: restart Visual Studio. Your .jsx files with .jsx code should no longer have red squiggles on your ES6 code.
NOTES:
- I do not know if the change specified in DECISION 2 will affect the
server.js file for code other than ES6. Therefore, implement at your own risk. - In addition, it is quite possible / likely, your changes can be overwritten and then updated in Visual Studio.
- It would be great / fun to replace using
react-tools inside react-server with the Babel CLI. UPDATE: Thanks to @NickDewitt, it looks like he was able to do the job: https://stackoverflow.com/a/312969/
Adam Weber Dec 05 '15 at 20:26 2015-12-05 20:26
source share