Skip to content

Commit

Permalink
[Breaking] Add config for Jest (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Feb 10, 2023
1 parent 8fda131 commit 43367b2
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 152 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Expand Up @@ -7,3 +7,11 @@ end_of_line = lf
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true

[package.json]
indent_style = space
indent_size = 2

[*.{md,yml}]
indent_style = space
indent_size = 2
123 changes: 9 additions & 114 deletions .eslintrc.js
@@ -1,120 +1,15 @@
'use strict';

// eslint-disable-next-line no-undef -- Keep backward compatibility with CommonJS.
module.exports = {
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
},
env: {
es6: true,
node: true,
},
extends: [
'eslint:recommended',
'plugin:node/recommended-module',
'plugin:jest/recommended',
'plugin:jest/style',
'plugin:regexp/recommended',
'prettier',
],
rules: {
'array-callback-return': 'error',
'dot-notation': 'error',
eqeqeq: ['error', 'smart'],
'func-name-matching': 'error',
'func-names': ['error', 'as-needed'],
'guard-for-in': 'error',
'no-confusing-arrow': [
'error',
{
allowParens: false,
},
],
'no-console': [
'error',
{
allow: ['warn', 'error'],
},
],
'no-else-return': [
'error',
{
allowElseIf: false,
},
],
'no-implicit-coercion': 'error',
'no-lonely-if': 'error',
'no-shadow': 'error',
'no-unneeded-ternary': 'error',
'no-unused-vars': [
'error',
{
ignoreRestSiblings: true,
},
],
'no-use-before-define': ['error', 'nofunc'],
'no-useless-return': 'error',
'no-var': 'error',
'node/no-unsupported-features/es-builtins': 'error',
'node/no-unsupported-features/es-syntax': 'error',
'node/no-unsupported-features/node-builtins': 'error',
'object-shorthand': 'error',
'one-var': ['error', 'never'],
'operator-assignment': 'error',
'padding-line-between-statements': [
'error',
// Require blank lines after all directive prologues (e. g. 'use strict')
{
blankLine: 'always',
prev: 'directive',
next: '*',
},
// Disallow blank lines between all directive prologues (e. g. 'use strict')
{
blankLine: 'never',
prev: 'directive',
next: 'directive',
},
// Require blank lines after every sequence of variable declarations
{
blankLine: 'always',
prev: ['const', 'let', 'var'],
next: '*',
},
// Blank lines could be between variable declarations
{
blankLine: 'any',
prev: ['const', 'let', 'var'],
next: ['const', 'let', 'var'],
},
// Require blank lines before all return statements
{
blankLine: 'always',
prev: '*',
next: 'return',
},
// Require blank lines before and after all following statements
{
blankLine: 'always',
prev: '*',
next: ['for', 'function', 'if', 'switch', 'try'],
},
{
blankLine: 'always',
prev: ['for', 'function', 'if', 'switch', 'try'],
next: '*',
},
],
'prefer-arrow-callback': 'error',
'prefer-object-spread': 'error',
'prefer-regex-literals': 'error',
'prefer-rest-params': 'error',
'prefer-spread': 'error',
'prefer-template': 'error',
'sort-imports': ['error', { allowSeparatedGroups: true }],
root: true,

// Prefer code readability, e.g. `[0-9A-Za-z]`.
'regexp/prefer-d': 'off',
extends: ['./index.js', './jest.js'],

// TODO: Keep backward compatibility with CommonJS. We may delete after ESM migration.
globals: {
module: true,
require: true,
},

reportUnusedDisableDirectives: true,
};
30 changes: 23 additions & 7 deletions README.md
@@ -1,10 +1,11 @@
# eslint-config-stylelint

[![NPM version](https://img.shields.io/npm/v/eslint-config-stylelint.svg)](https://www.npmjs.org/package/eslint-config-stylelint) [![Build Status](https://github.com/stylelint/eslint-config-stylelint/workflows/CI/badge.svg)](https://github.com/stylelint/eslint-config-stylelint/actions)
[![NPM version](https://img.shields.io/npm/v/eslint-config-stylelint.svg)](https://www.npmjs.org/package/eslint-config-stylelint)
[![Build Status](https://github.com/stylelint/eslint-config-stylelint/workflows/CI/badge.svg)](https://github.com/stylelint/eslint-config-stylelint/actions)

> stylelint org's shareable config for ESLint.
> Stylelint org's shareable config for ESLint.
For consistent JavaScript across stylelint's repos.
For consistent JavaScript across Stylelint's repos.

## Installation

Expand All @@ -14,13 +15,28 @@ $ npm install eslint-config-stylelint --save-dev

## Usage

Add this to your `package.json`:
Add this to your ESLint config:

```json
{
"eslintConfig": {
"extends": ["stylelint"]
}
"extends": ["stylelint"]
}
```

### For Jest

Install the plugin additionally:

```console
$ npm install eslint-plugin-jest --save-dev
```

Then, update your config:

```diff json
{
- "extends": ["stylelint"]
+ "extends": ["stylelint", "stylelint/jest"]
}
```

Expand Down
7 changes: 7 additions & 0 deletions __tests__/helper.js
@@ -0,0 +1,7 @@
'use strict';

function isObject(obj) {
return typeof obj === 'object' && obj !== null;
}

module.exports = { isObject };
15 changes: 5 additions & 10 deletions __tests__/index.test.js
@@ -1,25 +1,20 @@
'use strict';

// eslint-disable-next-line no-undef -- Keep backward compatibility with CommonJS.
const { ESLint } = require('eslint');

// eslint-disable-next-line no-undef -- Keep backward compatibility with CommonJS.
const config = require('../index');
const { isObject } = require('./helper');

it('test basic properties of config', () => {
test('basic properties of config', () => {
expect(isObject(config.parserOptions)).toBeTruthy();
expect(isObject(config.env)).toBeTruthy();
expect(isObject(config.rules)).toBeTruthy();
expect(Array.isArray(config.extends)).toBeTruthy();
});

it('load config in ESLint to validate all rule syntax is correct', async () => {
const eslint = new ESLint({});

test('load config in ESLint to validate all rule syntax is correct', async () => {
const eslint = new ESLint({ baseConfig: config, useEslintrc: false });
const results = await eslint.lintText('var foo\n');

expect(results).toBeTruthy();
});

function isObject(obj) {
return typeof obj === 'object' && obj !== null;
}
17 changes: 17 additions & 0 deletions __tests__/jest.test.js
@@ -0,0 +1,17 @@
'use strict';

const { ESLint } = require('eslint');

const config = require('../jest');
const { isObject } = require('./helper');

test('basic properties of config', () => {
expect(isObject(config.extends)).toBeTruthy();
});

test('load config in ESLint to validate all rule syntax is correct', async () => {
const eslint = new ESLint({ baseConfig: config, useEslintrc: false });
const results = await eslint.lintText('test("foo");');

expect(results).toBeTruthy();
});
117 changes: 115 additions & 2 deletions index.js
@@ -1,4 +1,117 @@
'use strict';

// eslint-disable-next-line no-undef -- Keep backward compatibility with CommonJS.
module.exports = require('./.eslintrc.js');
module.exports = {
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
},
env: {
es6: true,
node: true,
},
extends: [
'eslint:recommended',
'plugin:node/recommended-module',
'plugin:regexp/recommended',
'prettier',
],
rules: {
'array-callback-return': 'error',
'dot-notation': 'error',
eqeqeq: ['error', 'smart'],
'func-name-matching': 'error',
'func-names': ['error', 'as-needed'],
'guard-for-in': 'error',
'no-confusing-arrow': [
'error',
{
allowParens: false,
},
],
'no-console': [
'error',
{
allow: ['warn', 'error'],
},
],
'no-else-return': [
'error',
{
allowElseIf: false,
},
],
'no-implicit-coercion': 'error',
'no-lonely-if': 'error',
'no-shadow': 'error',
'no-unneeded-ternary': 'error',
'no-unused-vars': [
'error',
{
ignoreRestSiblings: true,
},
],
'no-use-before-define': ['error', 'nofunc'],
'no-useless-return': 'error',
'no-var': 'error',
'node/no-unsupported-features/es-builtins': 'error',
'node/no-unsupported-features/es-syntax': 'error',
'node/no-unsupported-features/node-builtins': 'error',
'object-shorthand': 'error',
'one-var': ['error', 'never'],
'operator-assignment': 'error',
'padding-line-between-statements': [
'error',
// Require blank lines after all directive prologues (e. g. 'use strict')
{
blankLine: 'always',
prev: 'directive',
next: '*',
},
// Disallow blank lines between all directive prologues (e. g. 'use strict')
{
blankLine: 'never',
prev: 'directive',
next: 'directive',
},
// Require blank lines after every sequence of variable declarations
{
blankLine: 'always',
prev: ['const', 'let', 'var'],
next: '*',
},
// Blank lines could be between variable declarations
{
blankLine: 'any',
prev: ['const', 'let', 'var'],
next: ['const', 'let', 'var'],
},
// Require blank lines before all return statements
{
blankLine: 'always',
prev: '*',
next: 'return',
},
// Require blank lines before and after all following statements
{
blankLine: 'always',
prev: '*',
next: ['for', 'function', 'if', 'switch', 'try'],
},
{
blankLine: 'always',
prev: ['for', 'function', 'if', 'switch', 'try'],
next: '*',
},
],
'prefer-arrow-callback': 'error',
'prefer-object-spread': 'error',
'prefer-regex-literals': 'error',
'prefer-rest-params': 'error',
'prefer-spread': 'error',
'prefer-template': 'error',
'sort-imports': ['error', { allowSeparatedGroups: true }],

// Prefer code readability, e.g. `[0-9A-Za-z]`.
'regexp/prefer-d': 'off',
},
};
5 changes: 5 additions & 0 deletions jest.js
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
extends: ['plugin:jest/recommended', 'plugin:jest/style'],
};

0 comments on commit 43367b2

Please sign in to comment.