diff --git a/bin/api-docs/are-api-docs-unstaged.js b/bin/api-docs/are-api-docs-unstaged.js index b7d956b9e145c3..ba6c9733db4a58 100644 --- a/bin/api-docs/are-api-docs-unstaged.js +++ b/bin/api-docs/are-api-docs-unstaged.js @@ -1,7 +1,7 @@ #!/usr/bin/env node /** - * Node dependencies. + * External dependencies */ const { extname } = require( 'path' ); const chalk = require( 'chalk' ); diff --git a/bin/generate-public-grammar.js b/bin/generate-public-grammar.js index ff3b09398de1a6..d66577adbc4d02 100755 --- a/bin/generate-public-grammar.js +++ b/bin/generate-public-grammar.js @@ -1,5 +1,13 @@ #!/usr/bin/env node + +/** + * Internal dependencies + */ const parser = require( '../node_modules/pegjs/lib/parser.js' ); + +/** + * External dependencies + */ const fs = require( 'fs' ); const path = require( 'path' ); const grammarSource = fs.readFileSync( diff --git a/bin/plugin/commands/changelog.js b/bin/plugin/commands/changelog.js index 330bca157f2427..8bacd4d951fe6c 100644 --- a/bin/plugin/commands/changelog.js +++ b/bin/plugin/commands/changelog.js @@ -1,10 +1,10 @@ -/* +/** * External dependencies */ const { groupBy } = require( 'lodash' ); const Octokit = require( '@octokit/rest' ); -/* +/** * Internal dependencies */ const { getNextMajorVersion } = require( '../lib/version' ); diff --git a/bin/plugin/commands/common.js b/bin/plugin/commands/common.js index 5d7e77f838ad15..d32b687720605b 100644 --- a/bin/plugin/commands/common.js +++ b/bin/plugin/commands/common.js @@ -5,7 +5,7 @@ const fs = require( 'fs' ); const rimraf = require( 'rimraf' ); const semver = require( 'semver' ); -/* +/** * Internal dependencies */ const { log, formats } = require( '../lib/logger' ); diff --git a/bin/plugin/lib/utils.js b/bin/plugin/lib/utils.js index 4a67b1950cea6c..2acebd804cf959 100644 --- a/bin/plugin/lib/utils.js +++ b/bin/plugin/lib/utils.js @@ -9,7 +9,7 @@ const { v4: uuid } = require( 'uuid' ); const path = require( 'path' ); const os = require( 'os' ); -/* +/** * Internal dependencies */ const { log, formats } = require( './logger' ); diff --git a/docs/tool/index.js b/docs/tool/index.js index d7e03d00974b3b..f8b5b36a9ea84d 100644 --- a/docs/tool/index.js +++ b/docs/tool/index.js @@ -1,5 +1,5 @@ /** - * Node dependencies + * External dependencies */ const fs = require( 'fs' ); const path = require( 'path' ); diff --git a/docs/tool/manifest.js b/docs/tool/manifest.js index 90c050388a0c50..44e7b2d5612176 100644 --- a/docs/tool/manifest.js +++ b/docs/tool/manifest.js @@ -1,5 +1,5 @@ /** - * Node dependencies + * External dependencies */ const { camelCase, nth, upperFirst } = require( 'lodash' ); const fs = require( 'fs' ); diff --git a/package-lock.json b/package-lock.json index 0473318676eb70..5bfd29f5bad323 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9071,6 +9071,22 @@ "resolved": "https://registry.npmjs.org/@types/color-name/-/color-name-1.1.1.tgz", "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==" }, + "@types/eslint": { + "version": "6.8.0", + "resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-6.8.0.tgz", + "integrity": "sha512-hqzmggoxkOubpgTdcOltkfc5N8IftRJqU70d1jbOISjjZVPvjcr+CLi2CI70hx1SUIRkLgpglTy9w28nGe2Hsw==", + "dev": true, + "requires": { + "@types/estree": "*", + "@types/json-schema": "*" + } + }, + "@types/estree": { + "version": "0.0.44", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.44.tgz", + "integrity": "sha512-iaIVzr+w2ZJ5HkidlZ3EJM8VTZb2MJLCjw3V+505yVts0gRC4UMvjw0d1HPtGqI/HQC/KdsYtayfzl+AXY2R8g==", + "dev": true + }, "@types/events": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/@types/events/-/events-3.0.0.tgz", diff --git a/package.json b/package.json index 8aa8a1b7887dab..77bc1d70591ca7 100644 --- a/package.json +++ b/package.json @@ -89,6 +89,8 @@ "@storybook/react": "5.3.2", "@testing-library/react": "10.0.2", "@types/classnames": "2.2.10", + "@types/eslint": "6.8.0", + "@types/estree": "0.0.44", "@types/lodash": "4.14.149", "@types/prettier": "1.19.0", "@types/qs": "6.9.1", diff --git a/packages/block-serialization-spec-parser/bin/create-php-parser.js b/packages/block-serialization-spec-parser/bin/create-php-parser.js index 8152f098491989..3b6439c75b52b4 100755 --- a/packages/block-serialization-spec-parser/bin/create-php-parser.js +++ b/packages/block-serialization-spec-parser/bin/create-php-parser.js @@ -1,5 +1,8 @@ #!/usr/bin/env node +/** + * External dependencies + */ const pegjs = require( 'pegjs' ); const phpegjs = require( 'phpegjs' ); const fs = require( 'fs' ); diff --git a/packages/custom-templated-path-webpack-plugin/test/fixtures/webpack.config.js b/packages/custom-templated-path-webpack-plugin/test/fixtures/webpack.config.js index a050642f040895..db4d2840f0f110 100644 --- a/packages/custom-templated-path-webpack-plugin/test/fixtures/webpack.config.js +++ b/packages/custom-templated-path-webpack-plugin/test/fixtures/webpack.config.js @@ -1,13 +1,11 @@ /** * External dependencies */ - const { basename } = require( 'path' ); /** - * Internal dependencies + * WordPress dependencies */ - const CustomTemplatedPathPlugin = require( '@wordpress/custom-templated-path-webpack-plugin' ); module.exports = { diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/combine-assets/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/combine-assets/webpack.config.js index 1c0e557b6d1e6e..2ce7ba1be98e25 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/combine-assets/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/combine-assets/webpack.config.js @@ -1,3 +1,6 @@ +/** + * Internal dependencies + */ const DependencyExtractionWebpackPlugin = require( '../../..' ); module.exports = { diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/dynamic-import/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/dynamic-import/webpack.config.js index ca6a3e9e85a75d..bfffff3ae78319 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/dynamic-import/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/dynamic-import/webpack.config.js @@ -1,3 +1,6 @@ +/** + * Internal dependencies + */ const DependencyExtractionWebpackPlugin = require( '../../..' ); module.exports = { diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/function-output-filename/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/function-output-filename/webpack.config.js index 1d533980ad52be..2d5b2e43b735ec 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/function-output-filename/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/function-output-filename/webpack.config.js @@ -1,3 +1,6 @@ +/** + * Internal dependencies + */ const DependencyExtractionWebpackPlugin = require( '../../..' ); module.exports = { diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/no-default/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/no-default/webpack.config.js index 4a751034233d0c..ed202e27044fff 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/no-default/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/no-default/webpack.config.js @@ -1,3 +1,6 @@ +/** + * Internal dependencies + */ const DependencyExtractionWebpackPlugin = require( '../../..' ); module.exports = { diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/no-deps/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/no-deps/webpack.config.js index ca6a3e9e85a75d..bfffff3ae78319 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/no-deps/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/no-deps/webpack.config.js @@ -1,3 +1,6 @@ +/** + * Internal dependencies + */ const DependencyExtractionWebpackPlugin = require( '../../..' ); module.exports = { diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/output-format-json/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/output-format-json/webpack.config.js index e4713c3a3778f8..b82f422f949f81 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/output-format-json/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/output-format-json/webpack.config.js @@ -1,3 +1,6 @@ +/** + * Internal dependencies + */ const DependencyExtractionWebpackPlugin = require( '../../..' ); module.exports = { diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/overrides/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/overrides/webpack.config.js index f61163ae384602..9885e5cade7e96 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/overrides/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/overrides/webpack.config.js @@ -1,3 +1,6 @@ +/** + * Internal dependencies + */ const DependencyExtractionWebpackPlugin = require( '../../..' ); module.exports = { diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/with-externs/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/with-externs/webpack.config.js index fe7038fe04a1ec..a1e17a2c06e2f3 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/with-externs/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/with-externs/webpack.config.js @@ -1,3 +1,6 @@ +/** + * Internal dependencies + */ const DependencyExtractionWebpackPlugin = require( '../../..' ); module.exports = { diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-require/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-require/webpack.config.js index ca6a3e9e85a75d..bfffff3ae78319 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-require/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-require/webpack.config.js @@ -1,3 +1,6 @@ +/** + * Internal dependencies + */ const DependencyExtractionWebpackPlugin = require( '../../..' ); module.exports = { diff --git a/packages/docgen/bin/cli.js b/packages/docgen/bin/cli.js index 7707b417d2f52d..1ed2f8441af111 100755 --- a/packages/docgen/bin/cli.js +++ b/packages/docgen/bin/cli.js @@ -1,5 +1,8 @@ #!/usr/bin/env node +/** + * Internal dependencies + */ const docgen = require( '../src' ); const optionator = require( 'optionator' )( { diff --git a/packages/docgen/src/engine.js b/packages/docgen/src/engine.js index a6f4ce646193b4..71d6436c462e8d 100644 --- a/packages/docgen/src/engine.js +++ b/packages/docgen/src/engine.js @@ -1,11 +1,11 @@ /** - * External dependencies. + * External dependencies */ const babel = require( '@babel/core' ); const { flatten } = require( 'lodash' ); /** - * Internal dependencies. + * Internal dependencies */ const getIntermediateRepresentation = require( './get-intermediate-representation' ); diff --git a/packages/docgen/src/get-intermediate-representation.js b/packages/docgen/src/get-intermediate-representation.js index 725cdc1ab8e5fe..d4c1dc8eb3b850 100644 --- a/packages/docgen/src/get-intermediate-representation.js +++ b/packages/docgen/src/get-intermediate-representation.js @@ -1,10 +1,10 @@ /** - * External dependencies. + * External dependencies */ const { get } = require( 'lodash' ); /** - * Internal dependencies. + * Internal dependencies */ const getExportEntries = require( './get-export-entries' ); const getJSDocFromToken = require( './get-jsdoc-from-token' ); diff --git a/packages/docgen/src/get-jsdoc-from-token.js b/packages/docgen/src/get-jsdoc-from-token.js index e9cbc361c3bc52..49b4c5625d4171 100644 --- a/packages/docgen/src/get-jsdoc-from-token.js +++ b/packages/docgen/src/get-jsdoc-from-token.js @@ -1,10 +1,10 @@ /** - * External dependencies. + * External dependencies */ const doctrine = require( 'doctrine' ); /** - * Internal dependencies. + * Internal dependencies */ const getLeadingComments = require( './get-leading-comments' ); const getTypeAsString = require( './get-type-as-string' ); diff --git a/packages/docgen/src/get-leading-comments.js b/packages/docgen/src/get-leading-comments.js index 3f025278b10337..95b330162a74b0 100644 --- a/packages/docgen/src/get-leading-comments.js +++ b/packages/docgen/src/get-leading-comments.js @@ -1,5 +1,5 @@ /** - * External dependencies. + * External dependencies */ const { last } = require( 'lodash' ); diff --git a/packages/docgen/src/markdown/index.js b/packages/docgen/src/markdown/index.js index 6e73f31582eeb5..34b0e4420c0fa9 100644 --- a/packages/docgen/src/markdown/index.js +++ b/packages/docgen/src/markdown/index.js @@ -1,5 +1,5 @@ /** - * External dependencies. + * External dependencies */ const remark = require( 'remark' ); const unified = require( 'unified' ); @@ -8,7 +8,7 @@ const inject = require( 'mdast-util-inject' ); const fs = require( 'fs' ); /** - * Internal dependencies. + * Internal dependencies */ const formatter = require( './formatter' ); const embed = require( './embed' ); diff --git a/packages/docgen/src/test/engine.js b/packages/docgen/src/test/engine.js index 3a9efc921a2f01..47bb04bdbc5a74 100644 --- a/packages/docgen/src/test/engine.js +++ b/packages/docgen/src/test/engine.js @@ -1,5 +1,5 @@ /** - * Internal dependencies. + * Internal dependencies */ const engine = require( '../engine' ); diff --git a/packages/docgen/src/test/formatter-markdown.js b/packages/docgen/src/test/formatter-markdown.js index e88fc75ce17f7e..3e40fa725334b2 100644 --- a/packages/docgen/src/test/formatter-markdown.js +++ b/packages/docgen/src/test/formatter-markdown.js @@ -1,5 +1,5 @@ /** - * Internal dependencies. + * Internal dependencies */ const formatter = require( '../markdown/formatter' ); diff --git a/packages/docgen/src/test/get-export-entries.js b/packages/docgen/src/test/get-export-entries.js index 10bc46916494bc..9f3285433e1183 100644 --- a/packages/docgen/src/test/get-export-entries.js +++ b/packages/docgen/src/test/get-export-entries.js @@ -1,11 +1,11 @@ /** - * Node dependencies. + * External dependencies */ const fs = require( 'fs' ); const path = require( 'path' ); /** - * Internal dependencies. + * Internal dependencies */ const getExportEntries = require( '../get-export-entries' ); diff --git a/packages/docgen/src/test/get-intermediate-representation.js b/packages/docgen/src/test/get-intermediate-representation.js index ab1914dbd397f4..00da71f6e597e7 100644 --- a/packages/docgen/src/test/get-intermediate-representation.js +++ b/packages/docgen/src/test/get-intermediate-representation.js @@ -1,11 +1,11 @@ /** - * Node dependencies. + * External dependencies */ const fs = require( 'fs' ); const path = require( 'path' ); /** - * Internal dependencies. + * Internal dependencies */ const getIntermediateRepresentation = require( '../get-intermediate-representation' ); diff --git a/packages/docgen/src/test/get-jsdoc-from-token.js b/packages/docgen/src/test/get-jsdoc-from-token.js index 3475e69938187e..f3946d8f0e7469 100644 --- a/packages/docgen/src/test/get-jsdoc-from-token.js +++ b/packages/docgen/src/test/get-jsdoc-from-token.js @@ -1,5 +1,5 @@ /** - * Internal dependencies. + * Internal dependencies */ const getJSDocFromToken = require( '../get-jsdoc-from-token' ); diff --git a/packages/docgen/src/test/get-type-as-string.js b/packages/docgen/src/test/get-type-as-string.js index 6ba09acd33df18..f317c02e1b8d37 100644 --- a/packages/docgen/src/test/get-type-as-string.js +++ b/packages/docgen/src/test/get-type-as-string.js @@ -1,5 +1,5 @@ /** - * Internal dependencies. + * Internal dependencies */ const getType = require( '../get-type-as-string' ); diff --git a/packages/e2e-tests/config/performance-reporter.js b/packages/e2e-tests/config/performance-reporter.js index 32ea362973ed1f..7353a9597207fc 100644 --- a/packages/e2e-tests/config/performance-reporter.js +++ b/packages/e2e-tests/config/performance-reporter.js @@ -1,3 +1,6 @@ +/** + * External dependencies + */ const { readFileSync, existsSync } = require( 'fs' ); const chalk = require( 'chalk' ); diff --git a/packages/eslint-plugin/CHANGELOG.md b/packages/eslint-plugin/CHANGELOG.md index 4e4c3b7dd97818..712acb256834e2 100644 --- a/packages/eslint-plugin/CHANGELOG.md +++ b/packages/eslint-plugin/CHANGELOG.md @@ -2,11 +2,15 @@ ## Unreleased +### Bug Fixes + +- `@wordpress/dependency-group` will now correctly identify issues associated with CommonJS (`require`) module imports. + ## 6.0.0 (2020-05-14) ### Breaking Changes -- The severity of the rule, `jsdoc/no-undefined-types`, has been increased from `warn` to `error`. In addition, `JSX` has been added to the default list of defined types. +- The severity of the rule, `jsdoc/no-undefined-types`, has been increased from `warn` to `error`. In addition, `JSX` has been added to the default list of defined types. ### Improvements diff --git a/packages/eslint-plugin/rules/__tests__/dependency-group.js b/packages/eslint-plugin/rules/__tests__/dependency-group.js index b81d172f26199e..0f0731c96323d7 100644 --- a/packages/eslint-plugin/rules/__tests__/dependency-group.js +++ b/packages/eslint-plugin/rules/__tests__/dependency-group.js @@ -35,6 +35,24 @@ import { Component } from '@wordpress/element'; */ import edit from './edit';`, }, + { + code: ` +/** + * External dependencies + */ +const { get } = require( 'lodash' ); +const classnames = require( 'classnames' ); + +/** + * WordPress dependencies + */ +const { Component } = require( '@wordpress/element' ); + +/** + * Internal dependencies + */ +const edit = require( './edit' );`, + }, ], invalid: [ { @@ -75,5 +93,43 @@ import { Component } from '@wordpress/element'; */ import edit from './edit';`, }, + { + code: ` +const { get } = require( 'lodash' ); +const classnames = require( 'classnames' ); +/* + * wordpress dependencies. + */ +const { Component } = require( '@wordpress/element' ); +const edit = require( './edit' );`, + errors: [ + { + message: + 'Expected preceding "External dependencies" comment block', + }, + { + message: + 'Expected preceding "WordPress dependencies" comment block', + }, + { + message: + 'Expected preceding "Internal dependencies" comment block', + }, + ], + output: ` +/** + * External dependencies + */ +const { get } = require( 'lodash' ); +const classnames = require( 'classnames' ); +/** + * WordPress dependencies + */ +const { Component } = require( '@wordpress/element' ); +/** + * Internal dependencies + */ +const edit = require( './edit' );`, + }, ], } ); diff --git a/packages/eslint-plugin/rules/dependency-group.js b/packages/eslint-plugin/rules/dependency-group.js index 8881cb28ef6ee2..311a2bf37c37fe 100644 --- a/packages/eslint-plugin/rules/dependency-group.js +++ b/packages/eslint-plugin/rules/dependency-group.js @@ -1,4 +1,7 @@ -module.exports = { +/** @typedef {import('estree').Comment} Comment */ +/** @typedef {import('estree').Node} Node */ + +module.exports = /** @type {import('eslint').Rule.RuleModule} */ ( { meta: { type: 'layout', docs: { @@ -7,7 +10,7 @@ module.exports = { 'https://github.com/WordPress/gutenberg/blob/master/packages/eslint-plugin/docs/rules/dependency-group.md', }, schema: [], - fixable: true, + fixable: 'code', }, create( context ) { const comments = context.getSourceCode().getAllComments(); @@ -22,11 +25,11 @@ module.exports = { /** * Object describing a dependency block correction to be made. * - * @property {?espree.Node} comment Comment node on which to replace - * value, if one can be salvaged. - * @property {string} value Expected comment node value. + * @typedef WPDependencyBlockCorrection * - * @typedef {Object} WPDependencyBlockCorrection + * @property {Comment} [comment] Comment node on which to replace value, + * if one can be salvaged. + * @property {string} value Expected comment node value. */ /** @@ -63,7 +66,7 @@ module.exports = { * Returns true if the given comment node satisfies a desired locality, * or false otherwise. * - * @param {espree.Node} node Comment node to check. + * @param {Comment} node Comment node to check. * @param {WPPackageLocality} locality Desired package locality. * * @return {boolean} Whether comment node satisfies locality. @@ -95,13 +98,17 @@ module.exports = { * Returns true if the given node occurs prior in code to a reference, * or false otherwise. * - * @param {espree.Node} node Node to test being before reference. - * @param {espree.Node} reference Node against which to compare. + * @param {Comment} node Node to test being before reference. + * @param {Node} reference Node against which to compare. * * @return {boolean} Whether node occurs before reference. */ function isBefore( node, reference ) { - return node.start < reference.start; + if ( ! node.range || ! reference.range ) { + return false; + } + + return node.range[ 0 ] < reference.range[ 0 ]; } /** @@ -110,10 +117,10 @@ module.exports = { * updates, the function returns undefined. Otherwise, it will return * a WPDependencyBlockCorrection object describing a correction. * - * @param {espree.Node} node Node to test. + * @param {Node} node Node to test. * @param {WPPackageLocality} locality Desired package locality. * - * @return {?WPDependencyBlockCorrection} Correction, if applicable. + * @return {WPDependencyBlockCorrection=} Correction, if applicable. */ function getDependencyBlockCorrection( node, locality ) { const value = getCommentValue( locality ); @@ -146,6 +153,9 @@ module.exports = { } return { + /** + * @param {import('estree').Program} node Program node. + */ Program( node ) { /** * The set of package localities which have been reported for @@ -157,36 +167,58 @@ module.exports = { */ const verified = new Set(); + /** + * Nodes to check for violations associated with module import, + * an array of tuples of the node and its import source string. + * + * @type {Array<[Node,string]>} + */ + const candidates = []; + // Since we only care to enforce imports which occur at the // top-level scope, match on Program and test its children, // rather than matching the import nodes directly. node.body.forEach( ( child ) => { + /** @type {string} */ let source; switch ( child.type ) { case 'ImportDeclaration': - source = child.source.value; + source = + /** @type {string} */ ( child.source.value ); + candidates.push( [ child, source ] ); break; - case 'CallExpression': - const { callee, arguments: args } = child; - if ( - callee.name === 'require' && - args.length === 1 && - args[ 0 ].type === 'Literal' && - typeof args[ 0 ].value === 'string' - ) { - source = args[ 0 ].value; - } - break; - } + case 'VariableDeclaration': + child.declarations.forEach( ( declaration ) => { + const { init } = declaration; + if ( + ! init || + init.type !== 'CallExpression' || + /** @type {import('estree').CallExpression} */ ( init ) + .callee.type !== 'Identifier' || + /** @type {import('estree').Identifier} */ ( init + .callee ).name !== 'require' + ) { + return; + } - if ( ! source ) { - return; + const { arguments: args } = init; + if ( + args.length === 1 && + args[ 0 ].type === 'Literal' && + typeof args[ 0 ].value === 'string' + ) { + source = args[ 0 ].value; + candidates.push( [ child, source ] ); + } + } ); } + } ); + for ( const [ child, source ] of candidates ) { const locality = getPackageLocality( source ); if ( verified.has( locality ) ) { - return; + continue; } // Avoid verifying any other imports for the locality, @@ -199,7 +231,7 @@ module.exports = { locality ); if ( ! correction ) { - return; + continue; } context.report( { @@ -208,15 +240,18 @@ module.exports = { fix( fixer ) { const { comment, value } = correction; const text = `/*${ value }*/`; - if ( comment ) { - return fixer.replaceText( comment, text ); + if ( comment && comment.range ) { + return fixer.replaceTextRange( + comment.range, + text + ); } return fixer.insertTextBefore( child, text + '\n' ); }, } ); - } ); + } }, }; }, -}; +} ); diff --git a/packages/eslint-plugin/tsconfig.json b/packages/eslint-plugin/tsconfig.json new file mode 100644 index 00000000000000..0f0a4598184f80 --- /dev/null +++ b/packages/eslint-plugin/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "rules", + "declarationDir": "build-types" + }, + // NOTE: This package is being progressively typed. You are encouraged to + // expand this array with files which can be type-checked. At some point in + // the future, this can be simplified to an `includes` of `src/**/*`. + "files": [ + "rules/dependency-group.js" + ] +} diff --git a/packages/eslint-plugin/utils/index.js b/packages/eslint-plugin/utils/index.js index 97daaaa3208dd3..4e517ac5ff2292 100644 --- a/packages/eslint-plugin/utils/index.js +++ b/packages/eslint-plugin/utils/index.js @@ -1,3 +1,6 @@ +/** + * Internal dependencies + */ const { TRANSLATION_FUNCTIONS, REGEXP_SPRINTF_PLACEHOLDER, diff --git a/packages/hooks/benchmark/index.js b/packages/hooks/benchmark/index.js index 6b54e16035bd54..0541b380f80fc3 100644 --- a/packages/hooks/benchmark/index.js +++ b/packages/hooks/benchmark/index.js @@ -1,4 +1,11 @@ +/** + * External dependencies + */ const Benchmark = require( 'benchmark' ); + +/** + * Internal dependencies + */ const hooks = require( '../' ); const suite = new Benchmark.Suite(); diff --git a/packages/i18n/benchmark/index.js b/packages/i18n/benchmark/index.js index f26b59038bf338..64a961c52d7d0f 100644 --- a/packages/i18n/benchmark/index.js +++ b/packages/i18n/benchmark/index.js @@ -1,4 +1,11 @@ +/** + * External dependencies + */ const Benchmark = require( 'benchmark' ); + +/** + * Internal dependencies + */ const { __ } = require( '../' ); const suite = new Benchmark.Suite(); diff --git a/packages/i18n/tools/pot-to-php.js b/packages/i18n/tools/pot-to-php.js index b8a1b92819498c..7917f5b62a4bc3 100755 --- a/packages/i18n/tools/pot-to-php.js +++ b/packages/i18n/tools/pot-to-php.js @@ -1,5 +1,8 @@ #!/usr/bin/env node +/** + * External dependencies + */ const gettextParser = require( 'gettext-parser' ); const { isEmpty } = require( 'lodash' ); const fs = require( 'fs' ); diff --git a/packages/library-export-default-webpack-plugin/test/fixtures/webpack.config.js b/packages/library-export-default-webpack-plugin/test/fixtures/webpack.config.js index df1810cc331e16..f02819026df7a3 100644 --- a/packages/library-export-default-webpack-plugin/test/fixtures/webpack.config.js +++ b/packages/library-export-default-webpack-plugin/test/fixtures/webpack.config.js @@ -1,5 +1,5 @@ /** - * Internal dependencies + * WordPress dependencies */ const LibraryExportDefaultPlugin = require( '@wordpress/library-export-default-webpack-plugin' ); diff --git a/packages/project-management-automation/lib/index.js b/packages/project-management-automation/lib/index.js index 2b280d1d5d5248..f7c05dd593cd56 100644 --- a/packages/project-management-automation/lib/index.js +++ b/packages/project-management-automation/lib/index.js @@ -1,5 +1,5 @@ /** - * GitHub dependencies + * External dependencies */ const { setFailed, getInput } = require( '@actions/core' ); const { context, GitHub } = require( '@actions/github' ); diff --git a/packages/scripts/scripts/env/cli.js b/packages/scripts/scripts/env/cli.js index 6fa336da96ac62..2d2a19192139ea 100644 --- a/packages/scripts/scripts/env/cli.js +++ b/packages/scripts/scripts/env/cli.js @@ -1,5 +1,5 @@ /** - * Node dependencies. + * External dependencies */ const { execSync } = require( 'child_process' ); const { env } = require( 'process' ); diff --git a/packages/scripts/scripts/env/docker-run.js b/packages/scripts/scripts/env/docker-run.js index 26faa06d18a3c6..02bf57eb2f8db2 100644 --- a/packages/scripts/scripts/env/docker-run.js +++ b/packages/scripts/scripts/env/docker-run.js @@ -1,5 +1,5 @@ /** - * Node dependencies. + * External dependencies */ const { execSync } = require( 'child_process' ); const { env } = require( 'process' ); diff --git a/packages/scripts/scripts/env/lint-php.js b/packages/scripts/scripts/env/lint-php.js index 125114a515bff8..9d489afa554c05 100644 --- a/packages/scripts/scripts/env/lint-php.js +++ b/packages/scripts/scripts/env/lint-php.js @@ -1,5 +1,5 @@ /** - * Node dependencies. + * External dependencies */ const { execSync } = require( 'child_process' ); const { env } = require( 'process' ); diff --git a/packages/scripts/scripts/env/reinstall.js b/packages/scripts/scripts/env/reinstall.js index a2cfc8cbadf1a7..8a0bf5c2f1821a 100644 --- a/packages/scripts/scripts/env/reinstall.js +++ b/packages/scripts/scripts/env/reinstall.js @@ -1,5 +1,5 @@ /** - * Node dependencies. + * External dependencies */ const { execSync } = require( 'child_process' ); diff --git a/packages/scripts/scripts/env/stop.js b/packages/scripts/scripts/env/stop.js index f0218c606411d8..17066e22cdd5ef 100644 --- a/packages/scripts/scripts/env/stop.js +++ b/packages/scripts/scripts/env/stop.js @@ -1,5 +1,5 @@ /** - * Node dependencies. + * External dependencies */ const { execSync } = require( 'child_process' ); const { env } = require( 'process' ); diff --git a/packages/scripts/scripts/env/test-php.js b/packages/scripts/scripts/env/test-php.js index 5c9a10cac75ce9..469c1f1717b08c 100644 --- a/packages/scripts/scripts/env/test-php.js +++ b/packages/scripts/scripts/env/test-php.js @@ -1,5 +1,5 @@ /** - * Node dependencies. + * External dependencies */ const { execSync } = require( 'child_process' ); const { env } = require( 'process' ); diff --git a/packages/scripts/scripts/format-js.js b/packages/scripts/scripts/format-js.js index 60b75a12d82bc8..ad57a24a20c195 100644 --- a/packages/scripts/scripts/format-js.js +++ b/packages/scripts/scripts/format-js.js @@ -1,5 +1,5 @@ /** - * Node dependencies + * External dependencies */ const { exit, stdout } = require( 'process' ); diff --git a/test/unit/scripts/babel-transformer.js b/test/unit/scripts/babel-transformer.js index 34c8dcea3017b2..7638370f1ba694 100644 --- a/test/unit/scripts/babel-transformer.js +++ b/test/unit/scripts/babel-transformer.js @@ -1,3 +1,6 @@ +/** + * External dependencies + */ const fs = require( 'fs' ); const babelJest = require( 'babel-jest' ); const babelJestTransformer = babelJest.createTransformer(); diff --git a/tsconfig.json b/tsconfig.json index 28f48647436b52..1c803518b91cd2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,6 +8,7 @@ { "path": "packages/element" }, { "path": "packages/dom-ready" }, { "path": "packages/escape-html" }, + { "path": "packages/eslint-plugin" }, { "path": "packages/html-entities" }, { "path": "packages/i18n" }, { "path": "packages/icons" },