Skip to content

Commit

Permalink
Remove support for @prettier/plugin-php
Browse files Browse the repository at this point in the history
The PHP prettier plugin doesn’t support working with inline HTML. We tried to get this to work by treating `inline` blocks as HTML and telling prettier to format them but this introduced complications and resulted in errors in some files.

The correct way for this to work is for the PHP prettier plugin to introduce support for HTML itself and, if made to work with Prettier’s multi-language support, would automatically run Tailwind’s prettier plugin to format classes.

We were trying to be too clever here but ultimately fixing the problems would be the same as adding full HTML support to the PHP prettier plugin which is not something we can tackle.
  • Loading branch information
thecrypticace committed Apr 28, 2023
1 parent df84d8f commit 75f16eb
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 42 deletions.
31 changes: 0 additions & 31 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,6 @@ export const parsers = {
...(base.parsers.astro
? { astro: createParser('astro', transformAstro) }
: {}),
...(base.parsers.php ? { php: createParser('php', transformPHP) } : {}),
...(base.parsers.melody
? { melody: createParser('melody', transformMelody) }
: {}),
Expand Down Expand Up @@ -687,33 +686,6 @@ function transformAstro(ast, { env, changes }) {
}
}

function transformPHP(ast, { env, changes }) {
if (ast.kind === 'inline') {
let leading = ast.raw.match(/^\s*/)[0]
let trailing = ast.raw.match(/\s*$/)[0]

// If the inline block is just whitespace then we don't need to format
if (ast.raw === leading) {
return
}

// We have to parse this as HTML with prettier
let parsed = prettier.format(ast.raw, {
...env.options,
parser: 'html',
})

let formatted = `${leading}${parsed.trimEnd()}${trailing}`

ast.raw = formatted
ast.value = formatted
}

for (let child of ast.children ?? []) {
transformPHP(child, { env, changes })
}
}

/*
function transformBlade(ast, { env, changes }) {
// Blade gets formatted on parse
Expand Down Expand Up @@ -908,7 +880,6 @@ function getBasePlugins() {
// And we are not bundling it with the main Prettier plugin
let astro = loadIfExists('prettier-plugin-astro')
let svelte = loadIfExists('prettier-plugin-svelte')
let php = loadIfExists('@prettier/plugin-php')
let melody = loadIfExists('prettier-plugin-twig-melody')
let pug = loadIfExists('@prettier/plugin-pug')
let liquid = loadIfExists('@shopify/prettier-plugin-liquid')
Expand All @@ -935,7 +906,6 @@ function getBasePlugins() {

...(svelte?.parsers ?? {}),
...(astro?.parsers ?? {}),
...(php?.parsers ?? {}),
...(melody?.parsers ?? {}),
...(pug?.parsers ?? {}),
...(liquid?.parsers ?? {}),
Expand All @@ -961,7 +931,6 @@ function getCompatibleParser(parserFormat, options) {
'@ianvs/prettier-plugin-sort-imports',
'@trivago/prettier-plugin-sort-imports',
'prettier-plugin-organize-imports',
'@prettier/plugin-php',
'@prettier/plugin-pug',
'@shopify/prettier-plugin-liquid',
'@shufo/prettier-plugin-blade',
Expand Down
11 changes: 0 additions & 11 deletions tests/plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,6 @@ let tests = [
],
},
},
{
plugins: ['@prettier/plugin-php'],
tests: {
php: [
[
`<?php $test = function($a,$b){}\n ?>\n <div class="sm:p-0 p-4">Example</div>\n <?php $test = function($c,$d)use($e) {}`,
`<?php $test = function ($a, $b) {}; ?>\n <div class="p-4 sm:p-0">Example</div>\n <?php $test = function ($c, $d) use ($e) {};`,
],
],
},
},
{
plugins: ['prettier-plugin-import-sort'],
tests: {
Expand Down

0 comments on commit 75f16eb

Please sign in to comment.