<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alex Anthony on Medium]]></title>
        <description><![CDATA[Stories by Alex Anthony on Medium]]></description>
        <link>https://medium.com/@alex.anthony?source=rss-d1cb227f276b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*vddo1qQ5Hwiodrx32eVfSw.jpeg</url>
            <title>Stories by Alex Anthony on Medium</title>
            <link>https://medium.com/@alex.anthony?source=rss-d1cb227f276b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 15:17:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alex.anthony/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Easier code reviews with ESLint and React]]></title>
            <link>https://medium.com/engineering-at-alfa/easier-code-reviews-with-eslint-and-react-153bc30f4817?source=rss-d1cb227f276b------2</link>
            <guid isPermaLink="false">https://medium.com/p/153bc30f4817</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[eslint]]></category>
            <dc:creator><![CDATA[Alex Anthony]]></dc:creator>
            <pubDate>Wed, 27 Mar 2019 10:14:26 GMT</pubDate>
            <atom:updated>2019-03-27T10:14:26.127Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This is an extended write-up of a talk I delivered at the </em><a href="http://meetup.react.london/"><em>React London meetup</em></a><em> — the video is </em><a href="https://www.youtube.com/watch?v=fIk7vW1BLGI&amp;list=PLW6ORi0XZU0BL3Up9mXpP75ilJBDOjMsQ&amp;index=3"><em>here</em></a><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ujtNWmYntlgcvfUp" /><figcaption>Photo by <a href="https://unsplash.com/@nesabymakers?utm_source=medium&amp;utm_medium=referral">NESA by Makers</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>Who uses ESLint?</strong></p><p><a href="https://eslint.org/">ESLint</a> is a pretty common tool in the front end armoury. It’s the industry standard for static code analysis of JavaScript (AKA ECMAScript, hence ESLint). It gets about 7 million downloads a week, which is a lot (for comparison, React gets 5.5 million).</p><p>We use ESLint to avoid errors (e.g. no undefined variables), code in a consistent style (semicolons or not, indentation) and keep our code tidy (no unreachable code).</p><p>Most people use a slight modification of a typical ESLint configuration. ESLint comes with with a set of rules and a default configuration, as does the React ESLint plugin. The <a href="https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb">Airbnb config</a> is also popular.</p><p>However, if you’re just using a standard ESLint config, you’re probably not using the full power of ESLint. You probably have team opinions that are passed from developer to developer, maybe by word of mouth or code reviews. A lot of the time, we could codify those unwritten rules and make them actual rules. ESLint rules.</p><p>That’s what I’m going to go through in the rest of this post — how to write an ESLint plugin and a rule, focusing on writing a rule to use with React code.</p><p><strong>The problem</strong></p><p>To illustrate the rule we’re going to write, I’ll first set out the problem. This example isn’t a major issue, but it was enough that when I spotted it in a code review I passed it back to my teammate. ESLint can do a lot more than this, but it’s a nice example to demonstrate it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e7dc98c0144c7f86d21a9502cbe878e/href">https://medium.com/media/7e7dc98c0144c7f86d21a9502cbe878e/href</a></iframe><p>This bit of code isn’t the actual example, but it illustrates the problem. This sort of conditional logic became much easier when React 16.2 was released and introduced Fragments. They allow two (or more) components to be returned from the same component, or same expression, as siblings, without any extra DOM element.</p><p>This is all lovely until we refactor it, extracting ThatButton and ThatOtherThing into a new component, ThatButtonAndThatOtherThing:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b49f10552fd02be77eb2aacb6bfef8e2/href">https://medium.com/media/b49f10552fd02be77eb2aacb6bfef8e2/href</a></iframe><p>The extra Fragment wrapper isn’t doing anything here. It isn’t doing any actual harm to the component, but it’s just making it slightly untidy.</p><p>I spotted it and asked the developer to tidy it up. But then I remembered developers are supposed to be lazy. I don’t want to have to spot this in a review, I want a computer to catch it, whether in CI or in the original developer’s editor, or both. Sounds like a case for ESLint. But the rule didn’t already exist! (Or at least I couldn’t find it.) So I set about writing my own rule.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*r_-_ZWam5n6mQn0o121-6g.png" /><figcaption>Automation — credit <a href="https://xkcd.com/1319/">XKCD</a></figcaption></figure><p><strong>A new ESLint plugin</strong></p><p>Because this was the first rule we’d written internally, I had to start my own ESLint plugin from scratch. I could also have raised a PR to get it merged into someone else’s plugin, but I wanted to use it straight away!</p><p>Fortunately, there are tools to do most of the setup of an ESLint plugin for you. It’s just these four commands:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7b9ce0af00484b6fe520af21df7acf1/href">https://medium.com/media/c7b9ce0af00484b6fe520af21df7acf1/href</a></iframe><p>This is using a tool called <a href="https://yeoman.io/">Yeoman</a> (“yo”), which you may have come across. It’s a tool for scaffolding new projects, based on generators which are elaborate templates. We’re using the <a href="https://github.com/eslint/generator-eslint">ESLint generator</a>, cleverly titled generator-eslint. We install these globally as they’re tools to set up our project, not part of the project itself.</p><p>We then run the generator with the “:plugin” suffix to tell it to create a plugin. It then asks a couple of questions, like the package name and description, and sets up an empty project.</p><p>Unfortunately the generator is a little out of date, so the first thing I did in the empty project was upgrade it to the version of ESLint we’re using in our project.</p><p><strong>A new rule</strong></p><p>Creating a rule is even easier. Again, this is using the yeoman generator, but this time for a rule.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f5db4cb584213ff2809f17987b4528c/href">https://medium.com/media/9f5db4cb584213ff2809f17987b4528c/href</a></iframe><p>One of the questions it asks is for an example of code that will fail the rule, so I went for the simplest example I could think of:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed883d1136ea059eb3fa5f4ee0512967/href">https://medium.com/media/ed883d1136ea059eb3fa5f4ee0512967/href</a></iframe><p><strong>How does ESLint work?</strong></p><p>ESLint works by parsing our source code files and turning it into an Abstract Syntax Tree, or AST. This is basically a representation of the source code as a tree of objects. Here’s a very simple example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72058b0b847fce947dc76bbde15f8343/href">https://medium.com/media/72058b0b847fce947dc76bbde15f8343/href</a></iframe><p>would correspond to</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1da342811823fa9ec343f06716faf920/href">https://medium.com/media/1da342811823fa9ec343f06716faf920/href</a></iframe><p>The simple variable declaration snippet gives this AST (this is a slight simplification in that I removed some noise, but it illustrates the concept. We have an object with type “VariableDeclaration”, with kind “const”. This has an identifier (id of type “Identifier”) with the name “val” and an initial value, which is a “BinaryExpression” made up of two literals and an operation.</p><p>ASTs beyond this simple example can get quite large quite quickly, but there are some awesome tools to explore them. One of these is <a href="http://www.astexplorer.net/">AST Explorer</a>, which can be set up to use a range of parsers (including <a href="https://github.com/eslint/espree">espree</a>, which ESLint uses). We can type whatever code we want, then click on it to highlight the corresponding elements in the AST on the right.</p><p>To write our rule, we just need to look for the particular pattern in the AST and report an issue if we find it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/881/0*ERna6ExoMlK271yq" /><figcaption>The Awesome AST Explorer</figcaption></figure><p>This example, which corresponds to our problem, shows that the Fragment is a JSXElement with an openingElement of type JSXOpeningElement.</p><p><strong>Test-Driven Development</strong></p><p>Writing ESLint rules is a great fit for TDD. We can write an example of code to check, then iterate on the rule until it gives the right outcome, and repeat. Fortunately, the yeoman generator has already set up a test for us.</p><p>ESLint comes with a great test framework using an import called RuleTester. The generator added mocha to run these tests, but they work fine with any JS unit test library that supports the “describe” and “it” keywords. Because I’m used to it, and because we’ve already got it installed in the monorepo this sits in, I switched this to use <a href="https://jestjs.io/">Jest</a>. I also set up <a href="https://babeljs.io/">Babel</a> so that ESLint can understand our JSX.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c14ab16588c697039b0a745f284e4c93/href">https://medium.com/media/c14ab16588c697039b0a745f284e4c93/href</a></iframe><p>This is the unit test that the generator set up (with me having added parser: ‘babel-eslint’ to make it run):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2d543ca3ae1c733f5fbc1a594cd1387/href">https://medium.com/media/f2d543ca3ae1c733f5fbc1a594cd1387/href</a></iframe><p>This is using that snippet I provided when I set up the rule. The rule tester will run my rule with this code and check that I get the right set of errors. The “type” is used to check that it is assigning the error to the right element, which is useful when you have ESLint integration in your text editor. We can change the message to the sensible error we want (“Remove redundant Fragment”).</p><p>Now we can just put Jest in watch mode and it will keep rerunning our test as we edit the rule.</p><p><strong>Writing the rule</strong></p><p>ESLint rules are based on the visitor pattern. In practice, this means we return an object where the keys are node types (from the AST) and the values are functions to run to check that node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/0*6Me0J0SW_mH9hN7E" /><figcaption>The AST from AST Explorer in more detail</figcaption></figure><p>We saw in AST explorer that our Fragment in JSX was a node with type JSXElement. So to begin with, let’s look for JSXElement nodes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38bcbe6dfa652026a4a92d2f9e6d9adb/href">https://medium.com/media/38bcbe6dfa652026a4a92d2f9e6d9adb/href</a></iframe><p>This is all a rule is — some metadata and a function to create our rule. Context is what we use to report the error. For now, we’re reporting every JSXElement as invalid.</p><p>If we look at the test, it’s not passing because the node type is incorrect. We’re reporting against the JSXElement, so we can just change our test expect an error against the JSXElement (just pass this as a string), and we’re good!</p><p><strong>Refining the rule</strong></p><p>Our test is passing, but we know that our rule is a bit overzealous — it’s currently reporting any use of Fragments. We can now add a new test which our current rule will fail, with a simple piece of valid code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/446c9df78a48adb12a188081c4db1cb9/href">https://medium.com/media/446c9df78a48adb12a188081c4db1cb9/href</a></iframe><p>For the valid case, we don’t have to provide an expected error obviously. This test is now failing. The logic we need is “the Fragment is valid if we have two or more children”. Back to the AST to work out how to do that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/701/0*Fu5dnhK7EQ5vCO_8" /></figure><p>We have an array of children, which makes this logic really simple — if that array has length &gt;= 2, the Fragment is fine. Let’s put that in code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8bc269566071a7cf081b7484ecdf99c4/href">https://medium.com/media/8bc269566071a7cf081b7484ecdf99c4/href</a></iframe><p>This is basically the cycle of refining the rule. Identify a case that the rule doesn’t handle properly, write a test for it, then tweak the rule to handle it.</p><p>In our version, we’ve added a few more tweaks:</p><ul><li>Rather than reporting against the JSXElement, we can report against the JSXOpeningElement. This looks nicer in your text editor.</li><li>When the code is properly indented, the whitespace is included in the array of children. We want to ignore that, so we filter the list of children.</li><li>Fragments can be called as React.Fragment or as the &lt;&gt;&lt;/&gt; shorthand in newer babel versions. We should handle that.</li></ul><p>If we apply the necessary tweaks for this and split it up into sensible functions for readability, we get something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7845bdb86ebc02c4b2e6d3bacbab0694/href">https://medium.com/media/7845bdb86ebc02c4b2e6d3bacbab0694/href</a></iframe><p><strong>Fix the code</strong></p><p>Another handy feature of ESLint is that in some cases it can automatically fix the code. The general guidance for this is to do as little as possible to fix your rule, and leave other bits to other plugins. We don’t try to reformat the code or remove an unused import, we just do what we need to to fix our error.</p><p>In our case, fixing the code is just deleting the Fragment. When we report an error, we can provide a fixing function to do this. This function receives a “fixer” as a parameter, which we use to interact with the code. To fix our error, we just use the fixer to remove the opening and closing elements. This will leave the code badly formatted, but we can leave that to something else (Prettier) to sort out.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53b99cbff107220e7c69a9dd6ae2be85/href">https://medium.com/media/53b99cbff107220e7c69a9dd6ae2be85/href</a></iframe><p><strong>Use the rule</strong></p><p>We need to add the rule to our ESLint config to use it.</p><p>We can publish our plugin package to NPM, or to an internal registry, or just install it as a local filepath or git repo URL. We then need to tell ESLint to load up our plugin and treat our rule as an error.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ad354ed141533f5ea241150e0e2de16/href">https://medium.com/media/4ad354ed141533f5ea241150e0e2de16/href</a></iframe><p>First we load up the plugin, using the shorthand where we leave our “eslint-plugin-” from the package name. Then we configure our rule (prefixed with the plugin name) to be treated as an error — 0 is accept, 1 is warn, 2 is error.</p><p><strong>It works!</strong></p><p>And finally, here is a screenshot of the error in practice in my text editor!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/0*-5jvSc_dFDbZSdP2" /><figcaption>Error appearing nicely in my editor</figcaption></figure><p>Now all the developers on my team can quickly see when they need to tidy up a Fragment like this and we don’t depend on spotting this in a code review. Once you’ve written one rule, writing more is easy, and the accumulation of these rules will make the entire codebase follow a more standardised pattern.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=153bc30f4817" width="1" height="1" alt=""><hr><p><a href="https://medium.com/engineering-at-alfa/easier-code-reviews-with-eslint-and-react-153bc30f4817">Easier code reviews with ESLint and React</a> was originally published in <a href="https://medium.com/engineering-at-alfa">Engineering at Alfa</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Welcome to the new Alfa Engineering blog!]]></title>
            <link>https://medium.com/engineering-at-alfa/welcome-to-the-new-alfa-engineering-blog-bc5f41e22178?source=rss-d1cb227f276b------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc5f41e22178</guid>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Alex Anthony]]></dc:creator>
            <pubDate>Tue, 19 Mar 2019 16:43:13 GMT</pubDate>
            <atom:updated>2019-03-20T16:49:55.530Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nB4LmwoxmWoTJpfMQX_7pA.png" /></figure><p><strong>Who are we?</strong></p><p>We’re Alfa, the makers of Alfa Systems — the number one software choice for asset finance companies, worldwide. The example of asset finance people are most familiar with is leasing a car, but our customers use Alfa Systems to finance an incredible range of assets — from planes, trains and automobiles to industrial equipment and machinery.</p><p><strong>What is Alfa Systems?</strong></p><p>Alfa Systems, our class-leading platform, is built with Java on the server side and JavaScript in the front end. We use a mixture of open-source libraries and homegrown tools to meet the demands of the modern asset finance industry. We’d expect more details of our stack to become clear in future posts.</p><p><strong>What is the Alfa Engineering blog?</strong></p><p>Delivering leading-edge technology is enshrined in our purpose and is at the core of our values. As a software provider for the asset finance industry, we face an unusual set of challenges. We’d like to use this blog to share details of how we’re meeting these challenges, and our opinions on the latest software trends.</p><p>Thank you for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc5f41e22178" width="1" height="1" alt=""><hr><p><a href="https://medium.com/engineering-at-alfa/welcome-to-the-new-alfa-engineering-blog-bc5f41e22178">Welcome to the new Alfa Engineering blog!</a> was originally published in <a href="https://medium.com/engineering-at-alfa">Engineering at Alfa</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>