Skip to content

Commit

Permalink
Assume we always camelize store names ; few refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
david-szabo97 committed Feb 12, 2021
1 parent ced7cbd commit 5e02dff
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ const valid = [
`import { controls as controlsAlias } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; controlsAlias.resolveSelect( store );`,
];

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

const invalid = [
// Callback functions
`import { createRegistrySelector } from '@wordpress/data'; createRegistrySelector(( select ) => { select( 'core' ); });`,
Expand All @@ -59,83 +74,48 @@ const invalid = [
`import { controls as controlsAlias } from '@wordpress/data'; controlsAlias.resolveSelect( 'core' );`,

// Direct function calls suggestions
{
// Replace core with coreStore and import coreStore
code: `import { select } from '@wordpress/data'; select( 'core' );`,
errors: [
{
suggestions: [
{
desc:
'Replace literal with store definition. Import store if neccessary.',
output: `import { select } from '@wordpress/data';\nimport { 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.
code: `import { select } from '@wordpress/data'; import { something } from '@wordpress/core-data'; select( 'core' );`,
errors: [
{
suggestions: [
{
desc:
'Replace literal with store definition. Import store if neccessary.',
output: `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.
code: `import { select } from '@wordpress/data'; import { something, } from '@wordpress/core-data'; select( 'core' );`,
errors: [
{
suggestions: [
{
desc:
'Replace literal with store definition. Import store if neccessary.',
output: `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.
code: `import { select } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; select( 'core' );`,
errors: [
{
suggestions: [
{
desc:
'Replace literal with store definition. Import store if neccessary.',
output: `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.
code: `import { a } from './a'; import { select } from '@wordpress/data'; import { b } from './b'; select( 'core' );`,
errors: [
{
suggestions: [
{
desc:
'Replace literal with store definition. Import store if neccessary.',
output: `import { a } from './a'; import { select } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data'; import { b } from './b'; select( coreStore );`,
},
],
},
],
},
// Replace core with coreStore and import coreStore
createSuggestionTestCase(
`import { select } from '@wordpress/data'; select( 'core' );`,
`import { select } from '@wordpress/data';\nimport { 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.
createSuggestionTestCase(
`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(
`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(
`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(
`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(
`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(
`import { select } from '@wordpress/data'; select( 'core/notices' );`,
`import { select } from '@wordpress/data';\nimport { store as noticesStore } from '@wordpress/notices'; select( noticesStore );`
),
// Replace edit-post with editPostStore
createSuggestionTestCase(
`import { select } from '@wordpress/data'; select( 'core/edit-post' );`,
`import { select } from '@wordpress/data';\nimport { store as editPostStore } from '@wordpress/edit-post'; select( editPostStore );`
),
];
const errors = [
{
Expand Down
99 changes: 42 additions & 57 deletions packages/eslint-plugin/rules/data-no-store-string-literals.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
/**
* Converts store name to variable name.
* Removes dashes and uppercases the characters after dashes and appends `Store` at the end.
*
* @param {string} storeName
* @return {string} store name as variable name
*/
function storeNameToVariableNames( storeName ) {
return (
storeName
.split( '-' )
.map( ( s, index ) =>
index === 0
? s.toLowerCase()
: s[ 0 ].toUpperCase() + s.slice( 1 ).toLowerCase()
)
.join( '' ) + 'Store'
);
}

/**
* Returns last element of an array.
*
* @param {Array} array
* @return {*} last element of the array
*/
function arrayLast( array ) {
return array[ array.length - 1 ];
}
Expand Down Expand Up @@ -106,61 +132,19 @@ function getFixes( fixer, context, callNode ) {
import: '@wordpress/core-data',
variable: 'coreStore',
},
'core/block-editor': {
import: '@wordpress/block-editor',
variable: 'blockEditorStore',
},
'core/block-directory': {
import: '@wordpress/block-directory',
variable: 'blockDirectoryStore',
},
'core/blocks': {
import: '@wordpress/blocks',
variable: 'blocksStore',
},
'core/editor': {
import: '@wordpress/editor',
variable: 'editorStore',
},
'core/notices': {
import: '@wordpress/notices',
variable: 'noticesStore',
},
'core/reusable-blocks': {
import: '@wordpress/reusable-blocks',
variable: 'reusableBlocksStore',
},
'core/keyboard-shortcuts': {
import: '@wordpress/keyboard-shortcuts',
variable: 'keyboardShortcutsStore',
},
'core/edit-post': {
import: '@wordpress/edit-post',
variable: 'editPostStore',
},
'core/edit-site': {
import: '@wordpress/edit-site',
variable: 'editSiteStore',
},
'core/interface': {
import: '@wordpress/interface',
variable: 'interfaceStore',
},
'core/viewport': {
import: '@wordpress/viewport',
variable: 'viewportStore',
},
'core/rich-text': {
import: '@wordpress/rich-text',
variable: 'richTextStore',
},
};
if ( ! storeDefinitions[ storeName ] ) {
let storeDefinition = storeDefinitions[ storeName ];
if ( ! storeDefinition && storeName.startsWith( 'core/' ) ) {
const storeNameWithoutCore = storeName.substring( 5 );
storeDefinition = {
import: `@wordpress/${ storeNameWithoutCore }`,
variable: storeNameToVariableNames( storeNameWithoutCore ),
};
}
if ( ! storeDefinition ) {
return null;
}
const { variable: variableName, import: importName } = storeDefinitions[
storeName
];
const { variable: variableName, import: importName } = storeDefinition;

const fixes = [
fixer.replaceText( callNode.arguments[ 0 ], variableName ),
Expand All @@ -172,13 +156,14 @@ function getFixes( fixer, context, callNode ) {
const packageImports = imports.filter(
( n ) => n.source.value === importName
);
const i = packageImports.length > 0 ? packageImports[ 0 ] : null;
if ( i ) {
const alreadyHasStore = i.specifiers.some(
( s ) => s.imported.name === 'store'
const packageImport =
packageImports.length > 0 ? packageImports[ 0 ] : null;
if ( packageImport ) {
const alreadyHasStore = packageImport.specifiers.some(
( specifier ) => specifier.imported.name === 'store'
);
if ( ! alreadyHasStore ) {
const lastSpecifier = arrayLast( i.specifiers );
const lastSpecifier = arrayLast( packageImport.specifiers );
fixes.push(
fixer.insertTextAfter(
lastSpecifier,
Expand Down

0 comments on commit 5e02dff

Please sign in to comment.