Skip to content

Commit

Permalink
feat: add strict option (#532)
Browse files Browse the repository at this point in the history
  • Loading branch information
snyamathi authored Nov 23, 2022
1 parent b9a1dcf commit 2041b46
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-laws-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"atomizer": minor
---

feat: add strict option
1 change: 1 addition & 0 deletions packages/atomizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Usage: atomizer [options] [path]
--rtl swaps `start` and `end` keyword replacements with `right` and `left`.
--exclude=[pattern] pattern to exclude file to be scanned
--bump-mq increases specificity of media queries a small amount
--strict bail if missing configs such as breakpoints.
--verbose show additional log info (warnings).
```
Expand Down
1 change: 1 addition & 0 deletions packages/atomizer/bin/atomizer
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ program
.option('--rtl', 'swaps `start` and `end` keyword replacements with `right` and `left`')
.option('--bump-mq', 'increases specificity of media queries a small amount')
.option('--ie', '[deprecated] no longer used')
.option('--strict', 'bail if missing configs such as breakpoints')
.option('--verbose', 'show additional log info (warnings)')
.option('--quiet', 'hide processing info')
.parse(process.argv);
Expand Down
2 changes: 2 additions & 0 deletions packages/atomizer/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ declare module 'atomizer' {
rtl?: boolean;
/** Path to custom rules file */
rules?: string;
/** Bail if missing declarations such as break points */
strict?: boolean;
/** Show additional log info (warnings) */
verbose?: boolean;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/atomizer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"*.d.ts"
],
"scripts": {
"lint": "eslint --ext .js .",
"lint:fix": "eslint --ext .js . --fix",
"lint": "eslint --ext .js . && prettier . --check",
"lint:fix": "eslint --ext .js . --fix && prettier . --write",
"test": "jest tests/"
},
"bin": {
Expand Down
20 changes: 19 additions & 1 deletion packages/atomizer/src/atomizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const GLOBAL_VALUES = {
* @param {Array<import('atomizer').AtomizerRule>} [rules] List of custom rules
*/
function Atomizer(options, rules) {
this.verbose = (options && options.verbose) || false;
options = options || {};
this.strict = options.strict || false;
this.verbose = options.verbose || false;
this.rules = [];
// we have two different objects to avoid name collision
this.rulesMap = {};
Expand Down Expand Up @@ -481,6 +483,8 @@ Atomizer.prototype.parseConfig = function (config, options) {
*/
Atomizer.prototype.getCss = function (config, options) {
const jss = {};
const isStrict = this.strict;
const isVerbose = this.verbose;
let content = '';
let breakPoints;

Expand Down Expand Up @@ -532,6 +536,20 @@ Atomizer.prototype.getCss = function (config, options) {
}

const breakPoint = breakPoints && breakPoints[treeo.breakPoint];
if (treeo.breakPoint && !breakPoint) {
const message = [
`Class \`${treeo.className}\` contains breakpoint \`${treeo.breakPoint}\` which does not exist and must be manually added to your config file:`,
'"breakPoints": {',
` "${treeo.breakPoint}": <YOUR-CUSTOM-VALUE>`,
'}',
].join('\n');
if (isStrict) {
console.error('Error:', message);
process.exit(1);
} else if (isVerbose) {
console.warn('Warning:', message);
}
}

// this is where we start writing the selector
selector = Atomizer.escapeSelector(treeo.className);
Expand Down
2 changes: 1 addition & 1 deletion packages/atomizer/src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module.exports.buildAtomicCss = function (files, config = {}, options = {}, done
}

// Instantiate Atomizer
const atomizer = new Atomizer({ verbose: !!options.verbose });
const atomizer = new Atomizer({ strict: !!options.strict, verbose: !!options.verbose });

// Custom rulesets
const rulesFiles = options.rules;
Expand Down
18 changes: 18 additions & 0 deletions packages/atomizer/tests/atomizer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,24 @@ describe('Atomizer()', () => {
});
});
describe('getCss()', () => {
it('should bail if a breakpoint is missing in strict mode', () => {
const consoleError = jest.spyOn(console, 'error').mockImplementation(() => {});
const processExit = jest.spyOn(process, 'exit').mockImplementation(() => {});

new Atomizer({ strict: true }).getCss({ classNames: ['W(100%)--sm'] });

expect(consoleError).toHaveBeenCalledWith(
'Error:',
expect.stringContaining(
'Class `W(100%)--sm` contains breakpoint `sm` which does not exist and must be manually added to your config file'
)
);
consoleError.mockRestore();

expect(processExit).toHaveBeenCalledWith(1);
processExit.mockRestore();
});

it('returns expected css for custom classes with break points', () => {
// set rules here so if helper change, we don't fail the test
const atomizer = new Atomizer();
Expand Down

0 comments on commit 2041b46

Please sign in to comment.