# @ampproject/toolbox-linter

> A linter for AMP documents

Latest version **2.10.1** (published 2024-06-14) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @ampproject/toolbox-linter
pnpm add @ampproject/toolbox-linter
yarn add @ampproject/toolbox-linter
bun add @ampproject/toolbox-linter
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.10.1 |
| Published | 2024-06-14 |
| First published | 2019-06-10 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 15 |
| Unpacked size | 85.9 KB |
| Known vulnerabilities | 0 (+8 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 458 |
| Author | AMPHTML Team |
| Maintainers | ampproject-admin, kristoferbaxter, jridgewell, choumx, amp-toolbox, fstanis, erwinmombay, samouri, caroqliu, rsimha, dvoytenko, patrickkettner, alanorozco, kdwan, ampprojectbot, esth |

## Links

- npm: https://www.npmjs.com/package/@ampproject/toolbox-linter
- Repository: https://github.com/ampproject/amp-toolbox
- Homepage: https://github.com/ampproject/amp-toolbox/tree/main/packages/linter
- Issues: https://github.com/ampproject/amp-toolbox/issues
- npm.io page: https://npm.io/package/@ampproject/toolbox-linter

## Dependencies (15)

- [chalk](https://npm.io/package/chalk.md) 4.1.0
- [debug](https://npm.io/package/debug.md) 4.3.1
- [execa](https://npm.io/package/execa.md) 5.0.0
- [throat](https://npm.io/package/throat.md) 6.0.1
- [cheerio](https://npm.io/package/cheerio.md) 1.0.0-rc.12
- [postcss](https://npm.io/package/postcss.md) 8.2.8
- [commander](https://npm.io/package/commander.md) 9.3.0
- [cross-fetch](https://npm.io/package/cross-fetch.md) 3.1.5
- [probe-image-size](https://npm.io/package/probe-image-size.md) 7.0.1
- [amphtml-validator](https://npm.io/package/amphtml-validator.md) 1.0.35
- [postcss-safe-parser](https://npm.io/package/postcss-safe-parser.md) 5.0.2
- [@ampproject/toolbox-cache-url](https://npm.io/package/@ampproject/toolbox-cache-url.md) 2.10.1
- [@ampproject/toolbox-optimizer](https://npm.io/package/@ampproject/toolbox-optimizer.md) 2.10.1
- [@ampproject/toolbox-cache-list](https://npm.io/package/@ampproject/toolbox-cache-list.md) 2.10.1
- [@ampproject/toolbox-validator-rules](https://npm.io/package/@ampproject/toolbox-validator-rules.md) 2.10.1

## Recent versions

- 2.10.1 (latest) — 2024-06-14
- 3.0.0-canary.2 (canary) — 2022-01-11
- 2.7.0-alpha.1 (alpha) — 2020-08-19
- 1.1.0-beta.0 (beta) — 2019-08-29
- 2.10.0 — 2024-06-13
- 2.9.0 — 2022-06-23
- 2.8.10 — 2022-03-16
- 2.8.8 — 2022-02-11
- 2.8.7 — 2022-01-14
- 2.8.6 — 2022-01-12
- 2.8.5 — 2022-01-12
- 2.8.4 — 2021-11-17
- 2.8.3 — 2021-07-12
- 2.8.2 — 2021-07-02
- 2.8.1 — 2021-06-10
- … 64 more at https://npm.io/package/@ampproject/toolbox-linter/versions

## README

# AMP Linter

[![npm version](https://badge.fury.io/js/%40ampproject%2Ftoolbox-linter.svg)](https://badge.fury.io/js/%40ampproject%2Ftoolbox-linter)

## Overview

A [linter](<https://en.wikipedia.org/wiki/Lint_(software)>) for AMP documents:
reports errors and suspicious constructions such as images missing or
incorrectly sized, missing CORS headers, or invalid metadata.

## Usage

Command-line (local build):

```sh
# from the amp-toolbox root
$ npm install
$ npm run build # generates packages/linter/dist/cli.js
$ cd packages/linter
$ node dist/cli.js https://amp.dev/
```

Command-line (from npm):

```sh <!-- markdownlint-disable MD014 -->
$ npx @ampproject/toolbox-cli lint https://amp.dev/
```

Node:

```js
const fs = require("fs");
const linter = require("@ampproject/toolbox-linter");
const cheerio = require("cheerio");

const body = fs.readFileSync("amp-dev.html");
const context = {
  $: cheerio.load(body),
  headers: {},
  url: "https://amp.dev/"
};

linter.MetaCharsetIsFirst(context).then(console.log);
```

## `dump-signedexchange`

One test has a dependency on the `dump-signedexchange` go binary. If this is
available ([installation
instructions](https://github.com/WICG/webpackage/tree/master/go/signedexchange#installation))
at additional check of the `application/signed-exchange` response will be
performed.

## Development

### Commands/Scripts

These scripts can be invoked in the usual way by `npm run XXX` if `npm install`
is run in this directory. They can also be invoked from the `amp-toolbox` root
directory without installing locally by `lerna run --scope '*/toolbox-linter' XXX`. (lerna sets the `PATH` so that the required binaries are available.)

#### `build`

Populates the `dist` directory with the appropriate `*.js` and `*.d.ts` files.
Note that tests are _not_ included. This script is intended to be used when
building the npm package.

#### `lint`

Checks the code for lint errors using prettier.

### Running tests

The unit tests run in the context of toolbox project.
```
# Run all linter tests
npm run test:node -- packages/linter

# Run linter network tests
npm run test:node -- packages/linter/tests/network.test.ts
```

### Suggested Development Workflow

1. Create stub rule in `rules/`, that always "fails". e.g. it always returns
   `qqqqqq`. It should extend the `Rule` class.
1. Write tests in `tests/network.test.ts` (If HTTP requests are required). If not
   then create a directory in `tests/local/MyNewTest-1` that contains a
   `source.html` (AMP HTML source) and add your test in `tests/local.test.ts`. 
1. Run the test using `npm run test:node -- packages/linter` in the toolbox root.
   If the fixtures can't be found, they will be generated automatically
   (via real network requests). Hopefully your test will fail.
1. Fix the implementation, and re-run the test.

---
_Source: https://npm.io/package/@ampproject/toolbox-linter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
