Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: blog: Differences between linting and type checking #664

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JoshuaKGoldberg
Copy link
Contributor

Prerequisites checklist

What is the purpose of this pull request?

Adds the blog post per #663.

What changes did you make? (Give an overview)

The post explains:

  1. The terminology difference between the two within "static analysis"
  2. Their differences in purpose: "can" vs. "should" 🧑‍🔬
  3. How linters are more granularly extensible with plugin rules
  4. Notes on their areas of overlap, with a suggestion to use linting only instead of TypeScript's noUnusedLocals and noUnusedParameters
  5. How linters and type checkers benefit from each other

In adding myself as an author, I noticed there wasn't yet support for Bluesky URLs. So I added that in too. I can split that out if preferred.

Related Issues

Fixes #663.

Is there anything you'd like reviewers to focus on?

  • Is this too long a post for its intended purpose of convincing folks that using ESLint with TypeScript is beneficial?
  • This title is not very emphatic - would it be better to go with a more punchy one? I'm drawing a blank.
  • On the other hand, I worry that the phrasing is a bit ... sales-y? for typescript-eslint. My personal biases might be showing 🙂.
  • I also commented some questions inline about whether we can visualize rich code blocks nicely

Copy link

netlify bot commented Nov 26, 2024

Deploy Preview for es-eslint ready!

Name Link
🔨 Latest commit b5ea884
🔍 Latest deploy log https://app.netlify.com/sites/es-eslint/deploys/674579ddd49031000814af09
😎 Deploy Preview https://deploy-preview-664--es-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Nov 26, 2024

Deploy Preview for ja-eslint ready!

Name Link
🔨 Latest commit b5ea884
🔍 Latest deploy log https://app.netlify.com/sites/ja-eslint/deploys/674579ddc5d7f80008c36b10
😎 Deploy Preview https://deploy-preview-664--ja-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Nov 26, 2024

Deploy Preview for new-eslint ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit b5ea884
🔍 Latest deploy log https://app.netlify.com/sites/new-eslint/deploys/674579dda94d520008569cfc
😎 Deploy Preview https://deploy-preview-664--new-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

// ~~~~~~~~~~~~~~~~~~~~~~~~~
// eslint(jsx-a11y/alt-text):
// img elements must have an alt prop, either with meaningful text, or an empty string for decorative images.
```
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Question] Is there a preferred strategy for showing ESLint rule reports in code blocks within blog posts?

Copy link

netlify bot commented Nov 26, 2024

Deploy Preview for hi-eslint ready!

Name Link
🔨 Latest commit b5ea884
🔍 Latest deploy log https://app.netlify.com/sites/hi-eslint/deploys/674579dd05bf520008f1baf9
😎 Deploy Preview https://deploy-preview-664--hi-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Nov 26, 2024

Deploy Preview for zh-hans-eslint ready!

Name Link
🔨 Latest commit b5ea884
🔍 Latest deploy log https://app.netlify.com/sites/zh-hans-eslint/deploys/674579dd4bab880008325a08
😎 Deploy Preview https://deploy-preview-664--zh-hans-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Nov 26, 2024

Deploy Preview for fr-eslint ready!

Name Link
🔨 Latest commit b5ea884
🔍 Latest deploy log https://app.netlify.com/sites/fr-eslint/deploys/674579ddd2884800082eece5
😎 Deploy Preview https://deploy-preview-664--fr-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Nov 26, 2024

Deploy Preview for de-eslint ready!

Name Link
🔨 Latest commit b5ea884
🔍 Latest deploy log https://app.netlify.com/sites/de-eslint/deploys/674579ddd6c61200085772be
😎 Deploy Preview https://deploy-preview-664--de-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Nov 26, 2024

Deploy Preview for pt-br-eslint ready!

Name Link
🔨 Latest commit b5ea884
🔍 Latest deploy log https://app.netlify.com/sites/pt-br-eslint/deploys/674579dda7a4910008a1ffd6
😎 Deploy Preview https://deploy-preview-664--pt-br-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

console.log("🍌");

// eslint(no-fallthrough):
// Expected a 'break' statement before 'case'.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love this first example:

  • It's pretty verbose
  • It doesn't show something that's clearly, definitely a valuable report ("what if I want to break through, that's too opinionated!")

...but I couldn't find a better lint rule that:

  • Produces clear valuable report on a likely bug
  • Is in js.configs.recommended
  • Isn't superseded by TypeScript

Seeking inputs on a better example to show!

## Differences in Purpose

Type checkers make sure the *intent* behind values in code matches the *usage* of those values.
They check that code is "type-safe": meaning values are used according to the ways their types describe as being allowed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
They check that code is "type-safe": meaning values are used according to the ways their types describe as being allowed.
They check that code is "type-safe": values are used according to how their types are described as being allowed.

Each lint rule can be granularly configured.
If you don't like a particular lint rule, you can always turn it off for a line, set of files, or your entire project.

Linters can also can be augmented by **plugins** that add new lint rules.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Linters can also can be augmented by **plugins** that add new lint rules.
Linters can also be augmented by **plugins** that add new lint rules.


Linters and type checkers operate differently and specialize in different areas of code defects.
Some code defects straddle the line between "best practices" and "type safety", and so can be caught by both linting and type checking.
There are some core rules in that are superseded by TypeScript's type checking.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
There are some core rules in that are superseded by TypeScript's type checking.
There are some core rules that are superseded by TypeScript's type checking.

They additionally ignore parameters that come before any parameter that is itself used.

Some projects prefer to never allow unused parameters regardless of name or position.
That stricter preferences helps prevent API designs that lead developers to creating many unused parameters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
That stricter preferences helps prevent API designs that lead developers to creating many unused parameters.
These stricter preferences help prevent API designs that lead developers to create many unused parameters.

### Linters, With Type Information

Traditional lint rules run on a single file at a time and have no knowledge of other files in the project.
They can't make decisions on files based on contents of other files.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
They can't make decisions on files based on contents of other files.
They can't make decisions on files based on the contents of other files.

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran out of time so just left some comments on the first bits. Overall, I think we need to work on clearing up the language and the message. I'll continue looking tomorrow.

I'd also like to pull out adding support Bluesky social links into a separate PR as it's not really related to the article.

@@ -16,7 +16,7 @@
</p>
</div>

<div class="profile__social-links">
<div class="profile__social-links a ">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we pull these changes out into a separate PR?

@@ -45,7 +45,9 @@
title: all_authors[author].title,
handle: all_authors[author].username,
github: all_authors[author].github_username,
bluesky: all_authors[author].bluesky_url,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, let's pull out into a separate PR.

- Linting
---

Linters and type checkers are two kinds of tools that both analyze code and report on detected issues.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a bit more in the intro. This is a bit abstract, and I think could use some grounding in reality. Something like, "If you're a JavaScript developer today, you're probably using TypeScript or ESLint, or both..."


## Definitions

*"Static analysis"* is the term for tooling that checks code for issues without running it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stylistically, let's just use italics when introducing new terms (no quotes).

Also, static analysis isn't tooling, it's what the tooling does, so probably more accurately:

Suggested change
*"Static analysis"* is the term for tooling that checks code for issues without running it.
*Static analysis* is the inspection of source code without executing it. This differs from *dynamic analysis*, in which source code is inspected while it is executed. As such, dynamic analysis brings with it the inherent danger of executing malicious code or creating side effects while static analysis is safe to execute regardless of the source code.

They may seem similar at first.
However, the two kinds of tools detect very different issues and are useful in different ways.

## Definitions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd like to make this more conversation that just listing definitions.

Suggested change
## Definitions
## What is static analysis?

*"Static analysis"* is the term for tooling that checks code for issues without running it.
There are three forms of static analysis commonly used in web development projects today:

1. **Formatters**: only print your code quickly, without worrying about logic
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure formatting belongs in this list. Unlike linters and type checkers, formatters don't return a list of things to know about code. For the purposes of this article, I'd say we can safely omit this.

## Definitions

*"Static analysis"* is the term for tooling that checks code for issues without running it.
There are three forms of static analysis commonly used in web development projects today:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add another paragraph here explaining the value of static analysis for catching problems before moving on to discuss linters and type checkers.

Comment on lines +23 to +24
2. **Linters**: execute individually configurable checks known as "lint rules"
3. **Type checkers**: collect all files into a full understanding of the project
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these descriptions are accurate. You're describing how ESLint works and how TypeScript works, but that doesn't necessarily mean all linters and type checkers work like this.

I'd focus instead on linting as the systematic evaluation of patterns in the structure of source code (maybe even briefly describe the original C lint tool) while type checking is an evaluation of data flow throughout the program.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Triaging
Development

Successfully merging this pull request may close these issues.

Blog post on why to use ESLint with TypeScript
3 participants