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 ignoreAtRules option to no-invalid-position-at-import #5520

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/rules/no-invalid-position-at-import-rule/README.md
Expand Up @@ -49,3 +49,33 @@ a {}
@charset 'utf-8';
@import 'foo.css';
```

## Optional secondary options

### `ignoreAtRules: ["/regex/", /regex/, "string"]`

Given:

```json
["/^my-/", "custom"]
```

The following patterns are _not_ considered violations:

<!-- prettier-ignore -->
```css
@my-at-rule "bar.css";
@import "foo.css";
```

<!-- prettier-ignore -->
```css
@my-other-at-rule {}
@import "foo.css";
```

<!-- prettier-ignore -->
```css
@custom "bar.css";
@import "foo.css"
```
53 changes: 53 additions & 0 deletions lib/rules/no-invalid-position-at-import-rule/__tests__/index.js
Expand Up @@ -113,6 +113,59 @@ testRule({
],
});

testRule({
ruleName,
config: [true, { ignoreAtRules: ['bar', '/^my-/', /foo/] }],

accept: [
{
code: stripIndent`
@bar 'bar.css';
@import 'foo.css';
`,
description: 'ignored atRule at the beggining - equal string',
},
{
code: stripIndent`
@foofoo 'bar.css';
@import 'foo.css';
`,
description: 'ignored atRule at the beggining - match regExp',
},
{
code: stripIndent`
@my-custom-rule {};
@import 'foo.css';
`,
description: 'ignored atRule at the beggining - match regExp',
},
],

reject: [
{
code: stripIndent`
@baz 'bar.css';
@import 'foo.css';
`,
message: messages.rejected,
description: 'atRule does not match the pattern',
line: 2,
column: 1,
},
{
code: stripIndent`
@bar 'bar.css';
@baz 'bar.css';
@import 'foo.css';
`,
message: messages.rejected,
description: 'one of atRules before @import does not match the pattern',
line: 3,
column: 1,
},
],
});

testRule({
Copy link
Member

Choose a reason for hiding this comment

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

Let's move this above the previous testRule as we tend to put non-standard tests last.

ruleName,
config: [true],
Expand Down
18 changes: 16 additions & 2 deletions lib/rules/no-invalid-position-at-import-rule/index.js
Expand Up @@ -4,19 +4,32 @@

const isStandardSyntaxAtRule = require('../../utils/isStandardSyntaxAtRule');
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const { isRegExp, isString } = require('../../utils/validateTypes');

const ruleName = 'no-invalid-position-at-import-rule';

const messages = ruleMessages(ruleName, {
rejected: 'Unexpected invalid position @import rule',
});

function rule(actual) {
function rule(actual, options) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual });
const validOptions = validateOptions(
result,
ruleName,
{ actual },
{
actual: options,
possible: {
ignoreAtRules: [isString, isRegExp],
},
optional: true,
},
);

if (!validOptions) {
return;
Expand All @@ -31,6 +44,7 @@ function rule(actual) {
(node.type === 'atrule' &&
nodeName !== 'charset' &&
nodeName !== 'import' &&
!optionsMatches(options, 'ignoreAtRules', node.name) &&
isStandardSyntaxAtRule(node)) ||
(node.type === 'rule' && isStandardSyntaxRule(node))
) {
Expand Down