Skip to content

Commit

Permalink
Fix(prettier): It should replace existing overrides for the same patt…
Browse files Browse the repository at this point in the history
…ern instead of adding a new item
  • Loading branch information
sapegin committed Jun 18, 2020
1 parent 2ccf4d1 commit 3df42c0
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 18 deletions.
20 changes: 20 additions & 0 deletions packages/mrm-task-prettier/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ exports[`should infer options from EditorConfig 1`] = `
}"
`;

exports[`should replace existing overrides for the same pattern 1`] = `
"{
\\"useTabs\\": true,
\\"overrides\\": [
{
\\"files\\": \\"*.js\\",
\\"options\\": {
\\"useTabs\\": false
}
},
{
\\"files\\": \\"*.md\\",
\\"options\\": {
\\"printWidth\\": 42
}
}
]
}"
`;

exports[`should use a custom indent 1`] = `
"{
\\"useTabs\\": true,
Expand Down
56 changes: 38 additions & 18 deletions packages/mrm-task-prettier/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ function getPattern(pkg) {
return `**/*.{${exts.join(',')}}`;
}

// Remove options that have the same values as Prettier defaults
function removeDefaultOptions(options) {
const newOptions = { ...options };
for (const option in newOptions) {
if (newOptions[option] === defaultPrettierOptions[option]) {
delete newOptions[option];
}
}
return newOptions;
}

module.exports = function task({
indent,
prettierPattern,
Expand All @@ -74,28 +85,37 @@ module.exports = function task({
const pkg = packageJson();

const overrides = prettierOverrides || defaultOverrides;
const options = Object.assign(
{},
prettierOptions
? {}
: {
useTabs: indent === 'tab',
},
editorconfigOptions,
prettierOptions,
overrides && { overrides }
const options = removeDefaultOptions(
Object.assign(
{},
prettierOptions
? {}
: {
useTabs: indent === 'tab',
},
editorconfigOptions,
prettierOptions
)
);

// Remove options that have the same values as Prettier defaults
for (const option in options) {
if (options[option] === defaultPrettierOptions[option]) {
delete options[option];
}
}

// .prettierrc
json('.prettierrc')
const prettierrc = json('.prettierrc');

// Get existing overrides and remove the ones we're going to add
const overridePatterns = overrides.map(override => override.files);
const oldOverrides = prettierrc
.get('overrides', [])
.filter(override => !overridePatterns.includes(override.files));

// Merge existing overrides with new ones
const newOverrides = [...oldOverrides, ...overrides];

// Update options and save
prettierrc
.merge(options)
// unset/set to make sure overrides are always placed after options
.unset('overrides')
.set('overrides', newOverrides)
.save();

const pattern =
Expand Down
38 changes: 38 additions & 0 deletions packages/mrm-task-prettier/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,44 @@ it('should use custom overrides', async () => {
expect(vol.toJSON()['/.prettierrc']).toMatchSnapshot();
});

it('should replace existing overrides for the same pattern', async () => {
vol.fromJSON({
'/.prettierrc': stringify({
overrides: [
{
files: '*.js',
options: {
useTabs: false,
},
},
{
files: '*.md',
options: {
useTabs: false,
printWidth: 30,
},
},
],
}),
'/package.json': packageJson,
});

task(
await getTaskOptions(task, false, {
prettierOverrides: [
{
files: '*.md',
options: {
printWidth: 42,
},
},
],
})
);

expect(vol.toJSON()['/.prettierrc']).toMatchSnapshot();
});

it('should infer options from EditorConfig', async () => {
vol.fromJSON({
'/.editorconfig': '[*]\nindent_style = space\nindent_size = 2',
Expand Down

0 comments on commit 3df42c0

Please sign in to comment.