{"id":22392,"date":"2015-12-30T17:33:57","date_gmt":"2015-12-30T17:33:57","guid":{"rendered":"https:\/\/blog.jetbrains.com\/webstorm\/?p=7972"},"modified":"2021-06-11T12:12:04","modified_gmt":"2021-06-11T11:12:04","slug":"working-with-reactjs-in-webstorm-linting-refactoring-and-compiling","status":"publish","type":"webstorm","link":"https:\/\/blog.jetbrains.com\/zh-hans\/webstorm\/2015\/12\/working-with-reactjs-in-webstorm-linting-refactoring-and-compiling","title":{"rendered":"Working With ReactJS in WebStorm: Linting, Refactoring and Compiling"},"content":{"rendered":"<p>We recently explored <a href=\"https:\/\/blog.jetbrains.com\/zh-hans\/webstorm\/2015\/10\/working-with-reactjs-in-webstorm-coding-assistance\">coding assistance<\/a> that WebStorm provides for React and JSX. Now we would like to talk a bit about the tools in the React ecosystem. In this area it\u2019s not easy to provide a complete overview as tools are developing at a crazy pace. So right now we\u2019ll focus on linters (code quality tools), refactoring and tools that can help us compile code.<\/p>\n<h2>Code analysis<\/h2>\n<p>As you may know, WebStorm has a wide range of built-in inspections for JavaScript and HTML, and these inspections also work for JSX code.<\/p>\n<p>For example, WebStorm alerts you in case of unused variables and functions, missing closing tags, missing statements and much more. For some inspections WebStorm provides quick-fixes, like add a missing semicolon:<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-7973\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2015\/12\/webstorm-react-inspection.png\" alt=\"react-inspection\" width=\"640\"><\/p>\n<p>You can customize the list of inspections in <em>Preferences | Editor | Inspections<\/em>. Disable those you don\u2019t want to see, or change severity level from warning to error or vice versa.<\/p>\n<p>On top of such inspections, you can also use linters like ESLint and JSCS for the JSX code. Let\u2019s talk about these in more detail.<\/p>\n<h3 dir=\"ltr\">ESLint<\/h3>\n<p><a href=\"http:\/\/eslint.org\/\" target=\"_blank\" rel=\"noopener\"><strong>ESLint<\/strong><\/a> is a linting utility that provides a wide range of linting rules, which can also be extended with plugins. WebStorm integrates with ESLint and allows you to see warnings and&nbsp;errors reported by ESLint right in the editor, as you type.<\/p>\n<p>While ESLint itself understands JSX syntax, authors <a href=\"https:\/\/github.com\/eslint\/eslint#does-eslint-support-jsx\" target=\"_blank\" rel=\"noopener\">recommend<\/a> using <a href=\"https:\/\/www.npmjs.com\/package\/eslint-plugin-react\" target=\"_blank\" rel=\"noopener\">eslint-plugin-react<\/a> if you are working with React. To get started, add <em>eslint<\/em> and <em>eslint-plugin-react<\/em> modules to your project via npm, then add an ESLint configuration file <em>.eslintrc<\/em>.<\/p>\n<p>Here\u2019s what <em>.eslint<\/em> file structure looks like when using ESLint 1.x and react plugin:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-linenumbers=\"false\" data-enlighter-title=\"\">{\n    &quot;ecmaFeatures&quot;: {\n        &quot;jsx&quot;: true\n    },\n    &quot;plugins&quot;: [\n        &quot;react&quot;\n    ],\n    &quot;rules&quot;: {}\n}<\/pre>\n<p>In <a href=\"http:\/\/eslint.org\/docs\/2.0.0\/user-guide\/configuring.html#specifying-parser-options\" target=\"_blank\" rel=\"noopener\">ecmaFeatures<\/a> object you can specify additional language features you\u2019d like to use, for example ES6 classes, modules, etc.<\/p>\n<p>In rules object you can list ESLint <a href=\"http:\/\/eslint.org\/docs\/rules\/\" target=\"_blank\" rel=\"noopener\">built-in rules<\/a> that you would like to enable, as well as <a href=\"https:\/\/github.com\/yannickcr\/eslint-plugin-react#list-of-supported-rules\" target=\"_blank\" rel=\"noopener\">rules<\/a> available via the react plugin.<\/p>\n<p>For example, thanks to ESLint with react plugin we can get warnings when the display name is not set for React component, or when some dangerous JSX properties are used. Here\u2019s how it looks in the editor, if you have ESLint integration enabled in WebStorm:<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-7974\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2015\/12\/webstorm-eslint-react.png\" alt=\"eslint-react\" width=\"640\"><\/p>\n<p>To enable ESLint, go to <em>Preferences | Languages &amp; Frameworks | JavaScript | Code quality | ESLint<\/em> (or simply search for ESLint in Preferences) and check the <em>Enable<\/em> checkbox. WebStorm will automatically locate ESLint in your project\u2019s node_modules folder and then use <em>.eslintrc<\/em> configuration by default.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-7975\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2015\/12\/webstorm-eslint-enable.png\" alt=\"eslint-enable\" width=\"640\"><\/p>\n<h2>Refactoring<\/h2>\n<p>WebStorm offers lots of different refactorings to modify and maintain your code. For example, when you rename a file with <em>Refactor -&gt; Rename<\/em>, all the references will be renamed automatically. Or, you can easily rename a variable, class or method throughout your whole project.<\/p>\n<p>For React applications, WebStorm can also help you rename components. Place the cursor on the component name and press <strong>Ctrl+T<\/strong> to open the <em>Refactor This <\/em>popup. Select <em>Rename&#8230;<\/em>, type the new name and press Enter. Done!<\/p>\n<p>Here\u2019s an example of renaming a component that is defined and used in only one file:<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-7976\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2015\/12\/webstorm-rename-component.gif\" alt=\"rename-component\" width=\"640\"><\/p>\n<p>In the same way, you can rename components defined in one file and then imported using a named export to another file:<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-7977\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2015\/12\/webstorm-rename-component-import.gif\" alt=\"rename-component-import\" width=\"640\"><\/p>\n<h2>Compiling the code<\/h2>\n<p>You can set up a build process for your React app in multiple ways. <a href=\"https:\/\/facebook.github.io\/react\/docs\/getting-started.html\" target=\"_blank\" rel=\"noopener\">The React Getting started page<\/a> suggests using <a href=\"http:\/\/browserify.org\/\" target=\"_blank\" rel=\"noopener\">Browserify<\/a> or <a href=\"https:\/\/webpack.github.io\/\" target=\"_blank\" rel=\"noopener\">Webpack<\/a> which are CommonJS module systems. You will also need Babel and, if using Babel 6 and ES6 code, <a href=\"https:\/\/www.npmjs.com\/package\/babel-preset-react\" target=\"_blank\" rel=\"noopener\">babel-preset-react<\/a> and <a href=\"https:\/\/www.npmjs.com\/package\/babel-preset-es2015\" target=\"_blank\" rel=\"noopener\">babel-preset-es2015<\/a> to compile your code. You can find lots of articles and tutorials with recommendations for the build process using various tools.<\/p>\n<p>As the <a href=\"https:\/\/facebook.github.io\/react\/docs\/getting-started.html\" target=\"_blank\" rel=\"noopener\">Getting started tutorial<\/a> suggests, install the following modules via npm:<\/p>\n<p><code>npm install --save react react-dom browserify babelify babel-preset-es2015 babel-preset-react<\/code><\/p>\n<p>To automate the build process a little bit, let\u2019s add the command suggested in the tutorial to the scripts section of the project\u2019s <em>package.json<\/em> file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-linenumbers=\"false\" data-enlighter-title=\"\">&quot;scripts&quot;: {\n    &quot;build&quot;: &quot;browserify -t [ babelify --presets [ react ] ] main.js -o bundle.js&quot;\n}<\/pre>\n<p>where <em>main.js<\/em> is the main app file and <em>bundle.js<\/em> is the output file.<\/p>\n<p>WebStorm displays npm tasks listed in package.json in a separate tool window. Just double-click on the task name to run it. No need to run commands in the terminal.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-7978\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2015\/12\/webstorm-npm-build.png\" alt=\"npm-build\" width=\"640\"><\/p>\n<p>In a similar way you can start <a href=\"https:\/\/youtu.be\/EI62ZhMx4lI\" target=\"_blank\" rel=\"noopener\">Gulp or Grunt tasks<\/a> in WebStorm.<\/p>\n<p>You can also set up a File watcher for Babel and Browserify in WebStorm to execute similar tasks (you can read about it <a href=\"https:\/\/blog.jetbrains.com\/zh-hans\/webstorm\/2015\/05\/ecmascript-6-in-webstorm-transpiling\">here<\/a>), but running tasks via npm scripts or Gulp gives you more flexibility if you add more steps.<\/p>\n<p>More on using React in WebStorm:<\/p>\n<ul>\n<li><a href=\"https:\/\/blog.jetbrains.com\/zh-hans\/webstorm\/2015\/10\/working-with-reactjs-in-webstorm-coding-assistance\">Working with ReactJS in WebStorm: Coding Assistance<\/a><\/li>\n<li><a href=\"https:\/\/blog.jetbrains.com\/zh-hans\/webstorm\/2017\/01\/debugging-react-apps\">Debugging React apps created with Create React App<\/a><\/li>\n<li><a href=\"https:\/\/blog.jetbrains.com\/zh-hans\/webstorm\/2016\/12\/developing-mobile-apps-with-react-native-in-webstorm\">Developing mobile apps with React Native<\/a><\/li>\n<\/ul>\n<p><em>\u2013 JetBrains WebStorm Team<\/em><\/p>\n","protected":false},"author":221,"featured_media":0,"comment_status":"open","ping_status":"open","template":"","categories":[601],"tags":[2812,1290,2805],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/webstorm\/22392"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/webstorm"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/types\/webstorm"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/users\/221"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/comments?post=22392"}],"version-history":[{"count":2,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/webstorm\/22392\/revisions"}],"predecessor-version":[{"id":153699,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/webstorm\/22392\/revisions\/153699"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media?parent=22392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/categories?post=22392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/tags?post=22392"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/cross-post-tag?post=22392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}