{"id":1318,"date":"2024-10-26T07:23:15","date_gmt":"2024-10-26T07:23:15","guid":{"rendered":"https:\/\/aki-hamano.blog\/?p=1318"},"modified":"2025-10-25T03:52:39","modified_gmt":"2025-10-25T03:52:39","slug":"introduce-eslint-to-the-block-development","status":"publish","type":"post","link":"https:\/\/aki-hamano.blog\/en\/2024\/10\/26\/introduce-eslint-to-the-block-development\/","title":{"rendered":"Introduce ESLint to the block development"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this article, I would like to explain how to introduce ESLint to improve the quality of JavaScript code and standardize coding style in WordPress block development.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why ESLint?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">No matter how many spaces there are before and after the brackets, where you break lines, or what variable names you use, as long as the code is all correct, the block will work correctly. No warnings or errors will be displayed in the browser console. If you are the only one who will read the code and you have not exposed it anywhere, that may not be a problem.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, if you expose the plugin on GitHub or somewhere else, someone may become interested in the plugin, read the code, or write code and submit a pull request. Also, if multiple developers are involved in one block, you will see code written by developers other than yourself.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">How would you feel if the coding rules were not unified in such a situation? It may be difficult to read, and it may cause bugs to be introduced.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create a Block Template<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before explaining about ESLint, let&#8217;s first create a template for blocks. You can create each file by yourself, but there is a convenient library called <code>@wordpress\/create-block<\/code>, so we will use it to create a template:<\/p>\n\n\n\n<pre class=\"wp-block-code lang-bash\"><code>npx @wordpress\/create-block gutenpride<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you open the directory called <code>gutenpride<\/code>, it should consist of the following files:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"330\" height=\"643\" src=\"https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-file-structure.png\" alt=\"create-block \u306b\u3088\u3063\u3066\u4f5c\u6210\u3055\u308c\u308b\u30d5\u30a1\u30a4\u30eb\" class=\"wp-image-1190\" style=\"object-fit:cover\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">EditorConfig<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Looking at the file structure, we can see that there is a file called <code>.editorconfig<\/code>. Before introducing ESLint, I would like to explain EditorConfig, which is another important setting for unifying coding style.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As one reason for introducing EditorConfig, I would like to give the following example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Multiple developers are involved in one plugin.<\/li>\n\n\n\n<li>Developer A has set the indentation to two half-width spaces in the default settings of his code editor.<\/li>\n\n\n\n<li>Developer B has set the indentation to tabs in the default settings of his code editor, with an indentation width of four characters.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose Developer A wrote the following code. Note that all the indentations are spaces.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"840\" height=\"256\" src=\"https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-space-indent-840x256.png\" alt=\"2\u30b9\u30da\u30fc\u30b9\u30a4\u30f3\u30c7\u30f3\u30c8\u3092\u6301\u3064\u30b3\u30fc\u30c9\" class=\"wp-image-1205\" srcset=\"https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-space-indent-840x256.png 840w, https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-space-indent-768x234.png 768w, https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-space-indent.png 1077w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Developer B opens the file with this code in his or her code editor, removes the indentation of 34 lines, and then <em>visually<\/em> rearranges the indentation so that it is correct.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"840\" height=\"201\" src=\"https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-tab-indent-840x201.png\" alt=\"\u4e00\u90e8\u306b\u30bf\u30d6\u30a4\u30f3\u30c7\u30f3\u30c8\u3092\u6301\u3064\u30b3\u30fc\u30c9\" class=\"wp-image-1203\" srcset=\"https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-tab-indent-840x201.png 840w, https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-tab-indent-768x184.png 768w, https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-tab-indent.png 913w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">However, if Developer A reopens the file, he or she sees that the indentation on line 34 is visually incorrect.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"840\" height=\"247\" src=\"https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-space-indent-with-tab-840x247.png\" alt=\"\u4e00\u90e8\u306b\u30bf\u30d6\u30a4\u30f3\u30c7\u30f3\u30c8\u3092\u6301\u3064\u30b3\u30fc\u30c9\" class=\"wp-image-1204\" srcset=\"https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-space-indent-with-tab-840x247.png 840w, https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-space-indent-with-tab-768x226.png 768w, https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-space-indent-with-tab.png 1076w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Some code editors may automatically detect the indentation of a file, but it is not guaranteed that the indentation will be visually consistent between all developers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One reason to introduce EditorConfig is to prevent such differences and unify coding style within a project.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">EditorConfig is supported by a large number of code editors. For some code editors, you may need to install a plugin. Check the following page to see if your code editor requires an additional plugin:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/editorconfig.org\/#pre-installed\" target=\"_blank\" rel=\"noreferrer noopener\">EditorConfig &#8211; No Plugin Necessary<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/editorconfig.org\/#download\" target=\"_blank\" rel=\"noreferrer noopener\">EditorConfig &#8211; Download a Plugin<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">EditorConfig for create-block<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s take a look at the <code>.editorconfig<\/code> settings created by create-block.<\/p>\n\n\n\n<pre data-label=\".editorconfig\" id=\".editorconfig\" class=\"wp-block-code lang-editorconfig\"><code># This file is for unifying the coding style for different editors and IDEs\n# editorconfig.org\n\n# WordPress Coding Standards\n# https:\/\/make.wordpress.org\/core\/handbook\/coding-standards\/\n\nroot = true\n\n&#91;*]\ncharset = utf-8\nend_of_line = lf\ninsert_final_newline = true\ntrim_trailing_whitespace = true\nindent_style = tab\n\n&#91;*.{yml,yaml}]\nindent_style = space\nindent_size = 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Looking at the settings in this file, for example, we can see that the indentation rule mentioned above is set to &#8220;tab indent.&#8221; You can customize these rules to your liking, but the important thing is <strong>whether the coding style is consistent across the entire project based on the rules<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I strongly recommend that you define rules using EditorConfig before introducing ESLint.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Check JavaScript Code with ESlint<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Returning to the topic of ESLint, let&#8217;s say you update the <code>Edit<\/code> component of a block to store the text the block displays in the <code>my_variable<\/code> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code lang-js\"><code>export default function Edit() {\n\tconst my_variable = __( 'Gutenpride \u2013 hello from the editor!', 'gutenpride' );\n\treturn (\n\t\t&lt;p { ...useBlockProps() }&gt;\n\t\t\t{ my_variable }\n\t\t&lt;\/p&gt;\n\t);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you create a template block using create-block, a mechanism for analyzing such code using ESLint is included by default. Let&#8217;s try running the <code>lint:js<\/code> script defined in the <code>scripts<\/code> field of <code>package.json<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code lang-bash\"><code>npm run lint:js<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When you run this script, you should see an error similar to the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"840\" height=\"184\" src=\"https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/lint-error-840x184.png\" alt=\"lint:js \u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u305f\u5f8c\u306b\u8868\u793a\u3055\u308c\u308b\u30a8\u30e9\u30fc\" class=\"wp-image-1307\" srcset=\"https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/lint-error-840x184.png 840w, https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/lint-error-768x168.png 768w, https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/lint-error.png 1474w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This error tells us that the code does not follow two rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The variable name <code>my_variable<\/code> is not in <a href=\"https:\/\/en.wikipedia.org\/wiki\/Camel_case\" target=\"_blank\" rel=\"noreferrer noopener\">camel case<\/a><\/li>\n\n\n\n<li>Line breaks and indentation are not correct<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Fix Lint Errors Automatically<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">So, to fix these errors, do we have to fix them all manually and run <code>lint:js<\/code> over and over again until the errors are fixed?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Like <code>lint:js<\/code>, create-block has the <code>format<\/code> script to fix these errors to some extent automatically.<\/p>\n\n\n\n<pre class=\"wp-block-code lang-bash\"><code>$ npm run format\n\n&gt; gutenpride@0.1.0 format\n&gt; wp-scripts format\n\npackage-lock.json 129ms\npackage.json 1ms\nsrc\/block.json 5ms\nsrc\/edit.js 10ms\nsrc\/index.js 3ms\nsrc\/save.js 2ms\nsrc\/view.js 2ms<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When you run this command, one of the two errors that was warned about, &#8220;errors related to line breaks and indentation,&#8221; will be automatically corrected.<\/p>\n\n\n\n<pre class=\"wp-block-code lang-js\"><code>export default function Edit() {\n\tconst my_variable = __(\n\t\t'Gutenpride \u2013 hello from the editor!',\n\t\t'gutenpride'\n\t);\n\treturn &lt;p { ...useBlockProps() }&gt;{ my_variable }&lt;\/p&gt;;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Fix Lint Errors Manually<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">However, errors in variable names are not automatically fixed. The <code>format<\/code> command does not automatically fix all errors, so you must manually fix any remaining errors. Change the variable name to camel case (<code>myVariable<\/code>) as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code lang-js\"><code>export default function Edit() {\n\tconst myVariable = __(\n\t\t'Gutenpride \u2013 hello from the editor!',\n\t\t'gutenpride'\n\t);\n\treturn &lt;p { ...useBlockProps() }&gt;{ myVariable }&lt;\/p&gt;;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you run <code>npm run lint:js<\/code> you should be able to confirm that all errors have been resolved.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Where are These ESLint Rules Defined?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Why is my code being analyzed based on certain rules even though I haven&#8217;t defined any ESLint rules myself?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When a file template is created with create-block, a library called <code>@wordpress\/scripts<\/code> is installed by default, and ESLint rules are defined in a library that this library depends on. That library is <code>@wordpress\/eslint-plugin<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using the <code>npm ls<\/code> command, you can see that <code>@wordpress\/scripts<\/code> depends on <code>@wordpress\/eslint-plugin<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code lang-bash\"><code>$ npm ls @wordpress\/eslint-plugin\ngutenpride@0.1.0 \/home\/username\/projects\/gutenpride\n\u2514\u2500\u252c @wordpress\/scripts@30.3.0\n  \u2514\u2500\u2500 @wordpress\/eslint-plugin@21.3.0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When you run <code>lint:js<\/code>, that is, the <code>wp-scripts lint-js<\/code> script, your code will be analyzed based on the rules defined in <code>@wordpress\/eslint-plugin<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also see that ESLint itself exists as a dependency.<\/p>\n\n\n\n<pre class=\"wp-block-code lang-bash\"><code>$ npm ls eslint\ngutenpride@0.1.0 \/home\/username\/projects\/gutenpride\n\u2514\u2500\u252c @wordpress\/scripts@30.3.0\n  \u251c\u2500\u252c @wordpress\/eslint-plugin@21.3.0\n  \u2502 \u251c\u2500\u252c @babel\/eslint-parser@7.25.7\n  \u2502 \u2502 \u2514\u2500\u2500 eslint@8.57.1 deduped\n  \u2502 \u251c\u2500\u252c @typescript-eslint\/eslint-plugin@6.21.0\n  \u2502 \u2502 \u251c\u2500\u252c @typescript-eslint\/type-utils@6.21.0\n  \u2502 \u2502 \u2502 \u2514\u2500\u2500 eslint@8.57.1 deduped\n  \u2502 \u2502 \u251c\u2500\u252c @typescript-eslint\/utils@6.21.0\n  \u2502 \u2502 \u2502 \u2514\u2500\u2500 eslint@8.57.1 deduped\n  \u2502 \u2502 \u2514\u2500\u2500 eslint@8.57.1 deduped\n  \u2502 \u251c\u2500\u252c @typescript-eslint\/parser@6.21.0\n  \u2502 \u2502 \u2514\u2500\u2500 eslint@8.57.1 deduped\n  \u2502 \u251c\u2500\u252c eslint-config-prettier@8.10.0\n  \u2502 \u2502 \u2514\u2500\u2500 eslint@8.57.1 deduped\n  \u2502 \u251c\u2500\u252c eslint-plugin-import@2.31.0\n  \u2502 \u2502 \u2514\u2500\u2500 eslint@8.57.1 deduped\n  \u2502 \u251c\u2500\u252c eslint-plugin-jest@27.9.0\n  \u2502 \u2502 \u251c\u2500\u252c @typescript-eslint\/utils@5.62.0\n  \u2502 \u2502 \u2502 \u2514\u2500\u2500 eslint@8.57.1 deduped\n  \u2502 \u2502 \u2514\u2500\u2500 eslint@8.57.1 deduped\n  \u2502 \u251c\u2500\u252c eslint-plugin-jsdoc@46.10.1\n  \u2502 \u2502 \u2514\u2500\u2500 eslint@8.57.1 deduped\n  \u2502 \u251c\u2500\u252c eslint-plugin-jsx-a11y@6.10.1\n  \u2502 \u2502 \u2514\u2500\u2500 eslint@8.57.1 deduped\n  \u2502 \u251c\u2500\u252c eslint-plugin-playwright@0.15.3\n  \u2502 \u2502 \u2514\u2500\u2500 eslint@8.57.1 deduped\n  \u2502 \u251c\u2500\u252c eslint-plugin-prettier@5.2.1\n  \u2502 \u2502 \u2514\u2500\u2500 eslint@8.57.1 deduped\n  \u2502 \u251c\u2500\u252c eslint-plugin-react-hooks@4.6.2\n  \u2502 \u2502 \u2514\u2500\u2500 eslint@8.57.1 deduped\n  \u2502 \u251c\u2500\u252c eslint-plugin-react@7.37.2\n  \u2502 \u2502 \u2514\u2500\u2500 eslint@8.57.1 deduped\n  \u2502 \u2514\u2500\u2500 eslint@8.57.1 deduped\n  \u2514\u2500\u252c eslint@8.57.1\n    \u2514\u2500\u252c @eslint-community\/eslint-utils@4.4.1\n      \u2514\u2500\u2500 eslint@8.57.1 deduped<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Extend ESLint Rules<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Of course, you can develop based only on these default rules. However, some developers may want to change some of the rules to suit their preferences or apply stricter rules.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To change or extend these default rules, create a file called <code>.eslintrc<\/code> in the root directory of your project and write the following code in it:<\/p>\n\n\n\n<pre data-label=\".eslintrc\" id=\".eslintrc\" class=\"wp-block-code lang-json\"><code>{\n\t\"extends\": &#91; \"plugin:@wordpress\/eslint-plugin\/recommended\" ]\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When you run the <code>lint:js<\/code> script, your code will be analyzed based on the rules defined here.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Tweak Existing Rules<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Try updating this file to change an existing rule.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, say you wrote the following code to use an API prefixed with <code>__experimental<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note: this is just an example; there is no library called <code>@wordpress\/foo<\/code> and no API called <code>__experimentalFeature<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code lang-js\"><code>import { __experimentalFeature } from '@wordpress\/foo';\n\nexport default function Edit() {\n\tconst myVariable = __(\n\t\t'Gutenpride \u2013 hello from the editor!',\n\t\t'gutenpride'\n\t);\n\treturn &lt;p { ...useBlockProps() }&gt;{ myVariable }&lt;\/p&gt;;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When you run <code>lint:js<\/code> you will get the following two errors:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"840\" height=\"136\" src=\"https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/lint-error-experimental-feature-840x136.png\" alt=\"lint:js \u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u305f\u5f8c\u306b\u8868\u793a\u3055\u308c\u308b\u3001\u5b9f\u9a13\u7684\u306a API \u306b\u95a2\u3059\u308b\u30a8\u30e9\u30fc\" class=\"wp-image-1241\" srcset=\"https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/lint-error-experimental-feature-840x136.png 840w, https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/lint-error-experimental-feature-768x124.png 768w, https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/lint-error-experimental-feature.png 1477w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Of these two errors, pay attention to the error that says &#8220;Usage of <code>__experimentalFeature<\/code> from <code>@wordpress\/foo<\/code> is not allowed.&#8221; This rule is defined by default in <code>@wordpress\/eslint-plugin<\/code>, and you can find more information about the rule in <a href=\"https:\/\/github.com\/WordPress\/gutenberg\/blob\/trunk\/packages\/eslint-plugin\/docs\/rules\/no-unsafe-wp-apis.md\" target=\"_blank\" rel=\"noreferrer noopener\">this README<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To disable this rule for the entire project, change the <code>.eslintrc<\/code> file as follows:<\/p>\n\n\n\n<pre data-label=\".eslintrc\" id=\".eslintrc\" class=\"wp-block-code lang-json\"><code>{\n\t\"extends\": &#91; \"plugin:@wordpress\/eslint-plugin\/recommended\" ],\n\t\"rules\": {\n\t\t\"@wordpress\/no-unsafe-wp-apis\": \"off\"\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you run the <code>lint:js<\/code> script again, you should see that the errors related to APIs with the <code>__experimental<\/code> prefix disappear.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This rule is also <a href=\"https:\/\/github.com\/WordPress\/gutenberg\/blob\/2a021834d58a0d2734194714d2777d15d16c065e\/.eslintrc.js#L188\" target=\"_blank\" rel=\"noreferrer noopener\">disabled in the Gutenberg project<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Add New Rules<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">I would like to introduce some rules that I personally recommend to improve the code quality and security of block development. These rules are not included in the default rules of <code>@wordpress\/eslint-plugin<\/code>, so you need to add them explicitly.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/jsx-eslint\/eslint-plugin-react\/blob\/d1556a3d2f95f56cfb6e7c0a341af16742f64033\/docs\/rules\/jsx-boolean-value.md\" target=\"_blank\" rel=\"noreferrer noopener\">react\/jsx-boolean-value<\/a><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In React, enforces rules when the value of a prop is a <code>boolean<\/code> and <code>true<\/code>.<\/p>\n\n\n\n<pre data-label=\".eslintrc\" id=\".eslintrc\" class=\"wp-block-code lang-json\"><code>{\n\t\"extends\": &#91; \"plugin:@wordpress\/eslint-plugin\/recommended\" ],\n\t\"rules\": {\n\t\t\"react\/jsx-boolean-value\": \"error\"\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Do<\/p>\n\n\n\n<pre class=\"wp-block-code lang-js\"><code>const Hello = &lt;Hello myProp \/&gt;;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u274c Don&#8217;t<\/p>\n\n\n\n<pre class=\"wp-block-code lang-js\"><code>const Hello = &lt;Hello myProp={ true } \/&gt;;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/WordPress\/gutenberg\/blob\/0466f7d510ef5185d054b3d30b19f21ed9ad986b\/packages\/eslint-plugin\/docs\/rules\/i18n-text-domain.md\" target=\"_blank\" rel=\"noreferrer noopener\">@wordpress\/i18n-text-domain<\/a><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This forces you to add the correct text domain to your translation functions. Change the value of the <code>allowedTextDomain<\/code> field to suit your project.<\/p>\n\n\n\n<pre data-label=\".eslintrc\" id=\".eslintrc\" class=\"wp-block-code lang-json\"><code>{\n\t\"extends\": &#91; \"plugin:@wordpress\/eslint-plugin\/recommended\" ],\n\t\"rules\": {\n\t\t\"@wordpress\/i18n-text-domain\": &#91;\n\t\t\t\"error\",\n\t\t\t{\n\t\t\t\t\"allowedTextDomain\": \"gutenpride\"\n\t\t\t}\n\t\t]\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Do<\/p>\n\n\n\n<pre class=\"wp-block-code lang-js\"><code>export default function Edit() {\n\tconst myVariable = __(\n\t\t'Gutenpride \u2013 hello from the editor!',\n\t\t'gutenpride'\n\t);\n\treturn &lt;p { ...useBlockProps() }&gt;{ myVariable }&lt;\/p&gt;;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u274c Don&#8217;t<\/p>\n\n\n\n<pre class=\"wp-block-code lang-js\"><code>export default function Edit() {\n\tconst myVariable = __(\n\t\t'Gutenpride \u2013 hello from the editor!'\n\t);\n\treturn &lt;p { ...useBlockProps() }&gt;{ myVariable }&lt;\/p&gt;;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/WordPress\/gutenberg\/blob\/0466f7d510ef5185d054b3d30b19f21ed9ad986b\/packages\/eslint-plugin\/docs\/rules\/dependency-group.md\" target=\"_blank\" rel=\"noreferrer noopener\">@wordpress\/dependency-group<\/a><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When you import code from another source, it forces you to group them properly and add comments.<\/p>\n\n\n\n<pre data-label=\".eslintrc\" id=\".eslintrc\" class=\"wp-block-code lang-json\"><code>{\n\t\"extends\": &#91; \"plugin:@wordpress\/eslint-plugin\/recommended\" ],\n\t\"rules\": {\n\t\t\"@wordpress\/dependency-group\": \"error\"\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Do<\/p>\n\n\n\n<pre class=\"wp-block-code lang-js\"><code>\/*\n * External dependencies\n *\/\nimport { camelCase } from 'change-case';\n\n\/*\n * WordPress dependencies\n *\/\nimport { Component } from 'react';\n\n\/*\n * Internal dependencies\n *\/\nimport edit from '.\/edit';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u274c Don&#8217;t<\/p>\n\n\n\n<pre class=\"wp-block-code lang-js\"><code>import { camelCase } from 'change-case';\nimport { Component } from 'react';\nimport edit from '.\/edit';<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">.eslintrc with the Rules Applied So Far<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">With all the changed and new rules applied to your <code>.eslintrc<\/code>, you should end up with something like this: Use this code as a guide to find the rules that best suit your project and preferences.<\/p>\n\n\n\n<pre data-label=\".eslintrc\" id=\".eslintrc\" class=\"wp-block-code lang-json\"><code>{\n\t\"extends\": &#91; \"plugin:@wordpress\/eslint-plugin\/recommended\" ],\n\t\"rules\": {\n\t\t\"@wordpress\/no-unsafe-wp-apis\": \"off\",\n\t\t\"react\/jsx-boolean-value\": \"error\",\n\t\t\"@wordpress\/i18n-text-domain\": &#91;\n\t\t\t\"error\",\n\t\t\t{\n\t\t\t\t\"allowedTextDomain\": \"gutenpride\"\n\t\t\t}\n\t\t],\n\t\t\"@wordpress\/dependency-group\": \"error\"\n\t}\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Detect Lint Rule Violations in the Code Editor<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So far, we have fixed Lint errors and changed\/added rules. But does that mean we need to run the <code>lint:js<\/code> or <code>format<\/code> script every time we update our code to detect and fix these errors?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Many code editors automatically read the ESLint rules in your project and detect them as errors in the code editor, or automatically fix code that violates the rules when you save the file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, I&#8217;ll introduce the method I usually use in VSCode.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, install <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=dbaeumer.vscode-eslint\" target=\"_blank\" rel=\"noreferrer noopener\">the ESLint extension<\/a> in VSCode and make sure ESLint is enabled in the user or workspace settings.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"840\" height=\"350\" src=\"https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-extension-840x350.png\" alt=\"\u30e6\u30fc\u30b6\u30fc\u8a2d\u5b9a\u3067\u6709\u52b9\u5316\u3055\u308c\u3066\u3044\u308b ESLint \u62e1\u5f35\" class=\"wp-image-1273\" srcset=\"https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-extension-840x350.png 840w, https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-extension-768x320.png 768w, https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/eslint-extension.png 902w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If the ESLint extension is enabled and you have code that violates a rule, you should see a warning like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"840\" height=\"266\" src=\"https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/vscode-eslint-error-840x266.png\" alt=\"ESLint \u62e1\u5f35\u304c\u767a\u3059\u308b\u8b66\u544a\" class=\"wp-image-1277\" srcset=\"https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/vscode-eslint-error-840x266.png 840w, https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/vscode-eslint-error-768x243.png 768w, https:\/\/aki-hamano.blog\/wp-content\/uploads\/2024\/10\/vscode-eslint-error.png 1476w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Documents<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.wordpress.org\/block-editor\/reference-guides\/packages\/packages-scripts\/\" target=\"_blank\" rel=\"noreferrer noopener\">@wordpress\/scripts \u2013 Block Editor Handbook | Developer.WordPress.org<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.wordpress.org\/block-editor\/reference-guides\/packages\/packages-eslint-plugin\/\" target=\"_blank\" rel=\"noreferrer noopener\">@wordpress\/eslint-plugin \u2013 Block Editor Handbook | Developer.WordPress.org<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/editorconfig.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">EditorConfig<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this article, I would like to explain how to introduce ESLint to improve the quality of JavaScript code and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"https:\/\/aki-hamano.blog\/?p=1187","footnotes":""},"categories":[21],"tags":[],"class_list":["post-1318","post","type-post","status-publish","format-standard","hentry","category-block-development","en-US"],"_links":{"self":[{"href":"https:\/\/aki-hamano.blog\/wp-json\/wp\/v2\/posts\/1318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aki-hamano.blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aki-hamano.blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aki-hamano.blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aki-hamano.blog\/wp-json\/wp\/v2\/comments?post=1318"}],"version-history":[{"count":35,"href":"https:\/\/aki-hamano.blog\/wp-json\/wp\/v2\/posts\/1318\/revisions"}],"predecessor-version":[{"id":1356,"href":"https:\/\/aki-hamano.blog\/wp-json\/wp\/v2\/posts\/1318\/revisions\/1356"}],"wp:attachment":[{"href":"https:\/\/aki-hamano.blog\/wp-json\/wp\/v2\/media?parent=1318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aki-hamano.blog\/wp-json\/wp\/v2\/categories?post=1318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aki-hamano.blog\/wp-json\/wp\/v2\/tags?post=1318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}