How do the rules of Javascript Lant work (and why summarize syntax trees)

by SkillAiNest

Before I started to contribute ESLANT PLAGIN RACTI didn’t think too deeply about the lunters used every day while writing the code. Like many developers, I installed them at the beginning of a project, praised red colors or auto -fixes, and proceeded.

But behind these helpful messages there is a powerful system of rules and structures that most of us rarely discover.

Lant is everywhere – in languages, framework and workflows. They help catch mistakes, enforce permanent formatting and promote the best methods. They are the first tools that we install in a new project, and they are still the most influential and the least considered.

In this article, I will take you down the pole. We will see how the rules of JavaScript Lant work, why ASTS (abstract syntax trees) are such a big deal, and how you can use this understanding to write or contribute to yourself.

📚 the table of content

lin what is a linter?

A lanter is a device that automatically analyzes your code for errors, enforcing styling rulesFor, for, for,. And catch the potential insects. Think of it as the world of coding world – helping you to write a clean, more permanent code by identifying problems soon.

Is a famous example AscentAn open source lighter for JavaScript and type script that checks the code for problems and can even auto -fix some of them.

Launters are often:

  • Merged with your text editor or IDE

  • Run as part of a CI pipeline or pre -commit hook

  • Used along with formators such as pretares for tight consistency

But how will they decide what to flag as a problem? There Lint rules Come in

🧱 Lant Rules: The brain behind the lanter

Lant rules are blocks of any lanter building. Explanation of each rule:

  1. Find what: A specific sample in your code.

  2. What to do about it: A warning, a mistake, or auto -fixed.

There are many principles, often groups in category such as:

  • Prevention of error: Catching insects, such as using unannounced variables.

  • Code styling: Permanent formatting and implementing name conventions.

  • Best practice: Encouraging safe or more reading coding patterns.

  • Hello: Flaging dangerous code, such as straight eval() Calls or unsafe regracts.

If you have ever seen a message like this:

Unexpected console.log

Missing semicolon

'myVar' is assigned a value but never used

… You have practically seen the rules of the Lant.

They are not justStyling police. Lantras help reduce the mental overhead by grabbing very little problems quickly, so you can focus on what your code is trying to do.

From CODE code to tree: enter AST

To understand how the rules of the lint work, we need to talk about it Summary syntax tree (AST) – Data structure in the heart of every linter.

AST is a structural, trees represented by your code. Instead of reading your code as a raw text, a linter turns it into a tree where every part of your code becomes (a variable, wire, a function, and so on) Node In the tree

Here is an example.

Paste this code AST ExplorerA device that facilitates you to view AST for the code in real time:

const name = "Tilda";

Set the language JavaScriptAnd select one of the ESLANT PARors Aspiri. You will see something like in the right panel:

AST (Summary syntax tree) shows the variable clearance node for a permanent declaration. There is a variable inside it that literally assigns the identifier named 'Name' to the Strong Value 'Tilda'.

In the aforementioned image of AST Explorer, you can see how the tree has been made a structure:

  • Program:

    • AST’s root node. It wraps the entire code.

    • Contains a bodyWhich is a row of statements.

  • Variable

    • Type: "VariableDeclaration"

    • Represents a declaration using const Key word

    • A kind Injured "const" And a list of declarations.

  • Variable

This nest is the one that makes the structure “Like a tree”. Each node is the parent of small pieces (its children), which helps the linen navigates the code reliably.

So when your eyes look a small line of JavaScript, Lant sees a detailed map of this line Means Structically allows to indicate how the classification of Esslant is being used as a code precisely – and where – so the rules can target such patterns such as:

  • “All flags const Variables ”

  • “When a variable name is taken name

  • “Hard -coded strings like no permission Tilda

🤖 Why ASTS Difference for Lantings

Now, here’s the key idea: Lant rules do not work by reading text like your code. They work together with specific node samples in AST.

This makes a lot of differences because there are dozens of ways to write the same logic in the JavaScript. Let’s take two versions of the same logic: one written as Function DeclarationAnd as a Arrow function.

function greet() {
  return "hello";
}

const greet = () => "hello";

At one glance, they look different. But when we look at their ASTS, we see that both follow similar structural patterns. This is what your code is doing, allows it to be recognized, even if it is written.

Tree behind the function declaration

Summary is showing a function declaring node with the identifier for the syntax tree (AST) function. The function contains a block statement with the returns statement node. There is a literal node within the Return Statement that is looting the wire 'Hello'

Here is what Eslint sees in the AST tree when you write a function declaration:

Tree on the back of the ver of the Ver arrow

Summary syntax tree (AST) shows a variable clearance node for a thin aero function. There is a variable that assigns an aero -functioning to an identifier. Aero -functioning has a body in the Expression that has a literal node in which the wire returns 'hello'.

When you write the same logic using the aero function, the same looks to use to the extent:

Although the syntax is different, both paths ultimately go towards one Literal node Consisting of "hello" – Which you need to care about all your linter.

💡 Let’s bring him home with an instance

Imagine that your team has a rule: functions should not be returned like a hard -coded indoor "hello". You need a linter that flags it.

With ASTS, you can write A lump rule Which matches A ReturnStatement Or a ArrowFunctionExpression Whose body is a Literal.

Here is the basic idea:

ReturnStatement(node) {
  if (node.argument?.type === "Literal" && node.argument.value === "hello") {
    context.report({ node, message: "Avoid returning static 'hello' strings." });
  }
}

And for the functions of arrows with the expression body:

ArrowFunctionExpression(node) {
  if (node.body?.type === "Literal" && node.body.value === "hello") {
    context.report({ node, message: "Avoid returning static 'hello' strings." });
  }
}

Although the code styles are different AST structure is quite uniform That both functions will trigger this principle, because the Linter does not see how the code is written, only AST’s structure is actually.

This is the thing that makes the ASTS so useful: they allow lunters to ignore the level of differences and focus on the true meaning and structure of your code. As a result, you can write smart, more flexible rules that catch samples of different styles, even if someone wrote their Javascript.

🔨 How does the ESLANT use ASTS under the hood

ESLANT relies on a standard format called Estry (Eximmerpat Tree) As an abstract syntax tree (AST) to represent the Javascript code. Estry itself is not a parser – this is a specification that explains how the Javascript code should be presented as a tree. This makes it possible to understand the Code in a permanent, structural manner (and similar tools).

When you run the Eslant on your code, what’s going on here under the hood:

1. Your code has been analyzed in AST

Esslan converts your code into AST that follows Estry Format. This tree is made of nodes, each represents a piece of your code (such as variable, function, or expression). As a result, the structure is the one that will analyze each of the rules.

2. The rules of the Lant “Subscribe” to specific node types

The rule of each Lant tells Eslant which one Node types It wants to hear. For example, a rule may be cared for:

  • Identifier

  • CallExpression

  • VariableDeclaration

These node types are similar to the structure you will see in tools like AST Explorer.

3. Easlint crosses the tree and triggers the rules

Esslan passes through AST, visits a node at a time. When it reaches the type of node on which a rule is purchased, it triggers the same function in this principle.

This process is effective and declared, you do not have to worry about scanning manually through every line of the code. The Esslan is walking, your rule only listens.

4. Inspect the rules nodes and report problems

Within every rule, you have received a node aslant. You can see its features – such as names, value, or surrounding structures – and decide whether it violates your desired sample.

If this happens, you use context.report() To ask Esslan to flag it as a problem. If you provide one, Eslint can automatically fix this problem fix() Within work context.report().

context.report({
    node: node,
    message: "Missing semicolon".
    fix: function(fixer) {
        return fixer.insertTextAfter(node, ";");
    }
});

LIT a lunt roll anatomy

Let’s see a very simple customs rules. It flags in the name of any variable any:

module.exports = {
  meta: {
    type: "problem",
    docs: {
      description: "Disallow variables named 'any'",
    },
  },

  create(context) {
    return {
      Identifier(node) {
        if (node.name === 'any') {
          context.report({
            node,
            message: "Don't use 'any' as a variable name."
          });
        }
      }
    };
  }
};

🔎 🔎 What is happening here:

  • The meta section provides information about this principle (used in essential documents and tooling).

  • create() The function explains which nodes listen to the principles.

  • Identifier(node) Whenever the identifier is found in the code, each time it is dynamic.

  • If the identifier is the name of anyCalls the rule context.report() Increasing a warning.

Understanding the ASTS may first feel abstract, but some tolls make curves more smooth. These are especially helpful when you are trying to imagine how your code translates into the trees structure, or when we debugged a custom roll.

1. AST Explorer

This is the most early -friendly and powerful tool to work with ASTS. You can do:

  • Stick to any JavaScript code

  • Choose the synchronized parster (such as espress) in accordance with ESLANT

  • Immediately see the AST structure side of the right hand side

  • Freeze the nodes of the trees and see how they map the specific parts of your code

If you are writing customs principles, AST Explorer will become your best friend. This helps you find out which node you need to target and what features are available on this node.

2. Examples and tests of the principle of aslant

Sometimes the best way to learn is to read the real code. The basic rules of the aslant (or the rules from the famous plugin ESLANT PLAGIN RACT) Include:

Browsing them help you understand how real -world rules are formed and how the test setup works.

Hint: I see tests/lib/rules/ Or lib/rules/ Essentials or plugs folders.

3. The documentation of the aslant

Eslant has comprehensive documents about working with rules:

✅ Wrap: Why should you understand this

When it comes to understanding and contributing to the Lanting Tools, understanding that ASTS work provides you with superpower. Whether you are trying to impose a specific sample in your team’s code base or want to contribute to a plug ESLANT PLAGIN RACTThis knowledge will help you:

  • 🔧 🔧 Cooperate in current rules Understanding what they are checking out and how

  • 🐛 🐛 Debug confused linter behavior When the rules are unexpectedly fire (or not at all)

  • 🛠 🛠 Write your customs rules To enforce specific standards, project conventions, or excellent methods

You do not have to be a complimentary expert or do not need to fully understand each node type in the speaker. You just need to get comfortable in identifying samples, detecting trees, and identifying nodes that care about your principle.

You may also like

Leave a Comment

At Skillainest, we believe the future belongs to those who embrace AI, upgrade their skills, and stay ahead of the curve.

Get latest news

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

@2025 Skillainest.Designed and Developed by Pro