Skip to content

Commit

Permalink
Performance: Move unused variable initializations after early return (#…
Browse files Browse the repository at this point in the history
…12827)

* Performance: Move unused variable initializations after early return

* Small tweak about JS var scoping

* Fix block switcher
  • Loading branch information
aduth authored and youknowriad committed Dec 24, 2018
1 parent e9f6a6b commit 47e69f3
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 31 deletions.
3 changes: 1 addition & 2 deletions bin/packages/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ getPackages().forEach( ( p ) => {
try {
fs.accessSync( srcDir, fs.F_OK );
fs.watch( path.resolve( p, 'src' ), { recursive: true }, ( event, filename ) => {
const filePath = path.resolve( srcDir, filename );

if ( ! isSourceFile( filename ) ) {
return;
}

const filePath = path.resolve( srcDir, filename );
if ( ( event === 'change' || event === 'rename' ) && exists( filePath ) ) {
// eslint-disable-next-line no-console
console.log( chalk.green( '->' ), `${ event }: ${ filename }` );
Expand Down
20 changes: 10 additions & 10 deletions packages/block-library/src/cover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,6 @@ export const settings = {
backgroundColor: overlayColor.color,
};

const classes = classnames(
className,
contentAlign !== 'center' && `has-${ contentAlign }-content`,
dimRatioToClass( dimRatio ),
{
'has-background-dim': dimRatio !== 0,
'has-parallax': hasParallax,
}
);

const controls = (
<Fragment>
<BlockControls>
Expand Down Expand Up @@ -346,6 +336,16 @@ export const settings = {
);
}

const classes = classnames(
className,
contentAlign !== 'center' && `has-${ contentAlign }-content`,
dimRatioToClass( dimRatio ),
{
'has-background-dim': dimRatio !== 0,
'has-parallax': hasParallax,
}
);

return (
<Fragment>
{ controls }
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ class ImageEdit extends Component {
linkTarget,
} = attributes;
const isExternal = isExternalImage( id, url );
const imageSizeOptions = this.getImageSizeOptions();

let toolbarEditButton;
if ( url ) {
Expand Down Expand Up @@ -446,6 +445,7 @@ class ImageEdit extends Component {

const isResizable = [ 'wide', 'full' ].indexOf( align ) === -1 && isLargeViewport;
const isLinkURLInputReadOnly = linkDestination !== LINK_DESTINATION_CUSTOM;
const imageSizeOptions = this.getImageSizeOptions();

const getInspectorControls = ( imageWidth, imageHeight ) => (
<InspectorControls>
Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,9 @@ const isPossibleTransformForSource = ( transform, direction, blocks ) => {
if ( isEmpty( blocks ) ) {
return false;
}
const isMultiBlock = blocks.length > 1;
const sourceBlock = first( blocks );

// If multiple blocks are selected, only multi block transforms are allowed.
const isMultiBlock = blocks.length > 1;
const isValidForMultiBlocks = ! isMultiBlock || transform.isMultiBlock;
if ( ! isValidForMultiBlocks ) {
return false;
Expand All @@ -132,6 +131,7 @@ const isPossibleTransformForSource = ( transform, direction, blocks ) => {
}

// Check if the transform's block name matches the source block only if this is a transform 'from'.
const sourceBlock = first( blocks );
const hasMatchingName = direction !== 'from' || transform.blocks.indexOf( sourceBlock.name ) !== -1;
if ( ! hasMatchingName ) {
return false;
Expand Down
7 changes: 4 additions & 3 deletions packages/blocks/src/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,18 @@ export function getCommentDelimitedContent( rawBlockName, attributes, content )
*/
export function serializeBlock( block ) {
const blockName = block.name;
const blockType = getBlockType( blockName );
const saveContent = getBlockContent( block );
const saveAttributes = getCommentAttributes( blockType, block.attributes );

switch ( blockName ) {
case getFreeformContentHandlerName():
case getUnregisteredTypeHandlerName():
return saveContent;

default:
default: {
const blockType = getBlockType( blockName );
const saveAttributes = getCommentAttributes( blockType, block.attributes );
return getCommentDelimitedContent( blockName, saveAttributes, saveContent );
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/dom/src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ export function placeCaretAtVerticalEdge( container, isReverse, rect, mayUseScro
const editableRect = container.getBoundingClientRect();
const x = rect.left;
const y = isReverse ? ( editableRect.bottom - buffer ) : ( editableRect.top + buffer );
const selection = window.getSelection();

let range = hiddenCaretRangeFromPoint( document, x, y, container );

Expand Down Expand Up @@ -413,6 +412,7 @@ export function placeCaretAtVerticalEdge( container, isReverse, rect, mayUseScro
}
}

const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange( range );
container.focus();
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/post-saved-state/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export class PostSavedState extends Component {
isLargeViewport,
} = this.props;
const { forceSavedMessage } = this.state;
const hasPublishAction = get( post, [ '_links', 'wp:action-publish' ], false );
if ( isSaving ) {
// TODO: Classes generation should be common across all return
// paths of this function, including proper naming convention for
Expand Down Expand Up @@ -93,6 +92,7 @@ export class PostSavedState extends Component {

// Once the post has been submitted for review this button
// is not needed for the contributor role.
const hasPublishAction = get( post, [ '_links', 'wp:action-publish' ], false );
if ( ! hasPublishAction && isPending ) {
return null;
}
Expand Down
5 changes: 2 additions & 3 deletions packages/editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ export class RichText extends Component {
items = isNil( items ) ? [] : items;
files = isNil( files ) ? [] : files;

const item = find( [ ...items, ...files ], ( { type } ) => /^image\/(?:jpe?g|png|gif)$/.test( type ) );
let plainText = '';
let html = '';

Expand Down Expand Up @@ -244,6 +243,7 @@ export class RichText extends Component {

// Only process file if no HTML is present.
// Note: a pasted file may have the URL as plain text.
const item = find( [ ...items, ...files ], ( { type } ) => /^image\/(?:jpe?g|png|gif)$/.test( type ) );
if ( item && ! html ) {
const file = item.getAsFile ? item.getAsFile() : item;
const content = pasteHandler( {
Expand Down Expand Up @@ -605,12 +605,11 @@ export class RichText extends Component {
* @param {Object} context The context for splitting.
*/
splitContent( blocks = [], context = {} ) {
const record = this.createRecord();

if ( ! this.onSplit ) {
return;
}

const record = this.createRecord();
let [ before, after ] = split( record );

// In case split occurs at the trailing or leading edge of the field,
Expand Down
2 changes: 2 additions & 0 deletions packages/editor/src/editor-styles/ast/parse.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @wordpress/no-unused-vars-before-return */

// Adapted from https://github.com/reworkcss/css
// because we needed to remove source map support.

Expand Down
2 changes: 2 additions & 0 deletions packages/editor/src/editor-styles/ast/stringify/identity.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @wordpress/no-unused-vars-before-return */

// Adapted from https://github.com/reworkcss/css
// because we needed to remove source map support.

Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/store/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ export default {
const state = store.getState();
const [ firstBlockClientId, secondBlockClientId ] = action.blocks;
const blockA = getBlock( state, firstBlockClientId );
const blockB = getBlock( state, secondBlockClientId );
const blockType = getBlockType( blockA.name );

// Only focus the previous block if it's not mergeable
Expand All @@ -169,6 +168,7 @@ export default {

// We can only merge blocks with similar types
// thus, we transform the block to merge first
const blockB = getBlock( state, secondBlockClientId );
const blocksWithTheSameType = blockA.name === blockB.name ?
[ blockB ] :
switchToBlockType( blockB, blockA.name );
Expand Down
5 changes: 3 additions & 2 deletions packages/editor/src/store/effects/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ const TRASH_POST_NOTICE_ID = 'TRASH_POST_NOTICE_ID';
export const requestPostUpdate = async ( action, store ) => {
const { dispatch, getState } = store;
const state = getState();
const post = getCurrentPost( state );
const isAutosave = !! action.options.isAutosave;

// Prevent save if not saveable.
// We don't check for dirtiness here as this can be overriden in the UI.
Expand All @@ -59,6 +57,7 @@ export const requestPostUpdate = async ( action, store ) => {
}

let edits = getPostEdits( state );
const isAutosave = !! action.options.isAutosave;
if ( isAutosave ) {
edits = pick( edits, [ 'title', 'content', 'excerpt' ] );
}
Expand All @@ -78,6 +77,8 @@ export const requestPostUpdate = async ( action, store ) => {
edits = { status: 'draft', ...edits };
}

const post = getCurrentPost( state );

let toSend = {
...edits,
content: getEditedPostContent( state ),
Expand Down
11 changes: 6 additions & 5 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,14 @@ export function getCurrentPostAttribute( state, attributeName ) {
* @return {*} Post attribute value.
*/
export function getEditedPostAttribute( state, attributeName ) {
const edits = getPostEdits( state );

// Special cases
switch ( attributeName ) {
case 'content':
return getEditedPostContent( state );
}

// Fall back to saved post value if not edited.
const edits = getPostEdits( state );
if ( ! edits.hasOwnProperty( attributeName ) ) {
return getCurrentPostAttribute( state, attributeName );
}
Expand Down Expand Up @@ -349,13 +348,15 @@ export function getAutosaveAttribute( state, attributeName ) {
*/
export function getEditedPostVisibility( state ) {
const status = getEditedPostAttribute( state, 'status' );
const password = getEditedPostAttribute( state, 'password' );

if ( status === 'private' ) {
return 'private';
} else if ( password ) {
}

const password = getEditedPostAttribute( state, 'password' );
if ( password ) {
return 'password';
}

return 'public';
}

Expand Down

0 comments on commit 47e69f3

Please sign in to comment.