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

Add fileInfoOptions to override options passed to prettier.getFileInfo #217

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ Objects are passed directly to Prettier as [options](https://prettier.io/docs/en

NB: This option will merge and override any config set with `.prettierrc` files (for Prettier < 1.7.0, config files are ignored)

### Additional Options

- fileInfoOptions: Options that are passed to [prettier.getFileInfo](https://prettier.io/docs/en/api.html#prettiergetfileinfofilepath--options) in order to decide if a particular file needs to be formatted. As an example, this can be used to override the default ignorePath inferred from the current working directory.

```json
{
"rules": {
"prettier/prettier": [
true,
{"fileInfoOptions": {"ignorePath": "../../.prettierignore"}}
Copy link
Author

Choose a reason for hiding this comment

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

Small note - ESLint has a third "slot" for options, I did not find a similar pattern for Stylelint, but I may just be not as familiar.

Theirs looks like this:

"prettier/prettier": ["error", { /* this is where Stylelint configures fileInfoOptions instead */ }, {
  "fileInfoOptions": {
    "withNodeModules": true
  }
}]

Let me know if there is a preferred approach instead.

]
}
}
```

---

## Contributing
Expand Down
16 changes: 12 additions & 4 deletions stylelint-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,17 @@ module.exports = stylelint.createPlugin(
editorconfig: true,
});

const prettierFileInfo = prettier.getFileInfo.sync(filepath, {
resolveConfig: true,
ignorePath: '.prettierignore',
});
const stylelintFileInfoOptions =
(options && options.fileInfoOptions) || {};

const prettierFileInfo = prettier.getFileInfo.sync(
filepath,
Object.assign(
{},
{resolveConfig: true, ignorePath: '.prettierignore'},
stylelintFileInfoOptions
)
);

// Skip if file is ignored using a .prettierignore file
if (prettierFileInfo.ignored) {
Expand Down Expand Up @@ -229,6 +236,7 @@ function omitStylelintSpecificOptions(options) {
const prettierOptions = Object.assign({}, options);
delete prettierOptions.message;
delete prettierOptions.severity;
delete prettierOptions.fileInfoOptions;
return prettierOptions;
}

Expand Down
1 change: 1 addition & 0 deletions test/prettierrc/file-options/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom-ignore-me.css
6 changes: 6 additions & 0 deletions test/prettierrc/file-options/custom-ignore-me.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.x {


color: red;
}

23 changes: 23 additions & 0 deletions test/stylelint-prettier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,29 @@ describe('stylelint configurations', () => {
);
});
});

it('resolves prettier fileInfoOptions', () => {
const linted = stylelint.lint({
files: [filename('file-options', 'custom-ignore-me.css')],
config: {
plugins: ['./'],
rules: {
'prettier/prettier': [
true,
{
fileInfoOptions: {
ignorePath: filename('file-options', '.prettierignore'),
},
},
],
},
},
});

return linted.then(({errored}) => {
expect(errored).toBe(false);
});
});
});

/**
Expand Down