diff --git a/tests/src/rules/first.js b/tests/src/rules/first.js index 3f7301319..9d2787067 100644 --- a/tests/src/rules/first.js +++ b/tests/src/rules/first.js @@ -7,60 +7,71 @@ const rule = require('rules/first'); ruleTester.run('first', rule, { valid: [ - test({ code: "import { x } from './foo'; import { y } from './bar';\ - export { x, y }" }), + test({ + code: "import { x } from './foo'; import { y } from './bar';\ + export { x, y }", + }), test({ code: "import { x } from 'foo'; import { y } from './bar'" }), test({ code: "import { x } from './foo'; import { y } from 'bar'" }), - test({ code: "import { x } from './foo'; import { y } from 'bar'", + test({ + code: "import { x } from './foo'; import { y } from 'bar'", options: ['disable-absolute-first'], }), - test({ code: "'use directive';\ - import { x } from 'foo';" }), + test({ + code: "'use directive';\ + import { x } from 'foo';", + }), ], invalid: [ - test({ code: "import { x } from './foo';\ - export { x };\ - import { y } from './bar';", - errors: 1, - output: "import { x } from './foo';\ - import { y } from './bar';\ - export { x };", + test({ + code: "import { x } from './foo';\ + export { x };\ + import { y } from './bar';", + errors: 1, + output: "import { x } from './foo';\ + import { y } from './bar';\ + export { x };", }), - test({ code: "import { x } from './foo';\ - export { x };\ - import { y } from './bar';\ - import { z } from './baz';", - errors: 2, - output: "import { x } from './foo';\ - import { y } from './bar';\ - import { z } from './baz';\ - export { x };", + test({ + code: "import { x } from './foo';\ + export { x };\ + import { y } from './bar';\ + import { z } from './baz';", + errors: 2, + output: "import { x } from './foo';\ + import { y } from './bar';\ + import { z } from './baz';\ + export { x };", }), - test({ code: "import { x } from './foo'; import { y } from 'bar'", + test({ + code: "import { x } from './foo'; import { y } from 'bar'", options: ['absolute-first'], errors: 1, }), - test({ code: "import { x } from 'foo';\ - 'use directive';\ - import { y } from 'bar';", - errors: 1, - output: "import { x } from 'foo';\ - import { y } from 'bar';\ - 'use directive';", + test({ + code: "import { x } from 'foo';\ + 'use directive';\ + import { y } from 'bar';", + errors: 1, + output: "import { x } from 'foo';\ + import { y } from 'bar';\ + 'use directive';", }), - test({ code: "var a = 1;\ - import { y } from './bar';\ - if (true) { x() };\ - import { x } from './foo';\ - import { z } from './baz';", - errors: 3, - output: "import { y } from './bar';\ - var a = 1;\ - if (true) { x() };\ - import { x } from './foo';\ - import { z } from './baz';", + test({ + code: "var a = 1;\ + import { y } from './bar';\ + if (true) { x() };\ + import { x } from './foo';\ + import { z } from './baz';", + errors: 3, + output: "import { y } from './bar';\ + var a = 1;\ + if (true) { x() };\ + import { x } from './foo';\ + import { z } from './baz';", }), - test({ code: "if (true) { console.log(1) }import a from 'b'", + test({ + code: "if (true) { console.log(1) }import a from 'b'", errors: 1, output: "import a from 'b'\nif (true) { console.log(1) }", }), @@ -72,7 +83,7 @@ context('TypeScript', function () { .filter((parser) => parser !== require.resolve('typescript-eslint-parser')) .forEach((parser) => { const parserConfig = { - parser: parser, + parser, settings: { 'import/parsers': { [parser]: ['.ts'] }, 'import/resolver': { 'eslint-import-resolver-typescript': true }, @@ -81,35 +92,29 @@ context('TypeScript', function () { ruleTester.run('order', rule, { valid: [ - test( - { - code: ` - import y = require('bar'); - import { x } from 'foo'; - import z = require('baz'); - `, - parser, - }, - parserConfig, - ), + test({ + code: ` + import y = require('bar'); + import { x } from 'foo'; + import z = require('baz'); + `, + ...parserConfig, + }), ], invalid: [ - test( - { - code: ` - import { x } from './foo'; - import y = require('bar'); - `, - options: ['absolute-first'], - parser, - errors: [ - { - message: 'Absolute imports should come before relative imports.', - }, - ], - }, - parserConfig, - ), + test({ + code: ` + import { x } from './foo'; + import y = require('bar'); + `, + options: ['absolute-first'], + ...parserConfig, + errors: [ + { + message: 'Absolute imports should come before relative imports.', + }, + ], + }), ], }); }); diff --git a/tests/src/rules/no-duplicates.js b/tests/src/rules/no-duplicates.js index d16f37b10..d1b9c7456 100644 --- a/tests/src/rules/no-duplicates.js +++ b/tests/src/rules/no-duplicates.js @@ -420,7 +420,7 @@ context('TypeScript', function() { .filter((parser) => parser !== require.resolve('typescript-eslint-parser')) .forEach((parser) => { const parserConfig = { - parser: parser, + parser, settings: { 'import/parsers': { [parser]: ['.ts'] }, 'import/resolver': { 'eslint-import-resolver-typescript': true }, @@ -430,13 +430,10 @@ context('TypeScript', function() { ruleTester.run('no-duplicates', rule, { valid: [ // #1667: ignore duplicate if is a typescript type import - test( - { - code: "import type { x } from './foo'; import y from './foo'", - parser, - }, - parserConfig, - ), + test({ + code: "import type { x } from './foo'; import y from './foo'", + ...parserConfig, + }), ], invalid: [], }); diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index e7e6b0566..61bf1bb70 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -2286,7 +2286,7 @@ context('TypeScript', function () { .filter((parser) => parser !== require.resolve('typescript-eslint-parser')) .forEach((parser) => { const parserConfig = { - parser: parser, + parser, settings: { 'import/parsers': { [parser]: ['.ts'] }, 'import/resolver': { 'eslint-import-resolver-typescript': true }, @@ -2298,352 +2298,316 @@ context('TypeScript', function () { // #1667: typescript type import support // Option alphabetize: {order: 'asc'} - test( - { - code: ` - import c from 'Bar'; - import type { C } from 'Bar'; - import b from 'bar'; - import a from 'foo'; - import type { A } from 'foo'; - - import index from './'; - `, - parser, - options: [ - { - groups: ['external', 'index'], - alphabetize: { order: 'asc' }, - }, - ], - }, - parserConfig, - ), + test({ + code: ` + import c from 'Bar'; + import type { C } from 'Bar'; + import b from 'bar'; + import a from 'foo'; + import type { A } from 'foo'; + + import index from './'; + `, + ...parserConfig, + options: [ + { + groups: ['external', 'index'], + alphabetize: { order: 'asc' }, + }, + ], + }), // Option alphabetize: {order: 'desc'} - test( - { - code: ` - import a from 'foo'; - import type { A } from 'foo'; - import b from 'bar'; - import c from 'Bar'; - import type { C } from 'Bar'; - - import index from './'; - `, - parser, - options: [ - { - groups: ['external', 'index'], - alphabetize: { order: 'desc' }, - }, - ], - }, - parserConfig, - ), + test({ + code: ` + import a from 'foo'; + import type { A } from 'foo'; + import b from 'bar'; + import c from 'Bar'; + import type { C } from 'Bar'; + + import index from './'; + `, + ...parserConfig, + options: [ + { + groups: ['external', 'index'], + alphabetize: { order: 'desc' }, + }, + ], + }), // Option alphabetize: {order: 'asc'} with type group - test( - { - code: ` - import c from 'Bar'; - import b from 'bar'; - import a from 'foo'; - - import index from './'; - - import type { C } from 'Bar'; - import type { A } from 'foo'; - `, - parser, - options: [ - { - groups: ['external', 'index', 'type'], - alphabetize: { order: 'asc' }, - }, - ], - }, - parserConfig, - ), + test({ + code: ` + import c from 'Bar'; + import b from 'bar'; + import a from 'foo'; + + import index from './'; + + import type { C } from 'Bar'; + import type { A } from 'foo'; + `, + ...parserConfig, + options: [ + { + groups: ['external', 'index', 'type'], + alphabetize: { order: 'asc' }, + }, + ], + }), // Option alphabetize: {order: 'asc'} with type group & path group - test( - { - // only: true, - code: ` - import c from 'Bar'; - import a from 'foo'; - - import b from 'dirA/bar'; - - import index from './'; - - import type { C } from 'dirA/Bar'; - import type { A } from 'foo'; - `, - parser, - options: [ - { - alphabetize: { order: 'asc' }, - groups: ['external', 'internal', 'index', 'type'], - pathGroups: [ - { - pattern: 'dirA/**', - group: 'internal', - }, - ], - 'newlines-between': 'always', - pathGroupsExcludedImportTypes: ['type'], - }, - ], - }, - parserConfig, - ), + test({ + // only: true, + code: ` + import c from 'Bar'; + import a from 'foo'; + + import b from 'dirA/bar'; + + import index from './'; + + import type { C } from 'dirA/Bar'; + import type { A } from 'foo'; + `, + ...parserConfig, + options: [ + { + alphabetize: { order: 'asc' }, + groups: ['external', 'internal', 'index', 'type'], + pathGroups: [ + { + pattern: 'dirA/**', + group: 'internal', + }, + ], + 'newlines-between': 'always', + pathGroupsExcludedImportTypes: ['type'], + }, + ], + }), // Option alphabetize: {order: 'asc'} with path group - test( - { - // only: true, - code: ` - import c from 'Bar'; - import type { A } from 'foo'; - import a from 'foo'; - - import type { C } from 'dirA/Bar'; - import b from 'dirA/bar'; - - import index from './'; - `, - parser, - options: [ - { - alphabetize: { order: 'asc' }, - groups: ['external', 'internal', 'index'], - pathGroups: [ - { - pattern: 'dirA/**', - group: 'internal', - }, - ], - 'newlines-between': 'always', - pathGroupsExcludedImportTypes: [], - }, - ], - }, - parserConfig, - ), + test({ + // only: true, + code: ` + import c from 'Bar'; + import type { A } from 'foo'; + import a from 'foo'; + + import type { C } from 'dirA/Bar'; + import b from 'dirA/bar'; + + import index from './'; + `, + ...parserConfig, + options: [ + { + alphabetize: { order: 'asc' }, + groups: ['external', 'internal', 'index'], + pathGroups: [ + { + pattern: 'dirA/**', + group: 'internal', + }, + ], + 'newlines-between': 'always', + pathGroupsExcludedImportTypes: [], + }, + ], + }), // Option alphabetize: {order: 'desc'} with type group - test( - { - code: ` - import a from 'foo'; - import b from 'bar'; - import c from 'Bar'; - - import index from './'; - - import type { A } from 'foo'; - import type { C } from 'Bar'; - `, - parser, - options: [ - { - groups: ['external', 'index', 'type'], - alphabetize: { order: 'desc' }, - }, - ], - }, - parserConfig, - ), - test( - { - code: ` - import { Partner } from '@models/partner/partner'; - import { PartnerId } from '@models/partner/partner-id'; - `, - parser, - options: [ - { - alphabetize: { order: 'asc' }, - }, - ], - }, - parserConfig, - ), - test( - { - code: ` - import { serialize, parse, mapFieldErrors } from '@vtaits/form-schema'; - import type { GetFieldSchema } from '@vtaits/form-schema'; - import { useMemo, useCallback } from 'react'; - import type { ReactElement, ReactNode } from 'react'; - import { Form } from 'react-final-form'; - import type { FormProps as FinalFormProps } from 'react-final-form'; - `, - parser, - options: [ - { - alphabetize: { order: 'asc' }, - }, - ], - }, - parserConfig, - ), + test({ + code: ` + import a from 'foo'; + import b from 'bar'; + import c from 'Bar'; + + import index from './'; + + import type { A } from 'foo'; + import type { C } from 'Bar'; + `, + ...parserConfig, + options: [ + { + groups: ['external', 'index', 'type'], + alphabetize: { order: 'desc' }, + }, + ], + }), + test({ + code: ` + import { Partner } from '@models/partner/partner'; + import { PartnerId } from '@models/partner/partner-id'; + `, + ...parserConfig, + options: [ + { + alphabetize: { order: 'asc' }, + }, + ], + }), + test({ + code: ` + import { serialize, parse, mapFieldErrors } from '@vtaits/form-schema'; + import type { GetFieldSchema } from '@vtaits/form-schema'; + import { useMemo, useCallback } from 'react'; + import type { ReactElement, ReactNode } from 'react'; + import { Form } from 'react-final-form'; + import type { FormProps as FinalFormProps } from 'react-final-form'; + `, + ...parserConfig, + options: [ + { + alphabetize: { order: 'asc' }, + }, + ], + }), ], invalid: [ // Option alphabetize: {order: 'asc'} - test( - { - code: ` - import b from 'bar'; - import c from 'Bar'; - import type { C } from 'Bar'; - import a from 'foo'; - import type { A } from 'foo'; - - import index from './'; - `, - output: ` - import c from 'Bar'; - import type { C } from 'Bar'; - import b from 'bar'; - import a from 'foo'; - import type { A } from 'foo'; - - import index from './'; - `, - parser, - options: [ - { - groups: ['external', 'index'], - alphabetize: { order: 'asc' }, - }, - ], - errors: [ - { - message: semver.satisfies(eslintPkg.version, '< 3') - ? '`bar` import should occur after import of `Bar`' - : /(`bar` import should occur after import of `Bar`)|(`Bar` import should occur before import of `bar`)/, - }, - ], - }, - parserConfig, - ), + test({ + code: ` + import b from 'bar'; + import c from 'Bar'; + import type { C } from 'Bar'; + import a from 'foo'; + import type { A } from 'foo'; + + import index from './'; + `, + output: ` + import c from 'Bar'; + import type { C } from 'Bar'; + import b from 'bar'; + import a from 'foo'; + import type { A } from 'foo'; + + import index from './'; + `, + ...parserConfig, + options: [ + { + groups: ['external', 'index'], + alphabetize: { order: 'asc' }, + }, + ], + errors: [ + { + message: semver.satisfies(eslintPkg.version, '< 3') + ? '`bar` import should occur after import of `Bar`' + : /(`bar` import should occur after import of `Bar`)|(`Bar` import should occur before import of `bar`)/, + }, + ], + }), // Option alphabetize: {order: 'desc'} - test( - { - code: ` - import a from 'foo'; - import type { A } from 'foo'; - import c from 'Bar'; - import type { C } from 'Bar'; - import b from 'bar'; - - import index from './'; - `, - output: ` - import a from 'foo'; - import type { A } from 'foo'; - import b from 'bar'; - import c from 'Bar'; - import type { C } from 'Bar'; - - import index from './'; - `, - parser, - options: [ - { - groups: ['external', 'index'], - alphabetize: { order: 'desc' }, - }, - ], - errors: [ - { - message: semver.satisfies(eslintPkg.version, '< 3') - ? '`bar` import should occur before import of `Bar`' - : /(`bar` import should occur before import of `Bar`)|(`Bar` import should occur after import of `bar`)/, - }, - ], - }, - parserConfig, - ), + test({ + code: ` + import a from 'foo'; + import type { A } from 'foo'; + import c from 'Bar'; + import type { C } from 'Bar'; + import b from 'bar'; + + import index from './'; + `, + output: ` + import a from 'foo'; + import type { A } from 'foo'; + import b from 'bar'; + import c from 'Bar'; + import type { C } from 'Bar'; + + import index from './'; + `, + ...parserConfig, + options: [ + { + groups: ['external', 'index'], + alphabetize: { order: 'desc' }, + }, + ], + errors: [ + { + message: semver.satisfies(eslintPkg.version, '< 3') + ? '`bar` import should occur before import of `Bar`' + : /(`bar` import should occur before import of `Bar`)|(`Bar` import should occur after import of `bar`)/, + }, + ], + }), // Option alphabetize: {order: 'asc'} with type group - test( - { - code: ` - import b from 'bar'; - import c from 'Bar'; - import a from 'foo'; - - import index from './'; - - import type { A } from 'foo'; - import type { C } from 'Bar'; - `, - output: ` - import c from 'Bar'; - import b from 'bar'; - import a from 'foo'; - - import index from './'; - - import type { C } from 'Bar'; - import type { A } from 'foo'; - `, - parser, - options: [ - { - groups: ['external', 'index', 'type'], - alphabetize: { order: 'asc' }, - }, - ], - errors: semver.satisfies(eslintPkg.version, '< 3') ? [ - { message: '`Bar` import should occur before import of `bar`' }, - { message: '`Bar` import should occur before import of `foo`' }, - ] : [ - { message: /(`Bar` import should occur before import of `bar`)|(`bar` import should occur after import of `Bar`)/ }, - { message: /(`Bar` import should occur before import of `foo`)|(`foo` import should occur after import of `Bar`)/ }, - ], - }, - parserConfig, - ), + test({ + code: ` + import b from 'bar'; + import c from 'Bar'; + import a from 'foo'; + + import index from './'; + + import type { A } from 'foo'; + import type { C } from 'Bar'; + `, + output: ` + import c from 'Bar'; + import b from 'bar'; + import a from 'foo'; + + import index from './'; + + import type { C } from 'Bar'; + import type { A } from 'foo'; + `, + ...parserConfig, + options: [ + { + groups: ['external', 'index', 'type'], + alphabetize: { order: 'asc' }, + }, + ], + errors: semver.satisfies(eslintPkg.version, '< 3') ? [ + { message: '`Bar` import should occur before import of `bar`' }, + { message: '`Bar` import should occur before import of `foo`' }, + ] : [ + { message: /(`Bar` import should occur before import of `bar`)|(`bar` import should occur after import of `Bar`)/ }, + { message: /(`Bar` import should occur before import of `foo`)|(`foo` import should occur after import of `Bar`)/ }, + ], + }), // Option alphabetize: {order: 'desc'} with type group - test( - { - code: ` - import a from 'foo'; - import c from 'Bar'; - import b from 'bar'; - - import index from './'; - - import type { C } from 'Bar'; - import type { A } from 'foo'; - `, - output: ` - import a from 'foo'; - import b from 'bar'; - import c from 'Bar'; - - import index from './'; - - import type { A } from 'foo'; - import type { C } from 'Bar'; - `, - parser, - options: [ - { - groups: ['external', 'index', 'type'], - alphabetize: { order: 'desc' }, - }, - ], - errors: semver.satisfies(eslintPkg.version, '< 3') ? [ - { message: '`bar` import should occur before import of `Bar`' }, - { message: '`foo` import should occur before import of `Bar`' }, - ] : [ - { message: /(`bar` import should occur before import of `Bar`)|(`Bar` import should occur after import of `bar`)/ }, - { message: /(`foo` import should occur before import of `Bar`)|(`Bar` import should occur after import of `foo`)/ }, - ], - }, - parserConfig, - ), + test({ + code: ` + import a from 'foo'; + import c from 'Bar'; + import b from 'bar'; + + import index from './'; + + import type { C } from 'Bar'; + import type { A } from 'foo'; + `, + output: ` + import a from 'foo'; + import b from 'bar'; + import c from 'Bar'; + + import index from './'; + + import type { A } from 'foo'; + import type { C } from 'Bar'; + `, + ...parserConfig, + options: [ + { + groups: ['external', 'index', 'type'], + alphabetize: { order: 'desc' }, + }, + ], + errors: semver.satisfies(eslintPkg.version, '< 3') ? [ + { message: '`bar` import should occur before import of `Bar`' }, + { message: '`foo` import should occur before import of `Bar`' }, + ] : [ + { message: /(`bar` import should occur before import of `Bar`)|(`Bar` import should occur after import of `bar`)/ }, + { message: /(`foo` import should occur before import of `Bar`)|(`Bar` import should occur after import of `foo`)/ }, + ], + }), // warns for out of order unassigned imports (warnOnUnassignedImports enabled) test({ code: ` diff --git a/tests/src/rules/prefer-default-export.js b/tests/src/rules/prefer-default-export.js index 4efa47f5f..34323fe53 100644 --- a/tests/src/rules/prefer-default-export.js +++ b/tests/src/rules/prefer-default-export.js @@ -151,7 +151,7 @@ ruleTester.run('prefer-default-export', rule, { context('TypeScript', function() { getNonDefaultParsers().forEach((parser) => { const parserConfig = { - parser: parser, + parser, settings: { 'import/parsers': { [parser]: ['.ts'] }, 'import/resolver': { 'eslint-import-resolver-typescript': true }, @@ -161,52 +161,34 @@ context('TypeScript', function() { ruleTester.run('prefer-default-export', rule, { valid: [ // Exporting types - test( - { - code: ` - export type foo = string; - export type bar = number;`, - parser, - }, - parserConfig, - ), - test( - { - code: ` - export type foo = string; - export type bar = number;`, - parser, - }, - parserConfig, - ), - test( - { - code: 'export type foo = string', - parser, - }, - parserConfig, - ), - test( - { - code: 'export type foo = string', - parser, - }, - parserConfig, - ), - test( - { - code: 'export interface foo { bar: string; }', - parser, - }, - parserConfig, - ), - test( - { - code: 'export interface foo { bar: string; }; export function goo() {}', - parser, - }, - parserConfig, - ), + test({ + code: ` + export type foo = string; + export type bar = number;`, + ...parserConfig, + }), + test({ + code: ` + export type foo = string; + export type bar = number;`, + ...parserConfig, + }), + test({ + code: 'export type foo = string', + ...parserConfig, + }), + test({ + code: 'export type foo = string', + ...parserConfig, + }), + test({ + code: 'export interface foo { bar: string; }', + ...parserConfig, + }), + test({ + code: 'export interface foo { bar: string; }; export function goo() {}', + ...parserConfig, + }), ], invalid: [], }); diff --git a/tests/src/utils.js b/tests/src/utils.js index a76826de5..e805e9545 100644 --- a/tests/src/utils.js +++ b/tests/src/utils.js @@ -32,6 +32,9 @@ export function testVersion(specifier, t) { } export function test(t) { + if (arguments.length !== 1) { + throw new SyntaxError('`test` requires exactly one object argument'); + } return Object.assign({ filename: FILENAME, }, t, {