Skip to content

Conversation

fisker
Copy link
Member

@fisker fisker commented Jun 1, 2020

To play with it,

mkdir prettier-vue-pug-test
cd prettier-vue-pug-test
yarn init -y
yarn add fisker/prettier#vue-embed @prettier/plugin-pug
code test.vue
npx prettier test.vue --plugin-search-dir=.

--plugin-search-dir won't need when we fix #8474

Fixes #6298

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory)
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/pr-XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@Shinigami92
Copy link
Member

First local test seems to work

image

OS: Windows 10
Used Terminal: cmd


Blank lines above and below the code could be on my side
(especially the blank line below)

@Shinigami92
Copy link
Member

With logs enabled and using yarn link "@prettier/plugin-pug":

D:\Shinigami\Documents\Projects\prettier-vue-pug-test>npx prettier test.vue --plugin-search-dir=.
test.vue[parsers:pug:preprocess]: { text: '\n  div.text( color =   "primary",  disabled    )\n' }
[parsers:pug:parse]: { text: '\n  div.text( color =   "primary",  disabled    )\n' }
[printers:pug-ast:embed]: {
  "stack": [
    [
      {
        "type": "indent",
        "loc": {
          "start": {
            "line": 2,
            "column": 1
          },
          "end": {
            "line": 2,
            "column": 3
          }
        },
        "val": 2
      },
      {
        "type": "tag",
        "loc": {
          "start": {
            "line": 2,
            "column": 3
          },
          "end": {
            "line": 2,
            "column": 6
          }
        },
        "val": "div"
      },
      {
        "type": "class",
        "loc": {
          "start": {
            "line": 2,
            "column": 6
          },
          "end": {
            "line": 2,
            "column": 11
          }
        },
        "val": "text"
      },
      {
        "type": "start-attributes",
        "loc": {
          "start": {
            "line": 2,
            "column": 11
          },
          "end": {
            "line": 2,
            "column": 12
          }
        }
      },
      {
        "type": "attribute",
        "loc": {
          "start": {
            "line": 2,
            "column": 13
          },
          "end": {
            "line": 2,
            "column": 32
          }
        },
        "name": "color",
        "val": "\"primary\"",
        "mustEscape": true
      },
      {
        "type": "attribute",
        "loc": {
          "start": {
            "line": 2,
            "column": 35
          },
          "end": {
            "line": 2,
            "column": 43
          }
        },
        "name": "disabled",
        "val": true,
        "mustEscape": true
      },
      {
        "type": "end-attributes",
        "loc": {
          "start": {
            "line": 2,
            "column": 47
          },
          "end": {
            "line": 2,
            "column": 48
          }
        }
      },
      {
        "type": "outdent",
        "loc": {
          "start": {
            "line": 3,
            "column": 1
          },
          "end": {
            "line": 3,
            "column": 1
          }
        }
      },
      {
        "type": "eos",
        "loc": {
          "start": {
            "line": 3,
            "column": 1
          },
          "end": {
            "line": 3,
            "column": 1
          }
        }
      }
    ]
  ]
}
[printers:pug-ast:print]:
  .text(color="primary", disabled)

<template lang="pug">

  .text(color="primary", disabled)

</template>

<script>
export default {
  name: "Hello",
};
</script>

Thats the first time my plugin receiving something from vue <template lang="pug"> 🎉

@fisker fisker marked this pull request as ready for review June 1, 2020 15:34
@fisker fisker requested a review from sosukesuzuki June 2, 2020 02:50
Copy link
Member

@sosukesuzuki sosukesuzuki left a comment

Choose a reason for hiding this comment

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

Overall looks good.

I have one question, should we respect vue-indent-script-and-style ?

Prettier pr-8465
Playground link

--parser vue
--vue-indent-script-and-style

Input:

<script lang="json">
{
  "foo": "bar"
}
</script>

<custom lang="json">
{
  "foo": "bar"
}
</custom>

Output:

<script lang="json">
  {
    "foo": "bar"
  }
</script>

<custom lang="json">
{
  "foo": "bar"
}
</custom>

@fisker
Copy link
Member Author

fisker commented Jun 4, 2020

I'm not sure about that, vue-indent-script-and-style mean to indent <script> and <style>, should we apply to those blocks ? They are not <script> 😄

There might be another problem(but I'm not sure), plugins will format these blocks, what if this language/plugin indent sensitive? Maybe python? or does pug care?

@@ -56,8 +56,11 @@ const isTestDirectory = (dirname, name) =>
dirname.startsWith(path.join(__dirname, "../tests", name));

global.run_spec = (fixtures, parsers, options) => {
// Can't test plugins on standalone
// Can't test plugins on standalone, a empty test to pass
Copy link
Member

Choose a reason for hiding this comment

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

You can test them if you pass them as objects, not strings.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've tried this before... I forgot. Can't work, maybe because there are functions?

Copy link
Member

Choose a reason for hiding this comment

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

Okay. Let's keep it this way. I might look at this later.

Copy link
Member Author

Choose a reason for hiding this comment

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

Are you talking about a empty test to pass? This not work too, snapshots don't match.

I ignored it in jest.config.js for now.

Copy link
Member

Choose a reason for hiding this comment

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

No, about passing the plugin as an object. It should work. If it doesn't, something isn't right.

@Shinigami92
Copy link
Member

Shinigami92 commented Jun 4, 2020

I'm not sure about that, vue-indent-script-and-style mean to indent <script> and <style>, should we apply to those blocks ? They are not <script> 😄

There might be another problem(but I'm not sure), plugins will format these blocks, what if this language/plugin indent sensitive? Maybe python? or does pug care?

In any case, it makes sense for HTML or XML-based languages

<template>
  <div>
    <p>some content</p>
  </div>
</template>

But for non XML based languages like pug? 🤷‍♂️

<template lang="pug">
div
  p some content
</template>

IMO makes no sense for such languages


let doc;
try {
doc = textToDoc(getNodeContent(node, options), { parser });
Copy link
Member Author

@fisker fisker Jun 4, 2020

Choose a reason for hiding this comment

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

This text pass to parser need discuss too,

<custom lang="pug">
  div
</custom>

What we should pass to plugin?

  • "\n div\n" (current)
  • " div" (trim leading and trailing empty lines)
  • " div\n" (trim trailing empty lines)
  • "div" (this should not consider)

Copy link
Member

Choose a reason for hiding this comment

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

I would prefer the exact behavior as currently with the embedded markdown

Copy link
Member Author

Choose a reason for hiding this comment

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

Which is?

if same

```pug
  div
```

Copy link
Member Author

Choose a reason for hiding this comment

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

Seems removed one line on each side, means extra blank lines kept. https://github.com/prettier/prettier/blob/master/src/language-markdown/utils.js#L220

Copy link
Member

Choose a reason for hiding this comment

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

Beside the fact that my plugin has a bug

embedded markdown

```pug
  div
```

currently formats to (bug)

```pug
| div
```

but should format to

```pug
div
```

Copy link
Member

Choose a reason for hiding this comment

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

@fisker Please update your @prettier/plugin-pug to v1.4.4
I fixed the bug

This reverts commit 4ca5990.
@Shinigami92
Copy link
Member

lang="uppercase-rocks" 😂
I like that 👍

@fisker
Copy link
Member Author

fisker commented Jun 8, 2020

lang="uppercase-rocks" 😂
I like that 👍

NO NO NO, YOU LIKE THAT!

@Shinigami92
Copy link
Member

lang="uppercase-rocks" 😂
I like that 👍

NO NO NO, YOU LIKE THAT!

OH MY GOD, IT TOOK ME SO LONG TO UNDERSTAND THIS JOKE XD

@fisker
Copy link
Member Author

fisker commented Jun 12, 2020

If no objection about vue-indent-script-and-style, approve?

@prettier/core

Copy link
Member

@sosukesuzuki sosukesuzuki left a comment

Choose a reason for hiding this comment

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

Good job

@thorn0
Copy link
Member

thorn0 commented Jun 15, 2020

Changelog?

@fisker
Copy link
Member Author

fisker commented Jun 17, 2020

Shared a changelog with #8023

@fisker
Copy link
Member Author

fisker commented Jun 17, 2020

I forgot discuss this with you guys, now we have extra \n for pug

<template lang="pug">
.text(color="primary", disabled="true")

</template>

First, plugins prints own \n? Why not add this in core.format()? @thorn0 Do you know the reason?

With builtin embed languages, we use stripTrailingHardline to remove the \n, but @prettier/plugin-pug returns a string. Should we keep it? or remove it? or ask plugins (in this case @prettier/plugin-pug) to remove it?

In this case, I think I should remove the \n if it's a string. But we should improve this in future.

@thorn0 @Shinigami92 @sosukesuzuki Thoughts?

@Shinigami92
Copy link
Member

I think this can be managed on my (plugin) side. So when the embed function is called, I could remove it at the end.

But why is markdown already working? 🤔

@fisker
Copy link
Member Author

fisker commented Jun 17, 2020

Interesting, I had a quick look, seems markdown did not insert a hardline after the code, it use the \n return from textToDoc,

replaceNewlinesWithLiterallines(doc),

But, I don't think that's the right way, if a plugin didn't add final_newline, it will be

```foo
text```

?

@thorn0
Copy link
Member

thorn0 commented Jun 17, 2020

@fisker It's unlikely there is a good reason for that. Just history I guess. Overall, 3rd party plugins should behave the same way the builtin plugins do, and we need to document these requirements. However, in this case, we need to decide first if we want to change this behavior for the builtin plugins and move the addition of the final new line to the core. I think this change would make sense.

@fisker
Copy link
Member Author

fisker commented Jun 17, 2020

I'm going to remove this \n manually if it's a string, and move this discussion to a new issue. Let's decide there, sounds good?

@thorn0
Copy link
Member

thorn0 commented Jun 17, 2020

@fisker Good

@fisker
Copy link
Member Author

fisker commented Jun 17, 2020

#8582

@fisker fisker merged commit 8999cc7 into prettier:master Jun 17, 2020
@fisker fisker deleted the vue-embed branch June 17, 2020 11:18
@fisker
Copy link
Member Author

fisker commented Jun 18, 2020

@thorn0 You might want look into this #8465 (comment)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Plugin autoload doesn't work for packages installed from GitHub and for Yarn PnP Pug plugin doesn't work for the .vue files
4 participants