@automattic/eslint-plugin-wpvip 0.5.3 → 0.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -33,8 +33,6 @@ You may also wish to define an `.eslintignore` file if there are files or paths
33
33
 
34
34
  The "recommended" config includes rules for JavaScript, TypeScript, Jest, and React, including rules related to formatting and white space. It is intended to be strict! Opinionated defaults keep our codebases consistent and reduce the friction we experience when context-switching between projects.
35
35
 
36
- If your project has installed [Prettier](https://prettier.io/) as a dependency, then many formatting tasks will be delegated to it (via `eslint-plugin-prettier`). You are encouraged to define your own [`.prettierrc` configuration file](https://prettier.io/docs/en/configuration.html) to fine-tune your project’s formatting.
37
-
38
36
  Of course, this recommended config may not be ideal for every project, so feel free to "build your own" using the available modular configs. The recommended config is equivalent to:
39
37
 
40
38
  ```js
@@ -52,6 +50,22 @@ module.exports = {
52
50
 
53
51
  Note that the order of configs can matter, since they can contain overrides. It is particularly important to add the `prettier` config last.
54
52
 
53
+ ### Prettier
54
+
55
+ Install [WP Prettier](https://github.com/Automattic/wp-prettier) to benefit from additional formatting rules:
56
+
57
+ ```sh
58
+ npm i --save-dev --save-exact "prettier@npm:wp-prettier@latest"
59
+ ```
60
+
61
+ This repo also provides a Prettier config, which you can use with the following `.prettierrc`:
62
+
63
+ ```json
64
+ "@automattic/eslint-plugin-wpvip/prettierrc"
65
+ ```
66
+
67
+ For maximum benefit, see [Prettier's documentation on enabling format-on-save in your editor](https://prettier.io/docs/en/editors.html). This enables you to concentrate on coding while Prettier handles formatting.
68
+
55
69
  ## CLI
56
70
 
57
71
  The `cli` config allows certain behaviors that are usually against best practice but are useful in a codebase that produces a CLI tool:
@@ -94,4 +108,4 @@ This plugin provides a few so-called "weak" configs for legacy codebases that ar
94
108
 
95
109
  These configs are intended for temporary use and should not be used long-term. We also do not recommend the use of tools like [eslines](https://github.com/Automattic/eslines) to ignore errors or warnings. While the intention is to prevent large-scale changes and transition slowly to stronger standards, the effect is usually that the transition stalls and eventually stops completely.
96
110
 
97
- Three "weak" configs are available: `weak-javascript`, `weak-typescript`, and `weak-jest`. While pull requests on this project are always welcome, please carefully consider whether adding rules to these configs is truly necessary. Ideally, we work to remove rules from these configs until they are no longer needed.
111
+ Three "weak" configs are available: `weak-javascript`, `weak-typescript`, and `weak-testing`. While pull requests on this project are always welcome, please carefully consider whether adding rules to these configs is truly necessary. Ideally, we work to remove rules from these configs until they are no longer needed.
package/configs/base.js CHANGED
@@ -1 +1 @@
1
- module.exports = require('./javascript');
1
+ module.exports = require( './javascript' );
@@ -4,13 +4,13 @@ module.exports = {
4
4
  * disable errors, include a brief justification or reasoning.
5
5
  */
6
6
  rules: {
7
- 'array-bracket-spacing': ['error', 'always'],
7
+ 'array-bracket-spacing': [ 'error', 'always' ],
8
8
 
9
- 'arrow-parens': ['error', 'always'],
9
+ 'arrow-parens': [ 'error', 'always' ],
10
10
 
11
11
  'arrow-spacing': 'error',
12
12
 
13
- 'brace-style': ['error', '1tbs'],
13
+ 'brace-style': [ 'error', '1tbs' ],
14
14
 
15
15
  // Identifiers should be in camelCase. Object properties are excluded
16
16
  // (including when destructuring) since they often come from external
@@ -23,24 +23,24 @@ module.exports = {
23
23
  },
24
24
  ],
25
25
 
26
- 'comma-dangle': ['error', 'always-multiline'],
26
+ 'comma-dangle': [ 'error', 'always-multiline' ],
27
27
 
28
28
  'comma-spacing': 'error',
29
29
 
30
- 'comma-style': ['error', 'last'],
30
+ 'comma-style': [ 'error', 'last' ],
31
31
 
32
- 'computed-property-spacing': ['error', 'always'],
32
+ 'computed-property-spacing': [ 'error', 'always' ],
33
33
 
34
- curly: ['error', 'all'],
34
+ curly: [ 'error', 'all' ],
35
35
 
36
36
  'dot-notation': 'error',
37
37
 
38
38
  // Files must end in a newline.
39
- 'eol-last': ['error', 'always'],
39
+ 'eol-last': [ 'error', 'always' ],
40
40
 
41
41
  'func-call-spacing': 'error',
42
42
 
43
- indent: ['error', 'tab', { SwitchCase: 1 }],
43
+ indent: [ 'error', 'tab', { SwitchCase: 1 } ],
44
44
 
45
45
  'key-spacing': 'error',
46
46
 
@@ -58,51 +58,47 @@ module.exports = {
58
58
 
59
59
  'no-multi-str': 'error',
60
60
 
61
- 'no-multiple-empty-lines': ['error', { max: 1 }],
61
+ 'no-multiple-empty-lines': [ 'error', { max: 1 } ],
62
62
 
63
63
  'no-trailing-spaces': 'error',
64
64
 
65
65
  'no-whitespace-before-property': 'error',
66
66
 
67
- 'object-curly-spacing': ['error', 'always'],
67
+ 'object-curly-spacing': [ 'error', 'always' ],
68
68
 
69
69
  'object-shorthand': 'error',
70
70
 
71
71
  'operator-linebreak': 'error',
72
72
 
73
- 'padded-blocks': ['error', 'never'],
73
+ 'padded-blocks': [ 'error', 'never' ],
74
74
 
75
75
  // Arrow functions should be used for function arguments and callbacks.
76
76
  'prefer-arrow-callback': 'warn',
77
77
 
78
- quotes: [
79
- 'error',
80
- 'single',
81
- { allowTemplateLiterals: true, avoidEscape: true },
82
- ],
78
+ quotes: [ 'error', 'single', { allowTemplateLiterals: true, avoidEscape: true } ],
83
79
 
84
- 'quote-props': ['error', 'as-needed'],
80
+ 'quote-props': [ 'error', 'as-needed' ],
85
81
 
86
82
  semi: 'error',
87
83
 
88
84
  'semi-spacing': 'error',
89
85
 
90
- 'space-before-blocks': ['error', 'always'],
86
+ 'space-before-blocks': [ 'error', 'always' ],
91
87
 
92
88
  'space-before-function-paren': [
93
89
  'error',
94
90
  { anonymous: 'never', named: 'never', asyncArrow: 'always' },
95
91
  ],
96
92
 
97
- 'space-in-parens': ['error', 'always'],
93
+ 'space-in-parens': [ 'error', 'always' ],
98
94
 
99
95
  'space-infix-ops': 'error',
100
96
 
101
- 'space-unary-ops': ['error', { overrides: { '!': true, yield: true } }],
97
+ 'space-unary-ops': [ 'error', { overrides: { '!': true, yield: true } } ],
102
98
 
103
99
  // Comments should always include consistent spacing for readability.
104
100
  'spaced-comment': 'warn',
105
101
 
106
- 'template-curly-spacing': ['error', 'always'],
102
+ 'template-curly-spacing': [ 'error', 'always' ],
107
103
  },
108
104
  };
package/configs/index.js CHANGED
@@ -1,15 +1,15 @@
1
1
  module.exports = {
2
- base: require('./base'), // synonym for javascript
3
- cli: require('./cli'),
4
- formatting: require('./formatting'),
5
- javascript: require('./javascript'),
6
- jsdoc: require('./jsdoc'),
7
- prettier: require('./prettier'),
8
- react: require('./react'),
9
- recommended: require('./recommended'),
10
- testing: require('./testing'),
11
- typescript: require('./typescript'),
12
- 'weak-javascript': require('./weak-javascript'),
13
- 'weak-testing': require('./weak-testing'),
14
- 'weak-typescript': require('./weak-typescript'),
2
+ base: require( './base' ), // synonym for javascript
3
+ cli: require( './cli' ),
4
+ formatting: require( './formatting' ),
5
+ javascript: require( './javascript' ),
6
+ jsdoc: require( './jsdoc' ),
7
+ prettier: require( './prettier' ),
8
+ react: require( './react' ),
9
+ recommended: require( './recommended' ),
10
+ testing: require( './testing' ),
11
+ typescript: require( './typescript' ),
12
+ 'weak-javascript': require( './weak-javascript' ),
13
+ 'weak-testing': require( './weak-testing' ),
14
+ 'weak-typescript': require( './weak-typescript' ),
15
15
  };
@@ -12,7 +12,7 @@ module.exports = {
12
12
  node: true,
13
13
  },
14
14
 
15
- extends: ['plugin:json/recommended', 'plugin:security/recommended'],
15
+ extends: [ 'plugin:json/recommended', 'plugin:security/recommended' ],
16
16
 
17
17
  parser: '@babel/eslint-parser',
18
18
 
@@ -24,7 +24,7 @@ module.exports = {
24
24
  /**
25
25
  * Note: We must explicitly add this plugin to use our custom rules.
26
26
  */
27
- plugins: ['@automattic/wpvip', 'import'],
27
+ plugins: [ '@automattic/wpvip', 'import' ],
28
28
 
29
29
  /**
30
30
  * Please include a short description of the rule. For rules that downgrade or
@@ -135,7 +135,7 @@ module.exports = {
135
135
 
136
136
  // Enforce Unix linebreaks. Included here and not in "formatting" since it
137
137
  // is not controversial and helps with interchange.
138
- 'linebreak-style': ['error', 'unix'],
138
+ 'linebreak-style': [ 'error', 'unix' ],
139
139
 
140
140
  'no-alert': 'error',
141
141
 
@@ -147,7 +147,7 @@ module.exports = {
147
147
 
148
148
  'no-caller': 'error',
149
149
 
150
- 'no-cond-assign': ['error', 'except-parens'],
150
+ 'no-cond-assign': [ 'error', 'except-parens' ],
151
151
 
152
152
  // `console.log` should not be used directly in code. Ideally, delegate to a
153
153
  // logging function that logs on your behalf (and ignore this rule there).
@@ -159,7 +159,7 @@ module.exports = {
159
159
 
160
160
  'no-else-return': 'error',
161
161
 
162
- 'no-empty': ['error', { allowEmptyCatch: true }],
162
+ 'no-empty': [ 'error', { allowEmptyCatch: true } ],
163
163
 
164
164
  'no-eq-null': 'error',
165
165
 
@@ -177,7 +177,7 @@ module.exports = {
177
177
 
178
178
  'no-unused-expressions': 'error',
179
179
 
180
- 'no-unused-vars': ['error', { ignoreRestSiblings: true }],
180
+ 'no-unused-vars': [ 'error', { ignoreRestSiblings: true } ],
181
181
 
182
182
  'no-useless-computed-key': 'error',
183
183
 
@@ -187,9 +187,9 @@ module.exports = {
187
187
 
188
188
  'no-var': 'error',
189
189
 
190
- 'one-var': ['error', 'never'],
190
+ 'one-var': [ 'error', 'never' ],
191
191
 
192
- 'prefer-const': ['error', { destructuring: 'all' }],
192
+ 'prefer-const': [ 'error', { destructuring: 'all' } ],
193
193
 
194
194
  radix: 'error',
195
195
 
@@ -201,13 +201,13 @@ module.exports = {
201
201
  },
202
202
  ],
203
203
 
204
- 'wrap-iife': ['error', 'any'],
204
+ 'wrap-iife': [ 'error', 'any' ],
205
205
  },
206
206
 
207
207
  settings: {
208
208
  'import/resolver': {
209
209
  node: {
210
- extensions: ['.js', '.jsx', '.ts', '.tsx'],
210
+ extensions: [ '.js', '.jsx', '.ts', '.tsx' ],
211
211
  },
212
212
  typescript: 'eslint-import-resolver-typescript',
213
213
  },
package/configs/jsdoc.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * https://github.com/WordPress/gutenberg/blob/%40wordpress/eslint-plugin%4014.1.0/packages/eslint-plugin/configs/jsdoc.js
4
4
  */
5
5
 
6
- const globals = require('globals');
6
+ const globals = require( 'globals' );
7
7
 
8
8
  /**
9
9
  * Helpful utilities that are globally defined and known to the TypeScript compiler.
@@ -41,7 +41,7 @@ const typescriptUtilityTypes = [
41
41
  ];
42
42
 
43
43
  module.exports = {
44
- extends: ['plugin:jsdoc/recommended'],
44
+ extends: [ 'plugin:jsdoc/recommended' ],
45
45
  settings: {
46
46
  jsdoc: {
47
47
  preferredTypes: {
@@ -61,7 +61,7 @@ module.exports = {
61
61
  // Required to reference browser types because we don't have the `browser` environment enabled for the project.
62
62
  // Here we filter out all browser globals that don't begin with an uppercase letter because those
63
63
  // generally refer to window-level event listeners and are not a valid type to reference (e.g. `onclick`).
64
- ...Object.keys(globals.browser).filter((key) => /^[A-Z]/.test(key)),
64
+ ...Object.keys( globals.browser ).filter( key => /^[A-Z]/.test( key ) ),
65
65
  ...typescriptUtilityTypes,
66
66
  'void',
67
67
  'JSX',
@@ -73,14 +73,14 @@ module.exports = {
73
73
  'jsdoc/require-returns': 'off',
74
74
  'jsdoc/require-yields': 'off',
75
75
  'jsdoc/tag-lines': 'off',
76
- 'jsdoc/no-multi-asterisks': ['error', { preventAtMiddleLines: false }],
76
+ 'jsdoc/no-multi-asterisks': [ 'error', { preventAtMiddleLines: false } ],
77
77
  'jsdoc/check-access': 'error',
78
78
  'jsdoc/check-alignment': 'error',
79
79
  'jsdoc/check-line-alignment': [
80
80
  'error',
81
81
  'always',
82
82
  {
83
- tags: ['param', 'arg', 'argument', 'property', 'prop'],
83
+ tags: [ 'param', 'arg', 'argument', 'property', 'prop' ],
84
84
  preserveMainDescriptionPostDelimiter: true,
85
85
  },
86
86
  ],
@@ -4,8 +4,8 @@
4
4
  */
5
5
 
6
6
  module.exports = {
7
- extends: ['plugin:prettier/recommended'],
8
- plugins: ['prettier'],
7
+ extends: [ 'plugin:prettier/recommended' ],
8
+ plugins: [ 'prettier' ],
9
9
  rules: {
10
10
  'prettier/prettier': 'error',
11
11
  },
package/configs/react.js CHANGED
@@ -22,7 +22,7 @@ module.exports = {
22
22
  },
23
23
  },
24
24
 
25
- plugins: ['@automattic/wpvip', 'jsx-a11y', 'react', 'react-hooks'],
25
+ plugins: [ '@automattic/wpvip', 'jsx-a11y', 'react', 'react-hooks' ],
26
26
 
27
27
  rules: {
28
28
  'jsx-a11y/label-has-associated-control': [
@@ -52,9 +52,9 @@ module.exports = {
52
52
 
53
53
  'react/jsx-equals-spacing': 'error',
54
54
 
55
- 'react/jsx-indent': ['error', 'tab'],
55
+ 'react/jsx-indent': [ 'error', 'tab' ],
56
56
 
57
- 'react/jsx-indent-props': ['error', 'tab'],
57
+ 'react/jsx-indent-props': [ 'error', 'tab' ],
58
58
 
59
59
  'react/jsx-key': 'error',
60
60
 
@@ -1,28 +1,28 @@
1
- const debugLog = require('../utils/debug-log');
2
- const isPackageInstalled = require('../utils/is-package-installed');
1
+ const debugLog = require( '../utils/debug-log' );
2
+ const isPackageInstalled = require( '../utils/is-package-installed' );
3
3
 
4
4
  const config = {
5
- extends: [require.resolve('./javascript')],
5
+ extends: [ require.resolve( './javascript' ) ],
6
6
  };
7
7
 
8
- if (isPackageInstalled('typescript')) {
9
- config.extends.push(require.resolve('./typescript'));
8
+ if ( isPackageInstalled( 'typescript' ) ) {
9
+ config.extends.push( require.resolve( './typescript' ) );
10
10
  }
11
11
 
12
- config.extends.push(require.resolve('./formatting'));
12
+ config.extends.push( require.resolve( './formatting' ) );
13
13
 
14
- if (isPackageInstalled('jest')) {
15
- config.extends.push(require.resolve('./testing'));
14
+ if ( isPackageInstalled( 'jest' ) ) {
15
+ config.extends.push( require.resolve( './testing' ) );
16
16
  }
17
17
 
18
- if (isPackageInstalled('react')) {
19
- config.extends.push(require.resolve('./react'));
18
+ if ( isPackageInstalled( 'react' ) ) {
19
+ config.extends.push( require.resolve( './react' ) );
20
20
  }
21
21
 
22
- if (isPackageInstalled('prettier')) {
23
- config.extends.push(require.resolve('./prettier'));
22
+ if ( isPackageInstalled( 'prettier' ) ) {
23
+ config.extends.push( require.resolve( './prettier' ) );
24
24
  }
25
25
 
26
- debugLog(`Using the following configs:\n${config.extends.join('\n')}`);
26
+ debugLog( `Using the following configs:\n${ config.extends.join( '\n' ) }` );
27
27
 
28
28
  module.exports = config;
@@ -5,11 +5,11 @@
5
5
  */
6
6
 
7
7
  module.exports = {
8
- extends: ['plugin:jest/recommended'],
8
+ extends: [ 'plugin:jest/recommended' ],
9
9
 
10
10
  env: {
11
11
  'jest/globals': true,
12
12
  },
13
13
 
14
- plugins: ['jest'],
14
+ plugins: [ 'jest' ],
15
15
  };
@@ -4,7 +4,7 @@
4
4
  */
5
5
 
6
6
  module.exports = {
7
- ignorePatterns: ['**/*.d.ts'],
7
+ ignorePatterns: [ '**/*.d.ts' ],
8
8
 
9
9
  overrides: [
10
10
  {
@@ -14,7 +14,7 @@ module.exports = {
14
14
  'plugin:@typescript-eslint/strict',
15
15
  ],
16
16
 
17
- files: ['**/*.ts', '**/*.tsx'],
17
+ files: [ '**/*.ts', '**/*.tsx' ],
18
18
 
19
19
  parser: '@typescript-eslint/parser',
20
20
 
@@ -58,5 +58,5 @@ module.exports = {
58
58
  },
59
59
  ],
60
60
 
61
- plugins: ['@typescript-eslint', 'jsdoc'],
61
+ plugins: [ '@typescript-eslint', 'jsdoc' ],
62
62
  };
@@ -10,7 +10,7 @@ module.exports = {
10
10
  overrides: [
11
11
  {
12
12
  // Don't apply weak rules to TypeScript files.
13
- files: ['**/*.js', '**/*.jsx'],
13
+ files: [ '**/*.js', '**/*.jsx' ],
14
14
 
15
15
  /**
16
16
  * Downgrade rules from the base preset to "warn". Do not disable rules (set
@@ -7,7 +7,7 @@
7
7
  module.exports = {
8
8
  overrides: [
9
9
  {
10
- files: ['**/*.ts', '**/*.tsx'],
10
+ files: [ '**/*.ts', '**/*.tsx' ],
11
11
 
12
12
  /**
13
13
  * Downgrade rules from the base preset to "warn". Do not disable rules (set
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  module.exports = {
2
- configs: require('./configs'),
3
- rules: require('./rules'),
2
+ configs: require( './configs' ),
3
+ rules: require( './rules' ),
4
4
  };
package/init.js CHANGED
@@ -1 +1 @@
1
- require('@rushstack/eslint-patch/modern-module-resolution');
1
+ require( '@rushstack/eslint-patch/modern-module-resolution' );
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@automattic/eslint-plugin-wpvip",
3
- "version": "0.5.3",
3
+ "version": "0.5.5",
4
4
  "description": "ESLint plugin for internal WordPress VIP projects",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "check-types": "tsc __tests__/**/*.ts",
8
+ "format": "prettier --ignore-path .gitignore --ignore-path .prettierignore --write '**/*.{js,ts}'",
8
9
  "jest": "npm run link-plugin && jest",
9
10
  "jest:update-snapshot": "jest --updateSnapshot",
10
11
  "link-plugin": "mkdir -p ./node_modules/@automattic; ln -fns $(pwd) ./node_modules/@automattic/eslint-plugin-wpvip",
@@ -53,7 +54,7 @@
53
54
  "@types/jest": "29.5.0",
54
55
  "eslint": "8.35.0",
55
56
  "jest": "29.5.0",
56
- "prettier": "2.8.4",
57
+ "prettier": "npm:wp-prettier@2.8.5",
57
58
  "ts-jest": "29.0.5",
58
59
  "typescript": "4.9.5"
59
60
  }
package/prettierrc.js ADDED
@@ -0,0 +1,11 @@
1
+ module.exports = {
2
+ arrowParens: 'avoid',
3
+ bracketSpacing: true,
4
+ jsxBracketSameLine: false,
5
+ parenSpacing: true,
6
+ printWidth: 100,
7
+ semi: true,
8
+ singleQuote: true,
9
+ trailingComma: 'es5',
10
+ useTabs: true,
11
+ };
@@ -17,7 +17,7 @@ module.exports = {
17
17
  schema: [],
18
18
  fixable: 'code',
19
19
  },
20
- create(context) {
20
+ create( context ) {
21
21
  const comments = context.getSourceCode().getAllComments();
22
22
 
23
23
  /**
@@ -45,8 +45,8 @@ module.exports = {
45
45
  *
46
46
  * @return {string} Expected comment node value.
47
47
  */
48
- function getCommentValue(locality) {
49
- return `*\n * ${locality} dependencies\n `;
48
+ function getCommentValue( locality ) {
49
+ return `*\n * ${ locality } dependencies\n `;
50
50
  }
51
51
 
52
52
  /**
@@ -57,10 +57,10 @@ module.exports = {
57
57
  *
58
58
  * @return {WPPackageLocality} Package locality.
59
59
  */
60
- function getPackageLocality(source) {
61
- if (source.startsWith('.')) {
60
+ function getPackageLocality( source ) {
61
+ if ( source.startsWith( '.' ) ) {
62
62
  return 'Internal';
63
- } else if (source.startsWith('@wordpress/')) {
63
+ } else if ( source.startsWith( '@wordpress/' ) ) {
64
64
  return 'WordPress';
65
65
  }
66
66
 
@@ -76,9 +76,9 @@ module.exports = {
76
76
  *
77
77
  * @return {boolean} Whether comment node satisfies locality.
78
78
  */
79
- function isLocalityDependencyBlock(node, locality) {
79
+ function isLocalityDependencyBlock( node, locality ) {
80
80
  const { type, value } = node;
81
- if (type !== 'Block') {
81
+ if ( type !== 'Block' ) {
82
82
  return false;
83
83
  }
84
84
 
@@ -88,16 +88,13 @@ module.exports = {
88
88
  // - Ending period
89
89
  // - "Node" dependencies as an alias for External.
90
90
 
91
- if (locality === 'External') {
91
+ if ( locality === 'External' ) {
92
92
  locality = '(External|Node)';
93
93
  }
94
94
 
95
95
  // eslint-disable-next-line security/detect-non-literal-regexp
96
- const pattern = new RegExp(
97
- `^\\*?\\n \\* ${locality} dependencies\\.?\\n $`,
98
- 'i'
99
- );
100
- return pattern.test(value);
96
+ const pattern = new RegExp( `^\\*?\\n \\* ${ locality } dependencies\\.?\\n $`, 'i' );
97
+ return pattern.test( value );
101
98
  }
102
99
 
103
100
  /**
@@ -109,12 +106,12 @@ module.exports = {
109
106
  *
110
107
  * @return {boolean} Whether node occurs before reference.
111
108
  */
112
- function isBefore(node, reference) {
113
- if (!node.range || !reference.range) {
109
+ function isBefore( node, reference ) {
110
+ if ( ! node.range || ! reference.range ) {
114
111
  return false;
115
112
  }
116
113
 
117
- return node.range[0] < reference.range[0];
114
+ return node.range[ 0 ] < reference.range[ 0 ];
118
115
  }
119
116
 
120
117
  /**
@@ -128,22 +125,22 @@ module.exports = {
128
125
  *
129
126
  * @return {WPDependencyBlockCorrection | undefined} Correction, if applicable.
130
127
  */
131
- function getDependencyBlockCorrection(node, locality) {
132
- const value = getCommentValue(locality);
128
+ function getDependencyBlockCorrection( node, locality ) {
129
+ const value = getCommentValue( locality );
133
130
 
134
- for (const comment of comments) {
135
- if (!isBefore(comment, node)) {
131
+ for ( const comment of comments ) {
132
+ if ( ! isBefore( comment, node ) ) {
136
133
  // Exhausted options.
137
134
  break;
138
135
  }
139
136
 
140
- if (!isLocalityDependencyBlock(comment, locality)) {
137
+ if ( ! isLocalityDependencyBlock( comment, locality ) ) {
141
138
  // Not usable (either not an block comment, or not one
142
139
  // matching a tolerable pattern).
143
140
  continue;
144
141
  }
145
142
 
146
- if (comment.value === value) {
143
+ if ( comment.value === value ) {
147
144
  // No change needed. (OK)
148
145
  return;
149
146
  }
@@ -159,7 +156,7 @@ module.exports = {
159
156
  /**
160
157
  * @param {import('estree').Program} node Program node.
161
158
  */
162
- Program(node) {
159
+ Program( node ) {
163
160
  /**
164
161
  * The set of package localities which have been reported for
165
162
  * the current program. Each locality is reported at most one
@@ -181,25 +178,24 @@ module.exports = {
181
178
  // Since we only care to enforce imports which occur at the
182
179
  // top-level scope, match on Program and test its children,
183
180
  // rather than matching the import nodes directly.
184
- node.body.forEach((child) => {
181
+ node.body.forEach( child => {
185
182
  /** @type {string} */
186
183
  let source;
187
- switch (child.type) {
184
+ switch ( child.type ) {
188
185
  case 'ImportDeclaration':
189
- source = /** @type {string} */ (child.source.value);
190
- candidates.push([child, source]);
186
+ source = /** @type {string} */ ( child.source.value );
187
+ candidates.push( [ child, source ] );
191
188
  break;
192
189
 
193
190
  case 'VariableDeclaration':
194
- child.declarations.forEach((declaration) => {
191
+ child.declarations.forEach( declaration => {
195
192
  const { init } = declaration;
196
193
  if (
197
- !init ||
194
+ ! init ||
198
195
  init.type !== 'CallExpression' ||
199
- /** @type {import('estree').CallExpression} */ (init).callee
200
- .type !== 'Identifier' ||
201
- /** @type {import('estree').Identifier} */ (init.callee)
202
- .name !== 'require'
196
+ /** @type {import('estree').CallExpression} */ ( init ).callee.type !==
197
+ 'Identifier' ||
198
+ /** @type {import('estree').Identifier} */ ( init.callee ).name !== 'require'
203
199
  ) {
204
200
  return;
205
201
  }
@@ -207,45 +203,45 @@ module.exports = {
207
203
  const { arguments: args } = init;
208
204
  if (
209
205
  args.length === 1 &&
210
- args[0].type === 'Literal' &&
211
- typeof args[0].value === 'string'
206
+ args[ 0 ].type === 'Literal' &&
207
+ typeof args[ 0 ].value === 'string'
212
208
  ) {
213
- source = args[0].value;
214
- candidates.push([child, source]);
209
+ source = args[ 0 ].value;
210
+ candidates.push( [ child, source ] );
215
211
  }
216
- });
212
+ } );
217
213
  }
218
- });
214
+ } );
219
215
 
220
- for (const [child, source] of candidates) {
221
- const locality = getPackageLocality(source);
222
- if (verified.has(locality)) {
216
+ for ( const [ child, source ] of candidates ) {
217
+ const locality = getPackageLocality( source );
218
+ if ( verified.has( locality ) ) {
223
219
  continue;
224
220
  }
225
221
 
226
222
  // Avoid verifying any other imports for the locality,
227
223
  // regardless whether a correction must be made.
228
- verified.add(locality);
224
+ verified.add( locality );
229
225
 
230
226
  // Determine whether a correction must be made.
231
- const correction = getDependencyBlockCorrection(child, locality);
232
- if (!correction) {
227
+ const correction = getDependencyBlockCorrection( child, locality );
228
+ if ( ! correction ) {
233
229
  continue;
234
230
  }
235
231
 
236
- context.report({
232
+ context.report( {
237
233
  node: child,
238
- message: `Expected preceding "${locality} dependencies" comment block`,
239
- fix(fixer) {
234
+ message: `Expected preceding "${ locality } dependencies" comment block`,
235
+ fix( fixer ) {
240
236
  const { comment, value } = correction;
241
- const text = `/*${value}*/`;
242
- if (comment && comment.range) {
243
- return fixer.replaceTextRange(comment.range, text);
237
+ const text = `/*${ value }*/`;
238
+ if ( comment && comment.range ) {
239
+ return fixer.replaceTextRange( comment.range, text );
244
240
  }
245
241
 
246
- return fixer.insertTextBefore(child, text + '\n');
242
+ return fixer.insertTextBefore( child, text + '\n' );
247
243
  },
248
- });
244
+ } );
249
245
  }
250
246
  },
251
247
  };
package/rules/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * Custom ESLint rules
3
3
  */
4
4
  module.exports = {
5
- 'dependency-group': require('./dependency-group'),
6
- 'no-async-foreach': require('./no-async-foreach'),
7
- 'no-unguarded-get-range-at': require('./no-unguarded-get-range-at'),
5
+ 'dependency-group': require( './dependency-group' ),
6
+ 'no-async-foreach': require( './no-async-foreach' ),
7
+ 'no-unguarded-get-range-at': require( './no-unguarded-get-range-at' ),
8
8
  };
@@ -8,26 +8,20 @@
8
8
  */
9
9
 
10
10
  module.exports = {
11
- create(context) {
11
+ create( context ) {
12
12
  return {
13
- ExpressionStatement(node) {
13
+ ExpressionStatement( node ) {
14
14
  const { callee } = node.expression;
15
- if (!callee || !callee.property || !callee.property.name) {
15
+ if ( ! callee || ! callee.property || ! callee.property.name ) {
16
16
  return;
17
17
  }
18
- if (callee.property.name === 'forEach') {
19
- const functionArguments = node.expression.arguments.find((exp) => {
20
- return (
21
- exp.type === 'ArrowFunctionExpression' ||
22
- exp.type === 'FunctionExpression'
23
- );
24
- });
25
- if (functionArguments) {
26
- if (functionArguments.async) {
27
- context.report(
28
- node,
29
- 'Avoid passing an async function to Array.prototype.forEach'
30
- );
18
+ if ( callee.property.name === 'forEach' ) {
19
+ const functionArguments = node.expression.arguments.find( exp => {
20
+ return exp.type === 'ArrowFunctionExpression' || exp.type === 'FunctionExpression';
21
+ } );
22
+ if ( functionArguments ) {
23
+ if ( functionArguments.async ) {
24
+ context.report( node, 'Avoid passing an async function to Array.prototype.forEach' );
31
25
  }
32
26
  }
33
27
  }
@@ -8,15 +8,15 @@ module.exports = {
8
8
  type: 'problem',
9
9
  schema: [],
10
10
  },
11
- create(context) {
11
+ create( context ) {
12
12
  return {
13
13
  'CallExpression[callee.object.callee.property.name="getSelection"][callee.property.name="getRangeAt"]'(
14
14
  node
15
15
  ) {
16
- context.report({
16
+ context.report( {
17
17
  node,
18
18
  message: 'Avoid unguarded getRangeAt',
19
- });
19
+ } );
20
20
  },
21
21
  };
22
22
  },
@@ -1,7 +1,7 @@
1
- module.exports = function debugLog(message) {
2
- if (process.env.DEBUG) {
1
+ module.exports = function debugLog( message ) {
2
+ if ( process.env.DEBUG ) {
3
3
  // Debugging output.
4
4
  // eslint-disable-next-line no-console
5
- console.log(message);
5
+ console.log( message );
6
6
  }
7
7
  };
@@ -1,14 +1,14 @@
1
- const debugLog = require('./debug-log');
2
- const findPackageJson = require('find-package-json');
1
+ const debugLog = require( './debug-log' );
2
+ const findPackageJson = require( 'find-package-json' );
3
3
 
4
- const packages = [...findPackageJson(__dirname)];
4
+ const packages = [ ...findPackageJson( __dirname ) ];
5
5
  const parent = packages.pop() || {};
6
6
 
7
- debugLog(`Found package.json: ${parent.__path || 'none'}`);
7
+ debugLog( `Found package.json: ${ parent.__path || 'none' }` );
8
8
 
9
- module.exports = function isPackageInstalled(packageName) {
10
- const isDevDependency = !!parent.devDependencies?.[`${packageName}`];
11
- const isProdDependency = !!parent.dependencies?.[`${packageName}`];
9
+ module.exports = function isPackageInstalled( packageName ) {
10
+ const isDevDependency = !! parent.devDependencies?.[ `${ packageName }` ];
11
+ const isProdDependency = !! parent.dependencies?.[ `${ packageName }` ];
12
12
 
13
13
  return isDevDependency || isProdDependency;
14
14
  };