-
-
Notifications
You must be signed in to change notification settings - Fork 929
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
Fix isStandardSyntaxAtRule.test.js
that use callbacks
#4957
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,136 +1,112 @@ | ||
'use strict'; | ||
|
||
const isStandardSyntaxAtRule = require('../isStandardSyntaxAtRule'); | ||
const less = require('postcss-less'); | ||
const postcss = require('postcss'); | ||
const sass = require('postcss-sass'); | ||
const scss = require('postcss-scss'); | ||
const postcssLess = require('postcss-less'); | ||
const postcssSass = require('postcss-sass'); | ||
const postcssScss = require('postcss-scss'); | ||
|
||
describe('isStandardSyntaxAtRule', () => { | ||
it('non nested at-rules without quotes', () => { | ||
atRules('@charset UTF-8;', (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRules('@charset UTF-8;')[0])).toBeTruthy(); | ||
ybiquitous marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
it("non nested at-rules with `'` quotes", () => { | ||
atRules("@charset 'UTF-8';", (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRules("@charset 'UTF-8';")[0])).toBeTruthy(); | ||
}); | ||
|
||
it('non nested at-rules with `"` quotes', () => { | ||
atRules('@charset "UTF-8";', (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRules('@charset "UTF-8";')[0])).toBeTruthy(); | ||
}); | ||
|
||
it("non nested at-rules with `'` quotes and without space after name", () => { | ||
atRules("@charset'UTF-8';", (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRules("@charset'UTF-8';")[0])).toBeTruthy(); | ||
}); | ||
|
||
it('non nested at-rules with `"` quotes and without space after name', () => { | ||
atRules('@charset"UTF-8";', (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRules('@charset"UTF-8";')[0])).toBeTruthy(); | ||
}); | ||
|
||
it('non nested at-rules with function and without space after name', () => { | ||
atRules('@import url("fineprint.css") print;', (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRules('@import url("fineprint.css") print;')[0])).toBeTruthy(); | ||
}); | ||
|
||
it('nested at-rules', () => { | ||
atRules('@media (min-width: 100px) {};', (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRules('@media (min-width: 100px) {};')[0])).toBeTruthy(); | ||
}); | ||
|
||
it('nested at-rules with newline after name', () => { | ||
atRules('@media\n(min-width: 100px) {};', (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRules('@media\n(min-width: 100px) {};')[0])).toBeTruthy(); | ||
}); | ||
|
||
it('nested at-rules with windows newline after name', () => { | ||
atRules('@media\r\n(min-width: 100px) {};', (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRules('@media\r\n(min-width: 100px) {};')[0])).toBeTruthy(); | ||
}); | ||
|
||
it('nested at-rules without space after name', () => { | ||
atRules('@media(min-width: 100px) {};', (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRules('@media(min-width: 100px) {};')[0])).toBeTruthy(); | ||
}); | ||
|
||
it('ignore `@content` inside mixins newline', () => { | ||
const sass = '@mixin mixin()\n @content'; | ||
// eslint-disable-next-line jest/no-disabled-tests -- TODO: `postcss-sass` parser does not support `@mixin`. | ||
it.skip('ignore `@content` inside mixins newline', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Has anybody left an issue on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found a PR to add the support of |
||
const rules = sassAtRules('@mixin mixin()\n @content'); | ||
|
||
sassAtRules(sass, (atRule) => { | ||
if (atRule.name === 'mixin') { | ||
return; | ||
} | ||
|
||
expect(isStandardSyntaxAtRule(atRule)).toBeFalsy(); | ||
}); | ||
expect(rules).toHaveLength(2); | ||
expect(rules.map((rule) => rule.name)).toEqual(['mixin', 'content']); | ||
expect(isStandardSyntaxAtRule(rules[0])).toBeTruthy(); | ||
expect(isStandardSyntaxAtRule(rules[1])).toBeFalsy(); | ||
}); | ||
|
||
it('ignore `@content` inside mixins space', () => { | ||
const scss = '@mixin mixin() { @content; };'; | ||
|
||
scssAtRules(scss, (atRule) => { | ||
if (atRule.name === 'mixin') { | ||
return; | ||
} | ||
const rules = scssAtRules('@mixin mixin() { @content; };'); | ||
|
||
expect(isStandardSyntaxAtRule(atRule)).toBeFalsy(); | ||
}); | ||
expect(rules).toHaveLength(2); | ||
expect(rules.map((rule) => rule.name)).toEqual(['mixin', 'content']); | ||
expect(isStandardSyntaxAtRule(rules[0])).toBeTruthy(); | ||
expect(isStandardSyntaxAtRule(rules[1])).toBeFalsy(); | ||
}); | ||
|
||
it('ignore passing rulesets to mixins', () => { | ||
const less = '@detached-ruleset: { background: red; }; .top { @detached-ruleset(); }'; | ||
const rules = lessAtRules( | ||
'@detached-ruleset: { background: red; }; .top { @detached-ruleset(); }', | ||
); | ||
|
||
lessAtRules(less, (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeFalsy(); | ||
}); | ||
expect(rules).toHaveLength(2); | ||
expect(isStandardSyntaxAtRule(rules[0])).toBeFalsy(); | ||
expect(isStandardSyntaxAtRule(rules[1])).toBeFalsy(); | ||
}); | ||
|
||
it('ignore calling of mixins', () => { | ||
const less = 'a { .mixin(); }'; | ||
const rules = lessAtRules('a { .mixin(); }'); | ||
|
||
lessAtRules(less, (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeFalsy(); | ||
}); | ||
expect(rules).toHaveLength(1); | ||
expect(isStandardSyntaxAtRule(rules[0])).toBeFalsy(); | ||
}); | ||
|
||
it('ignore variables', () => { | ||
const less = ` | ||
@my-variable: 10px; | ||
.top { margin-top: @my-variable; } | ||
`; | ||
|
||
lessAtRules(less, (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeFalsy(); | ||
}); | ||
const rules = lessAtRules('@my-variable: 10px; .top { margin-top: @my-variable; }'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [note] I think the one-line is easier to read here such as |
||
|
||
expect(rules).toHaveLength(1); | ||
expect(isStandardSyntaxAtRule(rules[0])).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
function atRules(css, cb) { | ||
postcss.parse(css).walkAtRules(cb); | ||
function atRules(code, parser = postcss) { | ||
const rules = []; | ||
|
||
parser.parse(code).walkAtRules((rule) => rules.push(rule)); | ||
|
||
return rules; | ||
} | ||
|
||
function sassAtRules(css, cb) { | ||
sass.parse(css).walkAtRules(cb); | ||
function sassAtRules(code) { | ||
return atRules(code, postcssSass); | ||
} | ||
|
||
function scssAtRules(css, cb) { | ||
scss.parse(css).walkAtRules(cb); | ||
function scssAtRules(code) { | ||
return atRules(code, postcssScss); | ||
} | ||
|
||
function lessAtRules(css, cb) { | ||
less.parse(css).walkAtRules(cb); | ||
function lessAtRules(code) { | ||
return atRules(code, postcssLess); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[note] I've renamed the variables not to hide local variables with the same name, e.g.