diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d5b23d828..b81ad61a61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,20 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange ## [Unreleased] +## [2.29.1] - 2023-12-14 + +### Fixed +- [`no-extraneous-dependencies`]: ignore `export type { ... } from '...'` when `includeTypes` is `false` ([#2919], thanks [@Pandemic1617]) +- [`no-unused-modules`]: support export patterns with array destructuring ([#2930], thanks [@ljharb]) +- [Deps] update `tsconfig-paths` ([#2447], thanks [@domdomegg]) + +## [2.29.0] - 2023-10-22 + +### Added +- TypeScript config: add .cts and .mts extensions ([#2851], thanks [@Zamiell]) +- [`newline-after-import`]: new option `exactCount` and docs update ([#1933], thanks [@anikethsaha] and [@reosarevok]) +- [`newline-after-import`]: fix `exactCount` with `considerComments` false positive, when there is a leading comment ([#2884], thanks [@kinland]) + ## [2.28.1] - 2023-08-18 ### Fixed @@ -1087,7 +1101,10 @@ for info on changes for earlier releases. [`memo-parser`]: ./memo-parser/README.md +[#2919]: https://github.com/import-js/eslint-plugin-import/pull/2919 +[#2884]: https://github.com/import-js/eslint-plugin-import/pull/2884 [#2854]: https://github.com/import-js/eslint-plugin-import/pull/2854 +[#2851]: https://github.com/import-js/eslint-plugin-import/pull/2851 [#2850]: https://github.com/import-js/eslint-plugin-import/pull/2850 [#2842]: https://github.com/import-js/eslint-plugin-import/pull/2842 [#2835]: https://github.com/import-js/eslint-plugin-import/pull/2835 @@ -1422,6 +1439,8 @@ for info on changes for earlier releases. [#211]: https://github.com/import-js/eslint-plugin-import/pull/211 [#164]: https://github.com/import-js/eslint-plugin-import/pull/164 [#157]: https://github.com/import-js/eslint-plugin-import/pull/157 + +[#2930]: https://github.com/import-js/eslint-plugin-import/issues/2930 [#2687]: https://github.com/import-js/eslint-plugin-import/issues/2687 [#2684]: https://github.com/import-js/eslint-plugin-import/issues/2684 [#2674]: https://github.com/import-js/eslint-plugin-import/issues/2674 @@ -1429,6 +1448,7 @@ for info on changes for earlier releases. [#2666]: https://github.com/import-js/eslint-plugin-import/issues/2666 [#2665]: https://github.com/import-js/eslint-plugin-import/issues/2665 [#2577]: https://github.com/import-js/eslint-plugin-import/issues/2577 +[#2447]: https://github.com/import-js/eslint-plugin-import/issues/2447 [#2444]: https://github.com/import-js/eslint-plugin-import/issues/2444 [#2412]: https://github.com/import-js/eslint-plugin-import/issues/2412 [#2392]: https://github.com/import-js/eslint-plugin-import/issues/2392 @@ -1547,7 +1567,9 @@ for info on changes for earlier releases. [#119]: https://github.com/import-js/eslint-plugin-import/issues/119 [#89]: https://github.com/import-js/eslint-plugin-import/issues/89 -[Unreleased]: https://github.com/import-js/eslint-plugin-import/compare/v2.28.1...HEAD +[Unreleased]: https://github.com/import-js/eslint-plugin-import/compare/v2.29.1...HEAD +[2.29.1]: https://github.com/import-js/eslint-plugin-import/compare/v2.29.0...v2.29.1 +[2.29.0]: https://github.com/import-js/eslint-plugin-import/compare/v2.28.1...v2.29.0 [2.28.1]: https://github.com/import-js/eslint-plugin-import/compare/v2.28.0...v2.28.1 [2.28.0]: https://github.com/import-js/eslint-plugin-import/compare/v2.27.5...v2.28.0 [2.27.5]: https://github.com/import-js/eslint-plugin-import/compare/v2.27.4...v2.27.5 @@ -1698,6 +1720,7 @@ for info on changes for earlier releases. [@devinrhode2]: https://github.com/devinrhode2 [@devongovett]: https://github.com/devongovett [@dmnd]: https://github.com/dmnd +[@domdomegg]: https://github.com/domdomegg [@duncanbeevers]: https://github.com/duncanbeevers [@dwardu]: https://github.com/dwardu [@echenley]: https://github.com/echenley @@ -1764,6 +1787,7 @@ for info on changes for earlier releases. [@kentcdodds]: https://github.com/kentcdodds [@kevin940726]: https://github.com/kevin940726 [@kgregory]: https://github.com/kgregory +[@kinland]: https://github.com/kinland [@kirill-konshin]: https://github.com/kirill-konshin [@kiwka]: https://github.com/kiwka [@klimashkin]: https://github.com/klimashkin @@ -1771,8 +1795,8 @@ for info on changes for earlier releases. [@knpwrs]: https://github.com/knpwrs [@KostyaZgara]: https://github.com/KostyaZgara [@kylemh]: https://github.com/kylemh -[@laysent]: https://github.com/laysent [@laurens-dg]: https://github.com/laurens-dg +[@laysent]: https://github.com/laysent [@le0nik]: https://github.com/le0nik [@leipert]: https://github.com/leipert [@lemonmade]: https://github.com/lemonmade @@ -1819,6 +1843,7 @@ for info on changes for earlier releases. [@ntdb]: https://github.com/ntdb [@nwalters512]: https://github.com/nwalters512 [@ombene]: https://github.com/ombene +[@Pandemic1617]: https://github.com/Pandemic1617 [@ota-meshi]: https://github.com/ota-meshi [@OutdatedVersion]: https://github.com/OutdatedVersion [@panrafal]: https://github.com/panrafal @@ -1896,4 +1921,5 @@ for info on changes for earlier releases. [@xpl]: https://github.com/xpl [@yndajas]: https://github.com/yndajas [@yordis]: https://github.com/yordis +[@Zamiell]: https://github.com/Zamiell [@zloirock]: https://github.com/zloirock diff --git a/config/typescript.js b/config/typescript.js index 9fd789dbf7..ff7d0795c8 100644 --- a/config/typescript.js +++ b/config/typescript.js @@ -1,19 +1,22 @@ /** - * Adds `.jsx`, `.ts` and `.tsx` as an extension, and enables JSX/TSX parsing. + * This config: + * 1) adds `.jsx`, `.ts`, `.cts`, `.mts`, and `.tsx` as an extension + * 2) enables JSX/TSX parsing */ // Omit `.d.ts` because 1) TypeScript compilation already confirms that // types are resolved, and 2) it would mask an unresolved // `.ts`/`.tsx`/`.js`/`.jsx` implementation. -const allExtensions = ['.ts', '.tsx', '.js', '.jsx']; +const typeScriptExtensions = ['.ts', '.cts', '.mts', '.tsx']; -module.exports = { +const allExtensions = [...typeScriptExtensions, '.js', '.jsx']; +module.exports = { settings: { 'import/extensions': allExtensions, 'import/external-module-folders': ['node_modules', 'node_modules/@types'], 'import/parsers': { - '@typescript-eslint/parser': ['.ts', '.tsx'], + '@typescript-eslint/parser': typeScriptExtensions, }, 'import/resolver': { node: { diff --git a/docs/rules/newline-after-import.md b/docs/rules/newline-after-import.md index fec6b23aca..ef5aeed767 100644 --- a/docs/rules/newline-after-import.md +++ b/docs/rules/newline-after-import.md @@ -12,70 +12,119 @@ This rule supports the following options: - `count` which sets the number of newlines that are enforced after the last top-level import statement or require call. This option defaults to `1`. + - `exactCount` which enforce the exact numbers of newlines that is mentioned in `count`. This option defaults to `false`. + - `considerComments` which enforces the rule on comments after the last import-statement as well when set to true. This option defaults to `false`. Valid: ```js -import defaultExport from './foo' +import defaultExport from './foo'; -const FOO = 'BAR' +const FOO = 'BAR'; ``` ```js -import defaultExport from './foo' -import { bar } from 'bar-lib' +import defaultExport from './foo'; +import { bar } from 'bar-lib'; -const FOO = 'BAR' +const FOO = 'BAR'; ``` ```js -const FOO = require('./foo') -const BAR = require('./bar') +const FOO = require('./foo'); +const BAR = require('./bar'); -const BAZ = 1 +const BAZ = 1; ``` Invalid: ```js import * as foo from 'foo' -const FOO = 'BAR' +const FOO = 'BAR'; ``` ```js -import * as foo from 'foo' -const FOO = 'BAR' +import * as foo from 'foo'; +const FOO = 'BAR'; -import { bar } from 'bar-lib' +import { bar } from 'bar-lib'; ``` ```js -const FOO = require('./foo') -const BAZ = 1 -const BAR = require('./bar') +const FOO = require('./foo'); +const BAZ = 1; +const BAR = require('./bar'); ``` With `count` set to `2` this will be considered valid: ```js -import defaultExport from './foo' +import defaultExport from './foo'; -const FOO = 'BAR' +const FOO = 'BAR'; +``` + +```js +import defaultExport from './foo'; + + + +const FOO = 'BAR'; ``` With `count` set to `2` these will be considered invalid: ```js -import defaultExport from './foo' -const FOO = 'BAR' +import defaultExport from './foo'; +const FOO = 'BAR'; ``` ```js -import defaultExport from './foo' +import defaultExport from './foo'; -const FOO = 'BAR' +const FOO = 'BAR'; +``` + +With `count` set to `2` and `exactCount` set to `true` this will be considered valid: + +```js +import defaultExport from './foo'; + + +const FOO = 'BAR'; +``` + +With `count` set to `2` and `exactCount` set to `true` these will be considered invalid: + +```js +import defaultExport from './foo'; +const FOO = 'BAR'; +``` + +```js +import defaultExport from './foo'; + +const FOO = 'BAR'; +``` + +```js +import defaultExport from './foo'; + + + +const FOO = 'BAR'; +``` + +```js +import defaultExport from './foo'; + + + + +const FOO = 'BAR'; ``` With `considerComments` set to `false` this will be considered valid: diff --git a/package.json b/package.json index 6128a4e76b..5c0af48543 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eslint-plugin-import", - "version": "2.28.1", + "version": "2.29.1", "description": "Import with sanity.", "engines": { "node": ">=4" @@ -69,11 +69,11 @@ "babel-preset-flow": "^6.23.0", "babel-register": "^6.26.0", "babylon": "^6.18.0", - "chai": "^4.3.7", + "chai": "^4.3.10", "cross-env": "^4.0.0", "escope": "^3.6.0", "eslint": "^2 || ^3 || ^4 || ^5 || ^6 || ^7.2.0 || ^8", - "eslint-doc-generator": "^1.4.3", + "eslint-doc-generator": "^1.6.1", "eslint-import-resolver-node": "file:./resolvers/node", "eslint-import-resolver-typescript": "^1.0.2 || ^1.1.1", "eslint-import-resolver-webpack": "file:./resolvers/webpack", @@ -88,7 +88,7 @@ "jackspeak": "=2.1.1", "linklocal": "^2.8.2", "lodash.isarray": "^4.0.0", - "markdownlint-cli": "^0.35.0", + "markdownlint-cli": "^0.38.0", "mocha": "^3.5.3", "npm-which": "^3.0.1", "nyc": "^11.9.0", @@ -103,22 +103,22 @@ "eslint": "^2 || ^3 || ^4 || ^5 || ^6 || ^7.2.0 || ^8" }, "dependencies": { - "array-includes": "^3.1.6", - "array.prototype.findlastindex": "^1.2.2", - "array.prototype.flat": "^1.3.1", - "array.prototype.flatmap": "^1.3.1", + "array-includes": "^3.1.7", + "array.prototype.findlastindex": "^1.2.3", + "array.prototype.flat": "^1.3.2", + "array.prototype.flatmap": "^1.3.2", "debug": "^3.2.7", "doctrine": "^2.1.0", - "eslint-import-resolver-node": "^0.3.7", + "eslint-import-resolver-node": "^0.3.9", "eslint-module-utils": "^2.8.0", - "has": "^1.0.3", - "is-core-module": "^2.13.0", + "hasown": "^2.0.0", + "is-core-module": "^2.13.1", "is-glob": "^4.0.3", "minimatch": "^3.1.2", - "object.fromentries": "^2.0.6", - "object.groupby": "^1.0.0", - "object.values": "^1.1.6", + "object.fromentries": "^2.0.7", + "object.groupby": "^1.0.1", + "object.values": "^1.1.7", "semver": "^6.3.1", - "tsconfig-paths": "^3.14.2" + "tsconfig-paths": "^3.15.0" } } diff --git a/resolvers/webpack/CHANGELOG.md b/resolvers/webpack/CHANGELOG.md index 457cd36ed4..4fed046b46 100644 --- a/resolvers/webpack/CHANGELOG.md +++ b/resolvers/webpack/CHANGELOG.md @@ -5,6 +5,14 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange ## Unreleased +## 0.13.8 - 2023-10-22 + - [refactor] use `hasown` instead of `has` + - [deps] update `array.prototype.find`, `is-core-module`, `resolve` + + +## 0.13.7 - 2023-08-19 + - [fix] use the `dirname` of the `configPath` as `basedir` ([#2859]) + ## 0.13.6 - 2023-08-16 - [refactor] revert back to `lodash/isEqual` @@ -197,6 +205,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange [#181]: https://github.com/import-js/eslint-plugin-import/pull/181 [#164]: https://github.com/import-js/eslint-plugin-import/pull/164 +[#2859]: https://github.com/import-js/eslint-plugin-import/issues/2859 [#2268]: https://github.com/import-js/eslint-plugin-import/issues/2268 [#1219]: https://github.com/import-js/eslint-plugin-import/issues/1219 [#788]: https://github.com/import-js/eslint-plugin-import/issues/788 diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index 1eea7d7ab5..3ca2874dd8 100644 --- a/resolvers/webpack/index.js +++ b/resolvers/webpack/index.js @@ -9,7 +9,7 @@ const fs = require('fs'); const isCore = require('is-core-module'); const resolve = require('resolve/sync'); const semver = require('semver'); -const has = require('has'); +const hasOwn = require('hasown'); const isRegex = require('is-regex'); const log = require('debug')('eslint-plugin-import:resolver:webpack'); @@ -183,7 +183,7 @@ function createResolveSync(configPath, webpackConfig, cwd) { if (typeof configPath === 'string') { // This can be changed via the settings passed in when defining the resolver - basedir = cwd || configPath; + basedir = cwd || path.dirname(configPath); log(`Attempting to load webpack path from ${basedir}`); } @@ -382,7 +382,7 @@ function findExternal(source, externals, context, resolveSync) { // else, vanilla object for (const key in externals) { - if (!has(externals, key)) { continue; } + if (!hasOwn(externals, key)) { continue; } if (source === key) { return true; } } return false; diff --git a/resolvers/webpack/package.json b/resolvers/webpack/package.json index c3363611b0..3fa47d9362 100644 --- a/resolvers/webpack/package.json +++ b/resolvers/webpack/package.json @@ -1,6 +1,6 @@ { "name": "eslint-import-resolver-webpack", - "version": "0.13.6", + "version": "0.13.8", "description": "Resolve paths to dependencies, given a webpack.config.js. Plugin for eslint-plugin-import.", "main": "index.js", "scripts": { @@ -30,16 +30,16 @@ }, "homepage": "https://github.com/import-js/eslint-plugin-import/tree/HEAD/resolvers/webpack", "dependencies": { - "array.prototype.find": "^2.2.1", + "array.prototype.find": "^2.2.2", "debug": "^3.2.7", "enhanced-resolve": "^0.9.1", "find-root": "^1.1.0", - "has": "^1.0.3", + "hasown": "^2.0.0", "interpret": "^1.4.0", - "is-core-module": "^2.13.0", + "is-core-module": "^2.13.1", "is-regex": "^1.1.4", "lodash": "^4.17.21", - "resolve": "^2.0.0-next.4", + "resolve": "^2.0.0-next.5", "semver": "^5.7.2" }, "peerDependencies": { diff --git a/src/rules/newline-after-import.js b/src/rules/newline-after-import.js index 8855e26e52..a33bb615b9 100644 --- a/src/rules/newline-after-import.js +++ b/src/rules/newline-after-import.js @@ -69,6 +69,7 @@ module.exports = { type: 'integer', minimum: 1, }, + exactCount: { type: 'boolean' }, considerComments: { type: 'boolean' }, }, additionalProperties: false, @@ -78,7 +79,12 @@ module.exports = { create(context) { let level = 0; const requireCalls = []; - const options = { count: 1, considerComments: false, ...context.options[0] }; + const options = { + count: 1, + exactCount: false, + considerComments: false, + ...context.options[0], + }; function checkForNewLine(node, nextNode, type) { if (isExportDefaultClass(nextNode) || isExportNameClass(nextNode)) { @@ -94,7 +100,10 @@ module.exports = { const lineDifference = getLineDifference(node, nextNode); const EXPECTED_LINE_DIFFERENCE = options.count + 1; - if (lineDifference < EXPECTED_LINE_DIFFERENCE) { + if ( + lineDifference < EXPECTED_LINE_DIFFERENCE + || options.exactCount && lineDifference !== EXPECTED_LINE_DIFFERENCE + ) { let column = node.loc.start.column; if (node.loc.start.line !== node.loc.end.line) { @@ -107,7 +116,7 @@ module.exports = { column, }, message: `Expected ${options.count} empty line${options.count > 1 ? 's' : ''} after ${type} statement not followed by another ${type}.`, - fix: (fixer) => fixer.insertTextAfter( + fix: options.exactCount && EXPECTED_LINE_DIFFERENCE < lineDifference ? undefined : (fixer) => fixer.insertTextAfter( node, '\n'.repeat(EXPECTED_LINE_DIFFERENCE - lineDifference), ), @@ -132,7 +141,7 @@ module.exports = { column, }, message: `Expected ${options.count} empty line${options.count > 1 ? 's' : ''} after import statement not followed by another import.`, - fix: (fixer) => fixer.insertTextAfter( + fix: options.exactCount && EXPECTED_LINE_DIFFERENCE < lineDifference ? undefined : (fixer) => fixer.insertTextAfter( node, '\n'.repeat(EXPECTED_LINE_DIFFERENCE - lineDifference), ), @@ -160,7 +169,7 @@ module.exports = { let nextComment; if (typeof parent.comments !== 'undefined' && options.considerComments) { - nextComment = parent.comments.find((o) => o.loc.start.line === endLine + 1); + nextComment = parent.comments.find((o) => o.loc.start.line >= endLine && o.loc.start.line <= endLine + options.count + 1); } // skip "export import"s diff --git a/src/rules/no-anonymous-default-export.js b/src/rules/no-anonymous-default-export.js index 59a3cbfdac..4f6947e814 100644 --- a/src/rules/no-anonymous-default-export.js +++ b/src/rules/no-anonymous-default-export.js @@ -3,7 +3,7 @@ * @author Duncan Beevers */ -import has from 'has'; +import hasOwn from 'hasown'; import values from 'object.values'; import fromEntries from 'object.fromentries'; @@ -65,7 +65,7 @@ const schemaProperties = fromEntries(values(defs).map((def) => [def.option, { type: 'boolean', }])); -const defaults = fromEntries(values(defs).map((def) => [def.option, has(def, 'default') ? def.default : false])); +const defaults = fromEntries(values(defs).map((def) => [def.option, hasOwn(def, 'default') ? def.default : false])); module.exports = { meta: { diff --git a/src/rules/no-extraneous-dependencies.js b/src/rules/no-extraneous-dependencies.js index 0408e0866d..df97987901 100644 --- a/src/rules/no-extraneous-dependencies.js +++ b/src/rules/no-extraneous-dependencies.js @@ -177,6 +177,7 @@ function reportIfMissing(context, deps, depsOptions, node, name) { && ( node.importKind === 'type' || node.importKind === 'typeof' + || node.exportKind === 'type' || Array.isArray(node.specifiers) && node.specifiers.length && node.specifiers.every((specifier) => specifier.importKind === 'type' || specifier.importKind === 'typeof') ) ) { diff --git a/src/rules/no-unused-modules.js b/src/rules/no-unused-modules.js index ecba3a19ce..8229d880ce 100644 --- a/src/rules/no-unused-modules.js +++ b/src/rules/no-unused-modules.js @@ -74,6 +74,7 @@ const FUNCTION_DECLARATION = 'FunctionDeclaration'; const CLASS_DECLARATION = 'ClassDeclaration'; const IDENTIFIER = 'Identifier'; const OBJECT_PATTERN = 'ObjectPattern'; +const ARRAY_PATTERN = 'ArrayPattern'; const TS_INTERFACE_DECLARATION = 'TSInterfaceDeclaration'; const TS_TYPE_ALIAS_DECLARATION = 'TSTypeAliasDeclaration'; const TS_ENUM_DECLARATION = 'TSEnumDeclaration'; @@ -97,6 +98,10 @@ function forEachDeclarationIdentifier(declaration, cb) { cb(pattern.name); } }); + } else if (id.type === ARRAY_PATTERN) { + id.elements.forEach(({ name }) => { + cb(name); + }); } else { cb(id.name); } diff --git a/tests/src/rules/newline-after-import.js b/tests/src/rules/newline-after-import.js index 8f3338eee8..6a8fb83e40 100644 --- a/tests/src/rules/newline-after-import.js +++ b/tests/src/rules/newline-after-import.js @@ -46,6 +46,16 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), { parserOptions: { ecmaVersion: 6 }, options: [{ considerComments: true }], }, + { + code: ` + const x = () => require('baz') && require('bar') + + // Some random single line comment + var bar = 42; + `, + parserOptions: { ecmaVersion: 6 }, + options: [{ considerComments: true, count: 1, exactCount: true }], + }, { code: ` const x = () => require('baz') && require('bar') @@ -122,6 +132,46 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), { parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, options: [{ count: 2 }], }, + { + code: `import foo from 'foo';\n\n\nvar bar = 'bar';`, + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + options: [{ count: 2, exactCount: true }], + }, + { + code: `import foo from 'foo';\n\nvar bar = 'bar';`, + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + options: [{ count: 1, exactCount: true }], + }, + { + code: `import foo from 'foo';\n\n// Some random comment\nvar bar = 'bar';`, + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + options: [{ count: 2, exactCount: true }], + }, + { + code: `import foo from 'foo';\n// Some random comment\nvar bar = 'bar';`, + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + options: [{ count: 1, exactCount: true }], + }, + { + code: `import foo from 'foo';\n\n\n// Some random comment\nvar bar = 'bar';`, + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + options: [{ count: 2, exactCount: true, considerComments: true }], + }, + { + code: `import foo from 'foo';\n\n// Some random comment\nvar bar = 'bar';`, + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + options: [{ count: 1, exactCount: true, considerComments: true }], + }, + { + code: `/**\n * A leading comment\n */\nimport foo from 'foo';\n\n// Some random comment\nexport {foo};`, + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + options: [{ count: 2, exactCount: true }], + }, + { + code: `import foo from 'foo';\n\n\nvar bar = 'bar';`, + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + options: [{ count: 1 }], + }, { code: `import foo from 'foo';\n\n\n\n\nvar bar = 'bar';`, parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, @@ -141,6 +191,21 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), { parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, options: [{ count: 4 }], }, + { + code: `var foo = require('foo-module');\n\n\n\n\nvar foo = 'bar';`, + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + options: [{ count: 4, exactCount: true }], + }, + { + code: `var foo = require('foo-module');\n\n// Some random comment\n\n\nvar foo = 'bar';`, + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + options: [{ count: 4, exactCount: true }], + }, + { + code: `var foo = require('foo-module');\n\n\n\n// Some random comment\nvar foo = 'bar';`, + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + options: [{ count: 4, exactCount: true, considerComments: true }], + }, { code: `require('foo-module');\n\nvar foo = 'bar';`, parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, @@ -620,5 +685,181 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), { parserOptions: { sourceType: 'module' }, parser: parsers.BABEL_OLD, })) || [], + { + code: `import foo from 'foo';\n\nexport default function() {};`, + output: `import foo from 'foo';\n\n\nexport default function() {};`, + options: [{ count: 2, exactCount: true }], + errors: [{ + line: 1, + column: 1, + message: IMPORT_ERROR_MESSAGE_MULTIPLE(2), + }], + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + }, + { + code: `import foo from 'foo';\n\n\n\nexport default function() {};`, + output: `import foo from 'foo';\n\n\n\nexport default function() {};`, + options: [{ count: 2, exactCount: true }], + errors: [{ + line: 1, + column: 1, + message: IMPORT_ERROR_MESSAGE_MULTIPLE(2), + }], + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + }, + { + code: `import foo from 'foo';\n\n\n\n\nexport default function() {};`, + output: `import foo from 'foo';\n\n\n\n\nexport default function() {};`, + options: [{ count: 2, exactCount: true }], + errors: [{ + line: 1, + column: 1, + message: IMPORT_ERROR_MESSAGE_MULTIPLE(2), + }], + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + }, + { + code: `import foo from 'foo';\n// some random comment\nexport default function() {};`, + output: `import foo from 'foo';\n\n// some random comment\nexport default function() {};`, + options: [{ count: 2, exactCount: true }], + errors: [{ + line: 1, + column: 1, + message: IMPORT_ERROR_MESSAGE_MULTIPLE(2), + }], + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + }, + { + code: `import foo from 'foo';\n// some random comment\n\n\nexport default function() {};`, + output: `import foo from 'foo';\n// some random comment\n\n\nexport default function() {};`, + options: [{ count: 2, exactCount: true }], + errors: [{ + line: 1, + column: 1, + message: IMPORT_ERROR_MESSAGE_MULTIPLE(2), + }], + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + }, + { + code: `import foo from 'foo';\n// some random comment\n\n\n\nexport default function() {};`, + output: `import foo from 'foo';\n// some random comment\n\n\n\nexport default function() {};`, + options: [{ count: 2, exactCount: true }], + errors: [{ + line: 1, + column: 1, + message: IMPORT_ERROR_MESSAGE_MULTIPLE(2), + }], + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + }, + { + code: `import foo from 'foo';\n// some random comment\nexport default function() {};`, + output: `import foo from 'foo';\n\n\n// some random comment\nexport default function() {};`, + options: [{ count: 2, exactCount: true, considerComments: true }], + errors: [{ + line: 1, + column: 1, + message: IMPORT_ERROR_MESSAGE_MULTIPLE(2), + }], + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + }, + { + code: `import foo from 'foo';\n\n// some random comment\nexport default function() {};`, + output: `import foo from 'foo';\n\n\n// some random comment\nexport default function() {};`, + options: [{ count: 2, exactCount: true, considerComments: true }], + errors: [{ + line: 1, + column: 1, + message: IMPORT_ERROR_MESSAGE_MULTIPLE(2), + }], + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + }, + { + code: `import foo from 'foo';\n\n\n\n// some random comment\nexport default function() {};`, + output: `import foo from 'foo';\n\n\n\n// some random comment\nexport default function() {};`, + options: [{ count: 2, exactCount: true, considerComments: true }], + errors: [{ + line: 1, + column: 1, + message: IMPORT_ERROR_MESSAGE_MULTIPLE(2), + }], + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + }, + { + code: ` + import foo from 'foo'; + + + // Some random single line comment + var bar = 42; + `, + output: ` + import foo from 'foo'; + + + // Some random single line comment + var bar = 42; + `, + errors: [{ + line: 2, + column: 9, + message: IMPORT_ERROR_MESSAGE, + }], + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + options: [{ considerComments: true, count: 1, exactCount: true }], + }, + { + code: `import foo from 'foo';export default function() {};`, + output: `import foo from 'foo';\n\nexport default function() {};`, + options: [{ count: 1, exactCount: true }], + errors: [{ + line: 1, + column: 1, + message: IMPORT_ERROR_MESSAGE, + }], + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + }, + { + code: `const foo = require('foo');\n\n\n\nconst bar = function() {};`, + output: `const foo = require('foo');\n\n\n\nconst bar = function() {};`, + options: [{ count: 2, exactCount: true }], + errors: [{ + line: 1, + column: 1, + message: 'Expected 2 empty lines after require statement not followed by another require.', + }], + parserOptions: { ecmaVersion: 2015 }, + }, + { + code: `const foo = require('foo');\n\n\n\n// some random comment\nconst bar = function() {};`, + output: `const foo = require('foo');\n\n\n\n// some random comment\nconst bar = function() {};`, + options: [{ count: 2, exactCount: true }], + errors: [{ + line: 1, + column: 1, + message: 'Expected 2 empty lines after require statement not followed by another require.', + }], + parserOptions: { ecmaVersion: 2015 }, + }, + { + code: `import foo from 'foo';// some random comment\nexport default function() {};`, + output: `import foo from 'foo';\n\n// some random comment\nexport default function() {};`, + options: [{ count: 1, exactCount: true, considerComments: true }], + errors: [{ + line: 1, + column: 1, + message: IMPORT_ERROR_MESSAGE, + }], + parserOptions: { ecmaVersion: 2015, considerComments: true, sourceType: 'module' }, + }, + { + code: `const foo = require('foo');\n\n\n// some random comment\nconst bar = function() {};`, + options: [{ count: 2, exactCount: true, considerComments: true }], + errors: [{ + line: 1, + column: 1, + message: 'Expected 2 empty lines after require statement not followed by another require.', + }], + parserOptions: { ecmaVersion: 2015 }, + }, ), }); diff --git a/tests/src/rules/no-duplicates.js b/tests/src/rules/no-duplicates.js index d61fda86e1..f83221105a 100644 --- a/tests/src/rules/no-duplicates.js +++ b/tests/src/rules/no-duplicates.js @@ -482,6 +482,68 @@ import {x,y} from './foo' errors: ["'../constants' imported multiple times.", "'../constants' imported multiple times."], ...jsxConfig, }), + + test({ + code: ` + import {A1,} from 'foo'; + import {B1,} from 'foo'; + import {C1,} from 'foo'; + + import { + A2, + } from 'bar'; + import { + B2, + } from 'bar'; + import { + C2, + } from 'bar'; + + `, + output: ` + import {A1,B1,C1} from 'foo'; + ${''} + import { + A2, + ${''} + B2, + C2} from 'bar'; + ${''} + `, + errors: [ + { + message: "'foo' imported multiple times.", + line: 2, + column: 27, + }, + { + message: "'foo' imported multiple times.", + line: 3, + column: 27, + }, + { + message: "'foo' imported multiple times.", + line: 4, + column: 27, + }, + { + message: "'bar' imported multiple times.", + line: 8, + column: 16, + }, + { + message: "'bar' imported multiple times.", + line: 11, + column: 16, + }, + { + message: "'bar' imported multiple times.", + line: 14, + column: 16, + }, + ], + ...jsxConfig, + }), ], }); diff --git a/tests/src/rules/no-extraneous-dependencies.js b/tests/src/rules/no-extraneous-dependencies.js index 21561615c6..cb0398ada2 100644 --- a/tests/src/rules/no-extraneous-dependencies.js +++ b/tests/src/rules/no-extraneous-dependencies.js @@ -427,6 +427,18 @@ describe('TypeScript', () => { options: [{ packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }], ...parserConfig, }), + + test({ + code: 'import type { T } from "a"; export type { T };', + options: [{ packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }], + ...parserConfig, + }), + + test({ + code: 'export type { T } from "a";', + options: [{ packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }], + ...parserConfig, + }), ], invalid: [ test({ diff --git a/tests/src/rules/no-unused-modules.js b/tests/src/rules/no-unused-modules.js index 77fb608ccb..b09d5d759c 100644 --- a/tests/src/rules/no-unused-modules.js +++ b/tests/src/rules/no-unused-modules.js @@ -160,6 +160,15 @@ ruleTester.run('no-unused-modules', rule, { filename: testFilePath('./no-unused-modules/file-o.js'), parser: parsers.BABEL_OLD, }), + test({ + options: unusedExportsOptions, + code: ` + export const [o0, o2] = createLoadingAndErrorSelectors( + AUTH_USER + ); + `, + filename: testFilePath('./no-unused-modules/file-o.js'), + }), ], invalid: [ test({ @@ -723,7 +732,7 @@ describe('renameDefault', () => { describe('test behavior for new file', () => { before(() => { - fs.writeFileSync(testFilePath('./no-unused-modules/file-added-0.js'), '', { encoding: 'utf8' }); + fs.writeFileSync(testFilePath('./no-unused-modules/file-added-0.js'), '', { encoding: 'utf8', flag: 'w' }); }); // add import in newly created file @@ -831,7 +840,7 @@ describe('test behavior for new file', () => { describe('test behavior for new file', () => { before(() => { - fs.writeFileSync(testFilePath('./no-unused-modules/file-added-1.js'), '', { encoding: 'utf8' }); + fs.writeFileSync(testFilePath('./no-unused-modules/file-added-1.js'), '', { encoding: 'utf8', flag: 'w' }); }); ruleTester.run('no-unused-modules', rule, { valid: [ @@ -866,7 +875,7 @@ describe('test behavior for new file', () => { describe('test behavior for new file', () => { before(() => { - fs.writeFileSync(testFilePath('./no-unused-modules/file-added-2.js'), '', { encoding: 'utf8' }); + fs.writeFileSync(testFilePath('./no-unused-modules/file-added-2.js'), '', { encoding: 'utf8', flag: 'w' }); }); ruleTester.run('no-unused-modules', rule, { valid: [ @@ -892,7 +901,7 @@ describe('test behavior for new file', () => { describe('test behavior for new file', () => { before(() => { - fs.writeFileSync(testFilePath('./no-unused-modules/file-added-3.js'), '', { encoding: 'utf8' }); + fs.writeFileSync(testFilePath('./no-unused-modules/file-added-3.js'), '', { encoding: 'utf8', flag: 'w' }); }); ruleTester.run('no-unused-modules', rule, { valid: [ @@ -943,7 +952,7 @@ describe('test behavior for destructured exports', () => { describe('test behavior for new file', () => { before(() => { - fs.writeFileSync(testFilePath('./no-unused-modules/file-added-4.js.js'), '', { encoding: 'utf8' }); + fs.writeFileSync(testFilePath('./no-unused-modules/file-added-4.js.js'), '', { encoding: 'utf8', flag: 'w' }); }); ruleTester.run('no-unused-modules', rule, { valid: [