Skip to content

Commit

Permalink
apply new prettier rules
Browse files Browse the repository at this point in the history
  • Loading branch information
horuskol committed Apr 21, 2022
1 parent 71fd2ec commit e3a62b6
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 359 deletions.
146 changes: 58 additions & 88 deletions jest/customMatchers.js
Original file line number Diff line number Diff line change
@@ -1,180 +1,150 @@
const prettier = require("prettier");
const { diff } = require("jest-diff");
const prettier = require('prettier')
const { diff } = require('jest-diff')

function format(input) {
return prettier.format(input, {
parser: "css",
parser: 'css',
printWidth: 100,
});
})
}

expect.extend({
// Compare two CSS strings with all whitespace removed
// This is probably naive but it's fast and works well enough.
toMatchCss(received, argument) {
function stripped(str) {
return str.replace(/\s/g, "").replace(/;/g, "");
return str.replace(/\s/g, '').replace(/;/g, '')
}

const options = {
comment: "stripped(received) === stripped(argument)",
comment: 'stripped(received) === stripped(argument)',
isNot: this.isNot,
promise: this.promise,
};
}

const pass = stripped(received) === stripped(argument);
const pass = stripped(received) === stripped(argument)

const message = pass
? () => {
return (
this.utils.matcherHint(
"toMatchCss",
undefined,
undefined,
options
) +
"\n\n" +
this.utils.matcherHint('toMatchCss', undefined, undefined, options) +
'\n\n' +
`Expected: not ${this.utils.printExpected(format(received))}\n` +
`Received: ${this.utils.printReceived(format(argument))}`
);
)
}
: () => {
const actual = format(received);
const expected = format(argument);
const actual = format(received)
const expected = format(argument)

const diffString = diff(expected, actual, {
expand: this.expand,
});
})

return (
this.utils.matcherHint(
"toMatchCss",
undefined,
undefined,
options
) +
"\n\n" +
(diffString && diffString.includes("- Expect")
this.utils.matcherHint('toMatchCss', undefined, undefined, options) +
'\n\n' +
(diffString && diffString.includes('- Expect')
? `Difference:\n\n${diffString}`
: `Expected: ${this.utils.printExpected(expected)}\n` +
`Received: ${this.utils.printReceived(actual)}`)
);
};
)
}

return { actual: received, message, pass };
return { actual: received, message, pass }
},
toIncludeCss(received, argument) {
function stripped(str) {
return str.replace(/\s/g, "").replace(/;/g, "");
return str.replace(/\s/g, '').replace(/;/g, '')
}

const options = {
comment: "stripped(received).includes(stripped(argument))",
comment: 'stripped(received).includes(stripped(argument))',
isNot: this.isNot,
promise: this.promise,
};
}

const pass = stripped(received).includes(stripped(argument));
const pass = stripped(received).includes(stripped(argument))

const message = pass
? () => {
return (
this.utils.matcherHint(
"toIncludeCss",
undefined,
undefined,
options
) +
"\n\n" +
this.utils.matcherHint('toIncludeCss', undefined, undefined, options) +
'\n\n' +
`Expected: not ${this.utils.printExpected(format(received))}\n` +
`Received: ${this.utils.printReceived(format(argument))}`
);
)
}
: () => {
const actual = format(received);
const expected = format(argument);
const actual = format(received)
const expected = format(argument)

const diffString = diff(expected, actual, {
expand: this.expand,
});
})

return (
this.utils.matcherHint(
"toIncludeCss",
undefined,
undefined,
options
) +
"\n\n" +
(diffString && diffString.includes("- Expect")
this.utils.matcherHint('toIncludeCss', undefined, undefined, options) +
'\n\n' +
(diffString && diffString.includes('- Expect')
? `Difference:\n\n${diffString}`
: `Expected: ${this.utils.printExpected(expected)}\n` +
`Received: ${this.utils.printReceived(actual)}`)
);
};
)
}

return { actual: received, message, pass };
return { actual: received, message, pass }
},
});
})

expect.extend({
// Compare two CSS strings with all whitespace removed
// This is probably naive but it's fast and works well enough.
toMatchFormattedCss(received, argument) {
function format(input) {
return prettier.format(input.replace(/\n/g, ""), {
parser: "css",
return prettier.format(input.replace(/\n/g, ''), {
parser: 'css',
printWidth: 100,
});
})
}
const options = {
comment: "stripped(received) === stripped(argument)",
comment: 'stripped(received) === stripped(argument)',
isNot: this.isNot,
promise: this.promise,
};
}

let formattedReceived = format(received);
let formattedArgument = format(argument);
let formattedReceived = format(received)
let formattedArgument = format(argument)

const pass = formattedReceived === formattedArgument;
const pass = formattedReceived === formattedArgument

const message = pass
? () => {
return (
this.utils.matcherHint(
"toMatchCss",
undefined,
undefined,
options
) +
"\n\n" +
this.utils.matcherHint('toMatchCss', undefined, undefined, options) +
'\n\n' +
`Expected: not ${this.utils.printExpected(formattedReceived)}\n` +
`Received: ${this.utils.printReceived(formattedArgument)}`
);
)
}
: () => {
const actual = formattedReceived;
const expected = formattedArgument;
const actual = formattedReceived
const expected = formattedArgument

const diffString = diff(expected, actual, {
expand: this.expand,
});
})

return (
this.utils.matcherHint(
"toMatchCss",
undefined,
undefined,
options
) +
"\n\n" +
(diffString && diffString.includes("- Expect")
this.utils.matcherHint('toMatchCss', undefined, undefined, options) +
'\n\n' +
(diffString && diffString.includes('- Expect')
? `Difference:\n\n${diffString}`
: `Expected: ${this.utils.printExpected(expected)}\n` +
`Received: ${this.utils.printReceived(actual)}`)
);
};
)
}

return { actual: received, message, pass };
return { actual: received, message, pass }
},
});
})
10 changes: 5 additions & 5 deletions prettier.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ module.exports = {
// These settings are duplicated in .editorconfig:
tabWidth: 2, // indent_size = 2
useTabs: false, // indent_style = space
endOfLine: "lf", // end_of_line = lf
endOfLine: 'lf', // end_of_line = lf
semi: false, // default: true
singleQuote: true, // default: false
printWidth: 100, // default: 80
trailingComma: "es5",
trailingComma: 'es5',
bracketSpacing: true,
overrides: [
{
files: "*.js",
files: '*.js',
options: {
parser: "flow",
parser: 'flow',
},
},
],
};
}
62 changes: 31 additions & 31 deletions src/plugin.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@
const _ = require("lodash");
const extractGridAreaNames = require("./util/extractGridAreaNames");
const _ = require('lodash')
const extractGridAreaNames = require('./util/extractGridAreaNames')

module.exports = function ({ addUtilities, matchUtilities, theme, variants }) {
const gridAreaNames = extractGridAreaNames(theme("gridTemplateAreas"));
const gridAreaNames = extractGridAreaNames(theme('gridTemplateAreas'))

const templateAreas = _.reduce(
theme("gridTemplateAreas"),
theme('gridTemplateAreas'),
(templates, area, name) => {
return {
...templates,
[`.grid-areas-${name}`]: {
"grid-template-areas": area
'grid-template-areas': area
.map((row) => {
return `"${row}"`;
return `"${row}"`
})
.join("\n"),
.join('\n'),
},
};
}
},
{}
);
)

addUtilities([templateAreas], variants("gridTemplateAreas"));
addUtilities([templateAreas], variants('gridTemplateAreas'))

const namedAreas = gridAreaNames.reduce((areas, name) => {
return {
...areas,
[`.grid-in-${name}`]: {
"grid-area": name,
'grid-area': name,
},
};
}, {});
}
}, {})

addUtilities([namedAreas], []);
addUtilities([namedAreas], [])

const namedLines = gridAreaNames.reduce((lines, name) => {
return {
...lines,
[`.row-start-${name}`]: {
"grid-row-start": `${name}-start`,
'grid-row-start': `${name}-start`,
},
[`.row-end-${name}`]: {
"grid-row-end": `${name}-end`,
'grid-row-end': `${name}-end`,
},
[`.col-start-${name}`]: {
"grid-column-start": `${name}-start`,
'grid-column-start': `${name}-start`,
},
[`.col-end-${name}`]: {
"grid-column-end": `${name}-end`,
'grid-column-end': `${name}-end`,
},
};
}, {});
}
}, {})

addUtilities([namedLines], []);
addUtilities([namedLines], [])

// allow arbitrary values
matchUtilities({
"grid-areas": (value) => {
value = `"${value}"`;
value = value.replace(/ *, */g, '" "');
'grid-areas': (value) => {
value = `"${value}"`
value = value.replace(/ *, */g, '" "')
return {
"grid-template-areas": `${value}`,
};
'grid-template-areas': `${value}`,
}
},
"grid-in": (value) => {
'grid-in': (value) => {
return {
"grid-area": `${value}`,
};
'grid-area': `${value}`,
}
},
});
};
})
}
16 changes: 8 additions & 8 deletions src/util/extractGridAreaNames.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const _ = require("lodash");
const _ = require('lodash')

module.exports = function (gridTemplateAreas) {
return _.uniq(
_.flatMap(gridTemplateAreas, (row) => {
return _.flatMap(row, (area) => {
// extract grid area names from the gridTemplate
return _.flatMap(area.match(/[^\s]+/g), (match) => {
if (match !== ".") {
return match;
if (match !== '.') {
return match
}
return [];
});
});
return []
})
})
})
);
};
)
}
Loading

0 comments on commit e3a62b6

Please sign in to comment.