Skip to content

Commit

Permalink
ESLint: fix eslint-plugin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shvlv committed Oct 15, 2024
1 parent 907f402 commit c6b0a25
Show file tree
Hide file tree
Showing 24 changed files with 180 additions and 74 deletions.
43 changes: 35 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
"deep-freeze": "0.0.1",
"equivalent-key-map": "0.2.2",
"escape-html": "1.0.3",
"eslint": "9.11.0",
"eslint": "9.11.1",
"eslint-config-prettier": "9.1.0",
"eslint-import-resolver-node": "0.3.9",
"eslint-plugin-import": "2.31.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RuleTester } from 'eslint';
import rule from '../data-no-store-string-literals';

const ruleTester = new RuleTester( {
parserOptions: {
languageOptions: {
sourceType: 'module',
ecmaVersion: 6,
},
Expand Down Expand Up @@ -38,77 +38,134 @@ const valid = [
`import { controls as controlsAlias } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; controlsAlias.resolveSelect( coreStore );`,
];

const createSuggestionTestCase = ( code, output ) => ( {
code,
errors: [
{
suggestions: [
{
desc: 'Replace literal with store definition. Import store if necessary.',
output,
},
],
},
],
} );
const createTestCase = ( code, output, message = '' ) => {
const base = {
code,
errors: [
{
suggestions: [
{
desc: 'Replace literal with store definition. Import store if necessary.',
output,
},
],
},
],
};

if ( message ) {
base.errors[ 0 ].message = message;
return base;
}

base.errors[ 0 ].messageId = 'doNotUseStringLiteral';

return base;
};

const createCoreDataCase = ( code, output ) =>
createTestCase(
code,
output,
`Do not use string literals ( 'core' ) for accessing @wordpress/data stores. Pass the store definition instead`
);

const invalid = [
// Callback functions.
`import { createRegistrySelector } from '@wordpress/data'; createRegistrySelector(( select ) => { select( 'core' ); });`,
`import { useSelect } from '@wordpress/data'; useSelect(( select ) => { select( 'core' ); });`,
`import { withSelect } from '@wordpress/data'; withSelect(( select ) => { select( 'core' ); });`,
`import { withDispatch } from '@wordpress/data'; withDispatch(( select ) => { select( 'core' ); });`,
`import { withDispatch as withDispatchAlias } from '@wordpress/data'; withDispatchAlias(( select ) => { select( 'core' ); });`,
createCoreDataCase(
`import { createRegistrySelector } from '@wordpress/data'; createRegistrySelector(( select ) => { select( 'core' ); });`,
`import { createRegistrySelector } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; createRegistrySelector(( select ) => { select( coreStore ); });`
),
createCoreDataCase(
`import { useSelect } from '@wordpress/data'; useSelect(( select ) => { select( 'core' ); });`,
`import { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; useSelect(( select ) => { select( coreStore ); });`
),
createCoreDataCase(
`import { withSelect } from '@wordpress/data'; withSelect(( select ) => { select( 'core' ); });`,
`import { withSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; withSelect(( select ) => { select( coreStore ); });`
),
createCoreDataCase(
`import { withDispatch } from '@wordpress/data'; withDispatch(( select ) => { select( 'core' ); });`,
`import { withDispatch } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; withDispatch(( select ) => { select( coreStore ); });`
),
createCoreDataCase(
`import { withDispatch as withDispatchAlias } from '@wordpress/data'; withDispatchAlias(( select ) => { select( 'core' ); });`,
`import { withDispatch as withDispatchAlias } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; withDispatchAlias(( select ) => { select( coreStore ); });`
),

// Direct function calls.
`import { useDispatch } from '@wordpress/data'; useDispatch( 'core' );`,
`import { dispatch } from '@wordpress/data'; dispatch( 'core' );`,
`import { useSelect } from '@wordpress/data'; useSelect( 'core' );`,
`import { select } from '@wordpress/data'; select( 'core' );`,
`import { resolveSelect } from '@wordpress/data'; resolveSelect( 'core' );`,
`import { resolveSelect as resolveSelectAlias } from '@wordpress/data'; resolveSelectAlias( 'core' );`,

// Object property function calls.
`import { controls } from '@wordpress/data'; controls.select( 'core' );`,
`import { controls } from '@wordpress/data'; controls.dispatch( 'core' );`,
`import { controls } from '@wordpress/data'; controls.resolveSelect( 'core' );`,
`import { controls as controlsAlias } from '@wordpress/data'; controlsAlias.resolveSelect( 'core' );`,

// Direct function calls suggestions
// Replace core with coreStore and import coreStore.
createSuggestionTestCase(
createCoreDataCase(
`import { useDispatch } from '@wordpress/data'; useDispatch( 'core' );`,
`import { useDispatch } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; useDispatch( coreStore );`
),
createCoreDataCase(
`import { dispatch } from '@wordpress/data'; dispatch( 'core' );`,
`import { dispatch } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; dispatch( coreStore );`
),
createCoreDataCase(
`import { useSelect } from '@wordpress/data'; useSelect( 'core' );`,
`import { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; useSelect( coreStore );`
),
createCoreDataCase(
`import { select } from '@wordpress/data'; select( 'core' );`,
`import { select } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; select( coreStore );`
),
createCoreDataCase(
`import { resolveSelect } from '@wordpress/data'; resolveSelect( 'core' );`,
`import { resolveSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; resolveSelect( coreStore );`
),
createCoreDataCase(
`import { resolveSelect as resolveSelectAlias } from '@wordpress/data'; resolveSelectAlias( 'core' );`,
`import { resolveSelect as resolveSelectAlias } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; resolveSelectAlias( coreStore );`
),

// Object property function calls.
createCoreDataCase(
`import { controls } from '@wordpress/data'; controls.select( 'core' );`,
`import { controls } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; controls.select( coreStore );`
),
createCoreDataCase(
`import { controls } from '@wordpress/data'; controls.dispatch( 'core' );`,
`import { controls } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; controls.dispatch( coreStore );`
),
createCoreDataCase(
`import { controls } from '@wordpress/data'; controls.resolveSelect( 'core' );`,
`import { controls } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; controls.resolveSelect( coreStore );`
),
createCoreDataCase(
`import { controls as controlsAlias } from '@wordpress/data'; controlsAlias.resolveSelect( 'core' );`,
`import { controls as controlsAlias } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; controlsAlias.resolveSelect( coreStore );`
),

// Replace core with coreStore. A @wordpress/core-data already exists, so it should append the import to that one.
createSuggestionTestCase(
createTestCase(
`import { select } from '@wordpress/data'; import { something } from '@wordpress/core-data'; select( 'core' );`,
`import { select } from '@wordpress/data'; import { something,store as coreStore } from '@wordpress/core-data'; select( coreStore );`
),
// Replace core with coreStore. A @wordpress/core-data already exists, so it should append the import to that one.
// This time there is a comma after the import.
createSuggestionTestCase(
createTestCase(
`import { select } from '@wordpress/data'; import { something, } from '@wordpress/core-data'; select( 'core' );`,
`import { select } from '@wordpress/data'; import { something,store as coreStore, } from '@wordpress/core-data'; select( coreStore );`
),
// Replace core with coreStore. Store import already exists. It shouldn't modify the import, just replace the literal with the store definition.
createSuggestionTestCase(
createTestCase(
`import { select } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; select( 'core' );`,
`import { select } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; select( coreStore );`
),
// Replace core with coreStore. There are internal and WordPress dependencies.
// It should append the import after the last WordPress dependency import.
createSuggestionTestCase(
createTestCase(
`import { a } from './a'; import { select } from '@wordpress/data'; import { b } from './b'; select( 'core' );`,
`import { a } from './a'; import { select } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; import { b } from './b'; select( coreStore );`
),
// Replace block-editor with blockEditorStore.
createSuggestionTestCase(
createTestCase(
`import { select } from '@wordpress/data'; select( 'core/block-editor' );`,
`import { select } from '@wordpress/data';\nimport { store as blockEditorStore } from '@wordpress/block-editor'; select( blockEditorStore );`
),
// Replace notices with noticesStore.
createSuggestionTestCase(
createTestCase(
`import { select } from '@wordpress/data'; select( 'core/notices' );`,
`import { select } from '@wordpress/data';\nimport { store as noticesStore } from '@wordpress/notices'; select( noticesStore );`
),
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/rules/__tests__/dependency-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RuleTester } from 'eslint';
import rule from '../dependency-group';

const ruleTester = new RuleTester( {
parserOptions: {
languageOptions: {
sourceType: 'module',
ecmaVersion: 6,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/rules/__tests__/i18n-ellipsis.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RuleTester } from 'eslint';
import rule from '../i18n-ellipsis';

const ruleTester = new RuleTester( {
parserOptions: {
languageOptions: {
ecmaVersion: 6,
},
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RuleTester } from 'eslint';
import rule from '../i18n-hyphenated-range';

const ruleTester = new RuleTester( {
parserOptions: {
languageOptions: {
ecmaVersion: 6,
},
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RuleTester } from 'eslint';
import rule from '../i18n-no-collapsible-whitespace';

const ruleTester = new RuleTester( {
parserOptions: {
languageOptions: {
ecmaVersion: 6,
},
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RuleTester } from 'eslint';
import rule from '../i18n-no-flanking-whitespace';

const ruleTester = new RuleTester( {
parserOptions: {
languageOptions: {
ecmaVersion: 6,
},
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RuleTester } from 'eslint';
import rule from '../i18n-no-placeholders-only';

const ruleTester = new RuleTester( {
parserOptions: {
languageOptions: {
ecmaVersion: 6,
},
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RuleTester } from 'eslint';
import rule from '../i18n-no-variables';

const ruleTester = new RuleTester( {
parserOptions: {
languageOptions: {
ecmaVersion: 6,
},
} );
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/rules/__tests__/i18n-text-domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RuleTester } from 'eslint';
import rule from '../i18n-text-domain';

const ruleTester = new RuleTester( {
parserOptions: {
languageOptions: {
ecmaVersion: 6,
},
} );
Expand Down Expand Up @@ -168,7 +168,7 @@ ruleTester.run( 'i18n-text-domain', rule, {
},
{
code: `__( 'Hello World' )`,
output: `__( 'Hello World' )`,
output: null,
options: [ { allowedTextDomain: [ 'foo', 'bar' ] } ],
errors: [ { messageId: 'missing' } ],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RuleTester } from 'eslint';
import rule from '../i18n-translator-comments';

const ruleTester = new RuleTester( {
parserOptions: {
languageOptions: {
ecmaVersion: 6,
},
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { RuleTester } from 'eslint';
import rule from '../no-base-control-with-label-without-id';

const ruleTester = new RuleTester( {
parserOptions: {
languageOptions: {
ecmaVersion: 6,
ecmaFeatures: {
jsx: true,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RuleTester } from 'eslint';
import rule from '../no-global-active-element';

const ruleTester = new RuleTester( {
parserOptions: {
languageOptions: {
ecmaVersion: 6,
},
} );
Expand Down
Loading

0 comments on commit c6b0a25

Please sign in to comment.