Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the grammar parser to the mobile app #7691

Merged
merged 10 commits into from
Jul 6, 2018

Conversation

hypest
Copy link
Contributor

@hypest hypest commented Jul 3, 2018

Description

Expose the parse function from the parser module to the native mobile app variant of the Block Api.

How has this been tested?

Besides successfully running npm test and npm run dev, tested this using this PR from the native app repo.

Types of changes

  • Adds a new feature, exposing the parser to the native app
  • Splits the way the parser is loaded to allow the pegjs parser (sourced and transformed via webpack) to be loaded for the web while a pre-generated parser is loaded on the native mobile since the transformation machinery is not yet available there
  • Adds the RawHTML function to the element package for the native mobile

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

Warning is:
"Warning: React.createElement: type is invalid -- expected a string (for
built-in components) or a class/function (for composite components) but
got: undefined."
@hypest hypest added [Feature] Parsing Related to efforts to improving the parsing of a string of data and converting it into a different f [Package] Element /packages/element Mobile App - i.e. Android or iOS Native mobile impl of the block editor. (Note: used in scripts, ping mobile folks to change) labels Jul 3, 2018
@hypest hypest requested a review from gziolo July 3, 2018 14:22
@hypest
Copy link
Contributor Author

hypest commented Jul 3, 2018

Any ideas on how to improve the testcov on this one? Not sure how to add tests for the native mobile code here.

*
* @return {WPElement} Dangerously-rendering element.
*/
export function RawHTML( { children, ...props } ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this component to its own file (raw-hatml.js?) and reference it from both index files to avoid code duplication and possible sync issues in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call @gziolo , made the change with da43e07.

I chose the rather boring name index-common.js for the base module, also having in mind that more functions will most probably end up there as we expose them to the native app. Let me know if you think the name can be a problem.

@gziolo
Copy link
Member

gziolo commented Jul 4, 2018

Any ideas on how to improve the testcov on this one? Not sure how to add tests for the native mobile code here.

We need to decide what to do about #7664, where I proposed moving the parser and grammar to their own package and skip Webpack loader altogether. This will solve the issue with the code coverage, but more importantly, it will make this module to work independently to Webpack config when its published to npm.

@hypest
Copy link
Contributor Author

hypest commented Jul 4, 2018

We need to decide what to do about #7664,

Right. I think that ideally, we want to have a simple and robust way to expose the parser to both the web and the native mobile app. So, #7664 in on the right path, for many reasons.

That said, it's not clear to me yet how soon we'll get there so, this PR with its pre-generated parser is a solution that helps unblock work on the native mobile side.

I've already added tests for the pre-generated parser so, if it's OK with you and provided #7664 doesn't get merged this week, I'd prefer if we merge this PR and optimize on top of it. Let me know what you think @gziolo .

*
* @return {Array} Block list.
*/
export function parsePreGenerated( content ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use the following code to avoid duplication in the blocks/api/parsers.js:

/**
 * Creates a parse implementation for the post content which returns a list of blocks.
 *
 * @param {Function} parseImplementation Parse implementation.
 *
 * @return {Function} An implementation which parses the post content.
 */
export const createParse = ( parseImplementation ) =>
    ( content ) => parseImplementation( content ).reduce( ( memo, blockNode ) => {
        const block = createBlockWithFallback( blockNode );
        if ( block ) {
            memo.push( block );
        }
        return memo;
    }, [] );

/**
 * Parses the post content with a PegJS grammar and returns a list of blocks.
 *
 * @param {string} content The post content.
 *
 * @return {Array} Block list.
 */
export const parseWithGrammar = createParse( grammarParse );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the proposal @gziolo ! Made the change in ca2a9ec.

type: 'string',
source: 'text',
// run the test cases using the PegJS defined parser
testCases( parsePegjs );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put those test cases in their own describe blocks to make it easier to debug which version of the parser failed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separated the 2 run in different describe() blocks with 2a012ff.

@@ -0,0 +1,20 @@
import { createElement } from 'react';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a convention, we add dependencies blocks for import statements, in this case it will be:

/**
 * External dependencies
 */

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, missed that, sorry :(. Fixed with 9ace658.

@hypest
Copy link
Contributor Author

hypest commented Jul 5, 2018

Addressed the new review @gziolo , ready for another pass, thanks!

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good, thanks for applying all those changes.

@gziolo gziolo merged commit 87252f2 into master Jul 6, 2018
@gziolo gziolo deleted the rnmobile/parser-updated-master branch July 6, 2018 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Parsing Related to efforts to improving the parsing of a string of data and converting it into a different f Mobile App - i.e. Android or iOS Native mobile impl of the block editor. (Note: used in scripts, ping mobile folks to change) [Package] Element /packages/element
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants