Skip to content

Commit

Permalink
Merge pull request #259 from patricklx/patch-2
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Jun 16, 2023
2 parents c9e0f97 + 620d1d0 commit bff5cb8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ module.exports = {

The recommended set will apply [these rules](https://github.com/ember-template-lint/ember-template-lint-plugin-prettier/blob/v1.1.0-beta.0/lib/config/recommended.js).

## Configuration

Prettier can be configured via [standard prettier config files](https://prettier.io/docs/en/configuration.html).

## Tips

You may want to define these two scripts in your package.json:
Expand Down
46 changes: 24 additions & 22 deletions lib/rules/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ export default class Prettier extends Rule {
this.filePath = options.filePath;
}

getPrettierOptions() {
let filepath = this.filePath;

if (!prettier) {
// Prettier is expensive to load, so only load it if needed.
prettier = require("prettier");
}

const prettierRcOptions = prettier.resolveConfig.sync(filepath, {
editorconfig: true,
});

return Object.assign({}, { parser: "glimmer" }, prettierRcOptions, {
filepath,
});
}

visitor() {
return {
Program: {
Expand All @@ -36,35 +53,20 @@ export default class Prettier extends Rule {
}

const source = this.sourceForNode(node);
let filepath = this.filePath;

if (!prettier) {
// Prettier is expensive to load, so only load it if needed.
prettier = require("prettier");
}

const prettierRcOptions = prettier.resolveConfig.sync(filepath, {
editorconfig: true,
});
const prettierOptions = this.getPrettierOptions();

const prettierFileInfo = prettier.getFileInfo.sync(filepath, {
ignorePath: ".prettierignore",
});
const prettierFileInfo = prettier.getFileInfo.sync(
prettierOptions.filepath,
{
ignorePath: ".prettierignore",
}
);

// Skip if file is ignored using a .prettierignore file
if (prettierFileInfo.ignored) {
return;
}

const prettierOptions = Object.assign(
{},
{ parser: "glimmer" },
prettierRcOptions,
{
filepath,
}
);

let prettierSource;
try {
prettierSource = prettier.format(source, prettierOptions);
Expand Down

0 comments on commit bff5cb8

Please sign in to comment.