Skip to content

Commit

Permalink
feat: add @nuxt/eslint-config for nuxt3 projects (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Dec 6, 2022
1 parent 427e6ed commit bf74ad9
Show file tree
Hide file tree
Showing 14 changed files with 541 additions and 322 deletions.
86 changes: 72 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,58 @@
# Nuxt ESLint Config
# Nuxt ESLint packages

## `@nuxt/eslint-config`

[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![Github Actions][github-actions-src]][github-actions-href]
[![Codecov][codecov-src]][codecov-href]
[![Bundlephobia][bundlephobia-src]][bundlephobia-href]
[![LGTM][lgtm-src]][lgtm-href]

> Non-opinionated [ESlint](https://eslint.org/) configuration for Nuxt 3 apps.
### Features

- Works out-of-the-box with no additional configuration.
- Nuxt-specific rules for pages, components and more.
- ... under active development

### Installation

1. Install this package and `eslint` in your `devDependencies`.

```bash
npm i -D @nuxt/eslint-config eslint
yarn add -D @nuxt/eslint-config eslint
pnpm add -D @nuxt/eslint-config eslint
```

2. Extend the default Nuxt config:

```js
module.exports = {
root: true,
extends: ["@nuxt/eslint-config"],
};
```

You might also want to add a script entry to your `package.json:

```json
{
"scripts": {
"lint": "eslint ."
}
}
```

## `@nuxtjs/eslint-config` and `@nuxtjs/eslint-config-typescript`

[![GitHub Actions](https://flat.badgen.net/github/checks/nuxt/eslint-config/main)](https://github.com/nuxt/eslint-config/actions?query=workflow%3Aci)
[![npm](https://flat.badgen.net/npm/dm/@nuxtjs/eslint-config)](https://npmjs.com/package/@nuxtjs/eslint-config)
[![npm (scoped with tag)](https://flat.badgen.net/npm/v/@nuxtjs/eslint-config)](https://npmjs.com/package/@nuxtjs/eslint-config)

[ESlint](https://eslint.org/) config used for Nuxt.
> Opinionated [ESlint](https://eslint.org/) configuration used internally by Nuxt projects.
## Usage

Expand Down Expand Up @@ -32,15 +80,14 @@ $ yarn add -D eslint

```json
{
"extends": [
"@nuxtjs"
]
"extends": ["@nuxtjs"]
}
```

## Full example

A full example `.eslintrc` for a project with babel support:

> Dont forget to `npm i -D @babel/eslint-parser` or `yarn add -D @babel/eslint-parser`
```json
Expand All @@ -50,9 +97,7 @@ A full example `.eslintrc` for a project with babel support:
"parserOptions": {
"sourceType": "module"
},
"extends": [
"@nuxtjs"
]
"extends": ["@nuxtjs"]
}
```

Expand All @@ -64,9 +109,7 @@ And in your `.eslintrc` all you need is :

```json
{
"extends": [
"@nuxtjs/eslint-config-typescript"
]
"extends": ["@nuxtjs/eslint-config-typescript"]
}
```

Expand All @@ -75,8 +118,23 @@ You can find the list of supported TypeScript rules [here](https://github.com/ty

Also see [Nuxt TypeScript Support](https://typescript.nuxtjs.org/guide/lint.html).

## License
### License

Made with ❤️

Published under [MIT License](./LICENCE).

Setup inspired by [eslint-config-standard](https://github.com/standard/eslint-config-standard)
<!-- Badges -->

Published under the [MIT License](./LICENSE).
[npm-version-src]: https://img.shields.io/npm/v/@nuxt/eslint-config?style=flat-square
[npm-version-href]: https://npmjs.com/package/@nuxt/eslint-config
[npm-downloads-src]: https://img.shields.io/npm/dm/@nuxt/eslint-config?style=flat-square
[npm-downloads-href]: https://npmjs.com/package/@nuxt/eslint-config
[github-actions-src]: https://img.shields.io/github/workflow/status/nuxt/eslint-config/ci/main?style=flat-square
[github-actions-href]: https://github.com/nuxt/eslint-config/actions?query=workflow%3Aci
[codecov-src]: https://img.shields.io/codecov/c/gh/nuxt/eslint-config/main?style=flat-square
[codecov-href]: https://codecov.io/gh/nuxt/eslint-config
[lgtm-src]: https://img.shields.io/lgtm/grade/javascript/github/nuxt/eslint-config?style=flat-square
[lgtm-href]: https://lgtm.com/projects/g/nuxt/eslint-config
[bundlephobia-src]: https://img.shields.io/bundlephobia/minzip/@nuxt/eslint-config?style=flat-square
[bundlephobia-href]: https://bundlephobia.com/package/@nuxt/eslint-config
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions packages/eslint-config-legacy/README.md
170 changes: 170 additions & 0 deletions packages/eslint-config-legacy/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
const { getPackageInfoSync } = require('local-pkg')

const nuxt = getPackageInfoSync('nuxt')
const isNuxt2 = nuxt && nuxt.version && nuxt.version.startsWith('2.')

module.exports = {
env: {
browser: true,
node: true,
...(isNuxt2 ? {} : { es6: true })
},
extends: [
'standard',
'plugin:import/errors',
'plugin:import/warnings',
isNuxt2
? 'plugin:vue/recommended'
: 'plugin:vue/vue3-recommended'
],
plugins: [
'unicorn',
'vue'
],
settings: {
'import/resolver': {
node: { extensions: ['.js', '.mjs'] }
}
},
rules: {

/**********************/
/* General Code Rules */
/**********************/

// Enforce import order
'import/order': 'error',

// Imports should come first
'import/first': 'error',

// Other import rules
'import/no-mutable-exports': 'error',

// Allow unresolved imports
'import/no-unresolved': 'off',

// Allow paren-less arrow functions only when there's no braces
'arrow-parens': ['error', 'as-needed', { requireForBlockBody: true }],

// Allow async-await
'generator-star-spacing': 'off',

// Allow debugger during development
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'warn',
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'warn',

// Prefer const over let
'prefer-const': ['error', {
destructuring: 'any',
ignoreReadBeforeAssign: false
}],

// No single if in an "else" block
'no-lonely-if': 'error',

// Force curly braces for control flow,
// including if blocks with a single statement
curly: ['error', 'all'],

// No async function without await
'require-await': 'error',

// Force dot notation when possible
'dot-notation': 'error',

'no-var': 'error',

// Force object shorthand where possible
'object-shorthand': 'error',

// No useless destructuring/importing/exporting renames
'no-useless-rename': 'error',

/**********************/
/* Unicorn Rules */
/**********************/

// Pass error message when throwing errors
'unicorn/error-message': 'error',

// Uppercase regex escapes
'unicorn/escape-case': 'error',

// Array.isArray instead of instanceof
'unicorn/no-array-instanceof': 'error',

// Prevent deprecated `new Buffer()`
'unicorn/no-new-buffer': 'error',

// Keep regex literals safe!
'unicorn/no-unsafe-regex': 'off',

// Lowercase number formatting for octal, hex, binary (0x12 instead of 0X12)
'unicorn/number-literal-case': 'error',

// ** instead of Math.pow()
'unicorn/prefer-exponentiation-operator': 'error',

// includes over indexOf when checking for existence
'unicorn/prefer-includes': 'error',

// String methods startsWith/endsWith instead of more complicated stuff
'unicorn/prefer-starts-ends-with': 'error',

// textContent instead of innerText
'unicorn/prefer-text-content': 'error',

// Enforce throwing type error when throwing error while checking typeof
'unicorn/prefer-type-error': 'error',

// Use new when throwing error
'unicorn/throw-new-error': 'error',

/**********************/
/* Vue Rules */
/**********************/

// Disable template errors regarding invalid end tags
'vue/no-parsing-error': ['error', {
'x-invalid-end-tag': false
}],

// Maximum 5 attributes per line instead of one
'vue/max-attributes-per-line': ['error', {
singleline: 5
}],

// v-model argument is supported in Vue3
'vue/no-v-model-argument': isNuxt2 ? 'error' : 'off'
},
overrides: [
{
files: [
'**/pages/**/*.{js,ts,vue}',
'**/layouts/**/*.{js,ts,vue}',
'**/app.{js,ts,vue}',
'**/error.{js,ts,vue}'
],
rules: {
'vue/multi-word-component-names': 'off',
// Pages and layouts are required to have a single root element
'vue/no-multiple-template-root': 'error'
}
}
],
reportUnusedDisableDirectives: true,
ignorePatterns: [
'*.min.*',
'*.d.ts',
'dist',
'LICENSE*',
'output',
'coverage',
'public',
'package-lock.json',
'pnpm-lock.yaml',
'yarn.lock',
'__snapshots__'
]
}
23 changes: 23 additions & 0 deletions packages/eslint-config-legacy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@nuxtjs/eslint-config",
"version": "12.0.0",
"description": "ESlint config used for Nuxt",
"repository": "nuxt/eslint-config",
"license": "MIT",
"files": [
"index.js"
],
"dependencies": {
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-n": "^15.6.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-unicorn": "^45.0.1",
"eslint-plugin-vue": "^9.8.0",
"local-pkg": "^0.4.2"
},
"peerDependencies": {
"eslint": "^8.29.0"
}
}
File renamed without changes.

0 comments on commit bf74ad9

Please sign in to comment.