Skip to content

Commit

Permalink
Fix handling of shared-line comments
Browse files Browse the repository at this point in the history
Introduces a number of utilities and adjusts the following rules:
at-rule-empty-line-before, custom-property-empty-line-before,
declaration-empty-line-before, rule-nested-empty-line-before,
rule-non-nested-empty-line-before

Closes #2237; closes #2239; closes #2240.
  • Loading branch information
David Clark committed Jan 15, 2017
1 parent 6eaa0a2 commit 4b3c213
Show file tree
Hide file tree
Showing 32 changed files with 1,159 additions and 142 deletions.
56 changes: 38 additions & 18 deletions decls/postcss.js
@@ -1,5 +1,10 @@
export type postcss$comment = {
text: string,
declare class postcss$node {
raw: Function,
type: 'rule' | 'atrule' | 'root' | 'comment' | 'decl';
parent: Object;
nodes: Array<Object>;
next(): postcss$node | void;
prev(): postcss$node | void;
source: {
start: {
line: number,
Expand All @@ -9,26 +14,41 @@ export type postcss$comment = {
line: number,
column: number,
},
},
};
error(message: string, options: { plugin: string }): void,
}

export type postcss$atRule = {
name: string,
params: string,
raw: Function,
declare class postcss$comment extends postcss$node {
text: string;
raws: {
before?: string,
after?: string,
};
}

declare class postcss$atRule extends postcss$node {
name: string;
params: string;
raws: {
afterName: string,
},
type: string,
parent: Object,
nodes: Array<Object>
before?: string,
after?: string,
afterName?: string,
};
}

export type postcss$rule = {
raws: Object,
selector: string,
type: string,
parent: Object,
nodes: Array<Object>,
declare class postcss$rule extends postcss$node {
selector: string;
raws: {
before?: string,
after?: string,
};
}

declare class postcss$decl extends postcss$node {
prop: string;
value: string;
raws: {
before?: string,
after?: string,
};
}
54 changes: 31 additions & 23 deletions jest-setup.js
Expand Up @@ -21,13 +21,17 @@ global.testRule = (rule, schema) => {
describe("accept", () => {
passingTestCases.forEach((testCase) => {
const spec = (testCase.only) ? it.only : it
spec(testCase.description || "no description", () => {
return stylelint({
code: testCase.code,
config: stylelintConfig,
syntax: schema.syntax,
}).then((output) => {
expect(output.results[0].warnings).toEqual([])
describe(JSON.stringify(schema.config), () => {
describe(JSON.stringify(testCase.code), () => {
spec(testCase.description || "no description", () => {
return stylelint({
code: testCase.code,
config: stylelintConfig,
syntax: schema.syntax,
}).then((output) => {
expect(output.results[0].warnings).toEqual([])
})
})
})
})
})
Expand All @@ -38,22 +42,26 @@ global.testRule = (rule, schema) => {
describe("reject", () => {
schema.reject.forEach((testCase) => {
const spec = (testCase.only) ? it.only : it
spec(testCase.description || "no description", () => {
return stylelint({
code: testCase.code,
config: stylelintConfig,
syntax: schema.syntax,
}).then((output) => {
const warning = output.results[0].warnings[0]
if (testCase.message) {
expect(_.get(warning, "text")).toBe(testCase.message)
}
if (testCase.line) {
expect(_.get(warning, "line")).toBe(testCase.line)
}
if (testCase.column) {
expect(_.get(warning, "column")).toBe(testCase.column)
}
describe(JSON.stringify(schema.config), () => {
describe(JSON.stringify(testCase.code), () => {
spec(testCase.description || "no description", () => {
return stylelint({
code: testCase.code,
config: stylelintConfig,
syntax: schema.syntax,
}).then((output) => {
const warning = output.results[0].warnings[0]
if (testCase.message) {
expect(_.get(warning, "text")).toBe(testCase.message)
}
if (testCase.line) {
expect(_.get(warning, "line")).toBe(testCase.line)
}
if (testCase.column) {
expect(_.get(warning, "column")).toBe(testCase.column)
}
})
})
})
})
})
Expand Down
26 changes: 26 additions & 0 deletions lib/rules/at-rule-empty-line-before/README.md
Expand Up @@ -108,6 +108,8 @@ Reverse the primary option for blockless at-rules that follow another blockless

This means that you can group your blockless at-rules by name.

Shared-line comments do not affect this option.

For example, with `"always"`:

The following patterns are *not* considered warnings:
Expand All @@ -119,6 +121,13 @@ The following patterns are *not* considered warnings:
@import url(y.css);
```

```css
@charset "UTF-8";

@import url(x.css); /* comment */
@import url(y.css);
```

```css
a {

Expand All @@ -134,6 +143,8 @@ a {

Reverse the primary option for at-rules within a blockless group.

Shared-line comments do not affect this option.

For example, with `"always"`:

The following patterns are considered warnings:
Expand All @@ -155,6 +166,13 @@ The following patterns are *not* considered warnings:
@media print {}
```

```css
@import url(x.css); /* comment */
@import url(y.css);

@media print {}
```

#### `"first-nested"`

Reverse the primary option for at-rules that are nested and the first child of their parent node.
Expand Down Expand Up @@ -197,6 +215,8 @@ b {

Ignore at-rules that come after a comment.

Shared-line comments do not trigger this option.

The following patterns are *not* considered warnings:

```css
Expand All @@ -210,6 +230,12 @@ The following patterns are *not* considered warnings:
@media {}
```

```css
@media {} /* comment */

@media {}
```

#### `"all-nested"`

Ignore at-rules that are nested.
Expand Down
81 changes: 65 additions & 16 deletions lib/rules/at-rule-empty-line-before/__tests__/index.js
Expand Up @@ -123,6 +123,12 @@ testRule(rule, mergeTestDescriptions(sharedAlwaysTests, {
}, {
code: "@import 'x.css';",
description: "single blockless rule",
}, {
code: stripIndent`
@charset "UTF-8";
@import url(x.css); /* comment */
@import url(y.css);`,
description: "shared-line comment accepted",
} ],

reject: [ {
Expand All @@ -138,13 +144,27 @@ testRule(rule, {
ruleName,
config: [ "always", { ignore: ["blockless-group"] } ],

accept: [{
code: "@media {}; @import 'x.css';",
}],
accept: [ {
code: "@import 'y.css'; @import 'x.css';",
}, {
code: stripIndent`
@charset "UTF-8";
@import url(x.css); /* comment */
@import url(y.css);`,
description: "shared-line comment accepted",
} ],

reject: [ {
code: "@import 'x.css'; @media {};",
message: messages.expected,
line: 1,
column: 18,
}, {
code: "@media {}; @import 'x.css';",
message: messages.expected,
line: 1,
column: 12,
}, {
code: "@import 'test'; @include mixin(1) { @content; };",
message: messages.expected,
Expand All @@ -164,10 +184,16 @@ testRule(rule, {
description: "CRLF",
} ],

reject: [{
reject: [ {
code: "a {} @media {}",
message: messages.expected,
}],
}, {
code: "bar {} /* foo */\n@media {}",
message: messages.expected,
line: 2,
column: 1,
description: "after shared-line comment",
} ],
})

testRule(rule, mergeTestDescriptions(sharedAlwaysTests, {
Expand Down Expand Up @@ -332,21 +358,15 @@ testRule(rule, {
ruleName,
config: [ "never", { ignore: ["blockless-group"] } ],

accept: [ {
code: `
@media {};
@import 'x.css';
`,
}, {
accept: [{
code: `
@import 'x.css';
@import 'y.css';
`,
} ],
}],

reject: [{
reject: [ {
code: `
@import 'x.css';
Expand All @@ -355,7 +375,16 @@ testRule(rule, {
message: messages.rejected,
line: 4,
column: 7,
}],
}, {
code: `
@media {};
@import 'x.css';
`,
message: messages.rejected,
line: 4,
column: 7,
} ],
})

testRule(rule, {
Expand All @@ -378,6 +407,12 @@ testRule(rule, {
code: "b {}\r\n\r\n@media {}",
description: "CRLF",
message: messages.rejected,
}, {
code: "b {} /* comment */\n\n@media {}",
description: "after shared-line comment",
message: messages.rejected,
line: 3,
column: 1,
} ],
})

Expand Down Expand Up @@ -578,6 +613,13 @@ testRule(rule, mergeTestDescriptions(sharedAlwaysTests, {
@include loop;
@include doo;
}`,
}, {
code: stripIndent`
@charset "UTF-8";
@import url(x.css); /* comment */
@import url(y.css);`,
description: "shared-line comment accepted",
} ],

reject: [ {
Expand Down Expand Up @@ -625,6 +667,13 @@ testRule(rule, mergeTestDescriptions(sharedAlwaysTests, {
@include loop;
@include doo;
}`,
}, {
code: stripIndent`
@charset "UTF-8";
@import url(x.css); /* comment */
@import url(y.css);`,
description: "shared-line comment accepted",
} ],

reject: [ {
Expand Down Expand Up @@ -660,7 +709,7 @@ testRule(rule, mergeTestDescriptions(sharedNeverTests, {
code: stripIndent`
@charset "UTF-8";
@import url(x.css);
@import url(y.css);`,
}, {
code: stripIndent`
Expand Down

0 comments on commit 4b3c213

Please sign in to comment.