🔍 Firefox Add-ons linter, written in JavaScript. 👁
Перейти к файлу
Ruchika 28b6bbde5e Move bn-BD to bn (Bengali) 2019-07-23 15:22:27 +05:30
bin Move internal scripts to separate folder (#2309) 2018-12-11 08:30:11 +01:00
docs fix(themes): Log validation errors on theme manifests using the deprecated LWT aliases 2019-07-19 21:12:39 +02:00
locale Move bn-BD to bn (Bengali) 2019-07-23 15:22:27 +05:30
scripts Fix errors 2019-07-15 12:50:01 +02:00
src Import Firefox 69 schema. (#2706) 2019-07-23 10:57:03 +02:00
tests fix(themes): Log validation errors on theme manifests using the deprecated LWT aliases 2019-07-19 21:12:39 +02:00
tmp Update tar to 3.0.1, handle stream errors in schema import (#1278) 2017-06-01 10:44:49 -05:00
.babelrc Upgrade to Babel 7. 2018-09-06 19:30:09 +02:00
.eslintignore Move internal scripts to separate folder (#2309) 2018-12-11 08:30:11 +01:00
.eslintrc Update eslint-config-amo, integrate Prettier and addons-frontend inspired configuration (#2144) 2018-09-04 13:18:45 +02:00
.gitignore Add yarn.lock to .gitignore (#2054) 2018-06-20 11:59:41 +01:00
.npmignore chore(npmignore): Cleanup list of assets included in the released npm package 2018-04-05 16:02:37 +02:00
.npmrc Force exact version pinning. 2018-10-11 16:12:56 +02:00
.nvmrc Update Node.js to v8.16.0 2019-04-16 20:26:57 +00:00
.prettierignore Move internal scripts to separate folder (#2309) 2018-12-11 08:30:11 +01:00
.prettierrc Fix errors 2019-07-15 12:50:01 +02:00
.snyk Update snyk 2018-11-27 07:27:14 +01:00
.spelling Run markdown-spellcheck against README and docs 2018-10-16 15:36:09 -07:00
.travis.yml Fix travis tests 2019-01-30 13:14:01 +01:00
CODE_OF_CONDUCT.md Update code of conduct (fixes #2529) 2019-04-25 19:00:09 +01:00
CONTRIBUTING.md Update eslint-config-amo, integrate Prettier and addons-frontend inspired configuration (#2144) 2018-09-04 13:18:45 +02:00
ISSUE_TEMPLATE.md Update eslint-config-amo, integrate Prettier and addons-frontend inspired configuration (#2144) 2018-09-04 13:18:45 +02:00
LICENSE Initial commit 2015-09-14 09:34:44 +01:00
PULL_REQUEST_TEMPLATE.md Fix typo 2018-11-21 11:09:08 +00:00
README.md Configure renovate, force exact package-lock versions. (#2410) 2019-01-30 16:01:20 +01:00
appveyor.yml Move to npm and update lock-file 2018-06-19 11:06:29 +01:00
config.js Move bn-BD to bn (Bengali) 2019-07-23 15:22:27 +05:30
jest.config.js Replace setupTestFrameworkScriptFile with setupFilesAfterEnv 2019-06-23 02:50:22 -05:00
jest.integration.config.js Update eslint-config-amo, integrate Prettier and addons-frontend inspired configuration (#2144) 2018-09-04 13:18:45 +02:00
package-lock.json chore(deps): update dependency tmp-promise to v2 (#2704) 2019-07-22 14:31:33 +02:00
package.json Prepare 1.11.0 2019-07-23 10:58:16 +02:00
renovate.json Configure renovate, force exact package-lock versions. (#2410) 2019-01-30 16:01:20 +01:00
webpack.config.js Update eslint-config-amo, integrate Prettier and addons-frontend inspired configuration (#2144) 2018-09-04 13:18:45 +02:00
webpack.l10n.config.babel.js Update eslint-config-amo, integrate Prettier and addons-frontend inspired configuration (#2144) 2018-09-04 13:18:45 +02:00

README.md

Build Status Coverage Status Dependency Status devDependency Status npm version

addons-linter

The Add-ons Linter is being used by web-ext and addons.mozilla.org to lint WebExtensions.

It can also be used as a standalone binary and library.

You can find more information about the linter and it's implemented rules in our documentation.

Usage

Command Line

You need Node.js to use the add-ons linter.

To validate your add-on locally, install the linter from npm:

# Install globally so you can use the linter from any directory on
# your machine.
npm install -g addons-linter

After installation, run the linter and direct it to your add-on file:

addons-linter my-addon.zip

Alternatively you can point it at a directory:

addons-linter my-addon/src/

The addons-linter will check your add-on and show you errors, warnings, and friendly messages for your add-on. If you want more info on the options you can enable/disable for the command-line app, use the --help option:

addons-linter --help

Linter API Usage

You can use the linter directly as a library to integrate it better into your development process.

import linter from 'addons-linter';

const sourceDir = process.cwd();

const linter = linter.createInstance({
  config: {
    // This mimics the first command line argument from yargs,
    // which should be the directory to the extension.
    _: [sourceDir],
    logLevel: process.env.VERBOSE ? 'debug' : 'fatal',
    stack: Boolean(process.env.VERBOSE),
    pretty: false,
    warningsAsErrors: false,
    metadata: false,
    output: 'none',
    boring: false,
    selfHosted: false,
    // Lint only the selected files
    //   scanFile: ['path/...', ...]
    //
    // Exclude files:
    shouldScanFile: (fileName) => true,
  },
  // This prevent the linter to exit the nodejs application
  runAsBinary: false,
});

linter.run()
  .then((linterResults) => ...)
  .catch((err) => console.error("addons-linter failure: ", err));

linter.output is composed by the following properties (the same of the 'json' report type):

{
  metadata: {...},
  summary: {
    error, notice, warning,
  },
  scanFile,
  count,
  error: [{
    type: "error",
    code, message, description,
    column, file, line
  }, ...],
  warning: [...],
  notice: [...]
}

Development

If you'd like to help us develop the addons-linter, that's great! It's pretty easy to get started, you just need Node.js installed on your machine.

Quick Start

If you have Node.js installed, here's the quick start to getting your development dependencies installed and running the tests

git clone https://github.com/mozilla/addons-linter.git
cd addons-linter
npm install
# Run the test-suite and watch for changes. Use `npm run test-once` to
# just run it once.
npm run test

You can also build the addons-linter binary to test your changes.

npm run build
# Now run it against your add-on. Please note that for every change
# in the linter itself you'll have to re-build the linter.
bin/addons-linter my-addon.zip

Required Node version

addons-linter requires Node.js v8 or greater. Have a look at our .travis.yml file which Node.js versions we officially test.

Using nvm is probably the easiest way to manage multiple Node versions side by side. See nvm on GitHub for more details.

Install dependencies

Install dependencies with npm:

npm install

Dependencies are automatically kept up-to-date using renovatebot.

npm scripts

Script Description
npm test Runs the tests (watches for changes)
npm [run] build Builds the lib (used by Travis)
npm run test-coverage Runs the tests with coverage (watches for changes)
npm run test-once Runs the tests once
npm run lint Runs ESLint
npm run test-coverage-once Runs the tests once with coverage
npm run test-integration-linter Runs our integration test-suite
npm run prettier Automatically format the whole code-base with Prettier
npm run prettier-dev Automatically compare and format modified source files against the master branch

Building

You can run npm build to build the library.

Once you build the library you can use the CLI in bin/addons-linter.

Testing

Run npm test. This will watch for file-changes and re-runs the test suite.

Coverage

We're looking to maintain coverage at 100%. Use the coverage data in the test output to work out what lines aren't covered and ensure they're covered.

Assertions and testing APIs

We are using using Sinon for assertions, mocks, stubs and more see the Sinon docs for the API available.

Jest is being used as a test-runner but also provides helpful tools. Please make sure you read their documentation for more details.

Logging

We use pino for logging:

  • By default logging is off (level is set to 'fatal') .
  • Logging in tests can be enabled using an env var e.g: LOG_LEVEL=debug jest test
  • Logging on the CLI can be enabled with --log-level [level].

Prettier

We use Prettier to automatically format our JavaScript code and stop all the on-going debates over styles. As a developer, you have to run it (with npm run prettier-dev) before submitting a Pull Request.

Architecture

In a nutshell the way the linter works is to take an add-on package, extract the metadata from the xpi (zip) format and then process the files it finds through various content scanners.

We are heavily relying on ESLint for JavaScript linting, cheerio for HTML parsing as well as fluent.js for parsing language packs.

Scanners

Each file-type has a scanner. For example: CSS files use CSSScanner; JavaScript files use JavaScriptScanner. Each scanner looks at relevant files and passes each file through a parser which then hands off to a set of rules that look for specific things.

Rules

Rules get exported via a single function in a single file. A rule can have private functions it uses internally, but rule code should not depend on another rule file and each rule file should export one rule.

Each rule function is passed data from the scanner in order to carry out the specific checks for that rule it returns a list of objects which are then made into message objects and are passed to the Collector.

Collector

The Collector is an in-memory store for all validation message objects "collected" as the contents of the package are processed.

Messages

Each message has a code which is also its key. It has a message which is a short outline of what the message represents, and a description which is more detail into why that message was logged. The type of the message is set as messages are added so that if necessary the same message could be an error or a warning for example.

Output

Lastly when the processing is complete the linter will output the collected data as text or JSON.

Deploys

We deploy to npm automatically using TravisCI. To release a new version, increment the version in package.json and create a PR. Make sure your version number conforms to the semver format eg: 0.2.1.

After merging the PR, create a new release with the same tag name as your new version. Once the build passes it will deploy. Magic!