Skip to content

<?=2; crashes #1264

@sugar700

Description

@sugar700

The following program.

require('prettier').format('<?=2;', { parser: 'php', plugins: [require('@prettier/plugin-php')] })

causes an exception

TypeError: Cannot read property 'includes' of undefined
    at printLines (node_modules/@prettier/plugin-php/src/printer.js:1077:31)
    at printNode (node_modules/@prettier/plugin-php/src/printer.js:1559:11)
    at Object.genericPrint [as print] (node_modules/@prettier/plugin-php/src/printer.js:116:32)
    at callPluginPrintFunction (node_modules/prettier/index.js:12984:18)

This is caused by the following code:

      const between = options.originalText.trim().match(/^<\?(php|=)(\s+)?\S/);
      const afterOpenTag = concat([
        between && between[2].includes("\n")
          ? concat([
              hardline,
              between[2].split("\n").length > 2 ? hardline : ""
            ])
          : " ",
        node.comments ? comments.printComments(node.comments, options) : ""
      ]);

The second match is optional because of ? character, but things crash if it actually becomes optional.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions