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

Fix at-import-partial-extension end positions #902

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
52 changes: 39 additions & 13 deletions src/rules/at-import-partial-extension/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ testRule({
@import "fff";
`,
line: 2,
column: 7,
column: 16,
endLine: 2,
endColumn: 19,
message: messages.expected,
description: "Single file, no extension."
},
Expand All @@ -161,7 +163,9 @@ testRule({
"fff.ruthless";
`,
line: 2,
column: 7,
column: 16,
endLine: 2,
endColumn: 19,
message: messages.expected,
description: "Multiple files, one without extension."
},
Expand All @@ -172,12 +176,16 @@ testRule({
warnings: [
{
line: 2,
column: 7,
column: 16,
endLine: 2,
endColumn: 19,
message: messages.expected
},
{
line: 2,
column: 7,
column: 23,
endLine: 2,
endColumn: 28,
message: messages.expected
}
],
Expand Down Expand Up @@ -320,7 +328,9 @@ testRule({
@import "fff";
`,
line: 2,
column: 20,
column: 19,
endLine: 2,
endColumn: 24,
message: messages.rejected("scss"),
description: "Single file, .scss extension."
},
Expand All @@ -332,7 +342,9 @@ testRule({
@import "screen";
`,
line: 2,
column: 23,
column: 22,
endLine: 2,
endColumn: 27,
message: messages.rejected("scss"),
description: "Single file with media query type as name, .scss extension."
},
Expand All @@ -344,7 +356,9 @@ testRule({
@import "fff ";
`,
line: 2,
column: 20,
column: 19,
endLine: 2,
endColumn: 24,
message: messages.rejected("scss"),
description: "Single file, has extension, space at the end."
},
Expand All @@ -356,7 +370,9 @@ testRule({
@import " fff ";
`,
line: 2,
column: 21,
column: 20,
endLine: 2,
endColumn: 25,
message: messages.rejected("scss"),
description: "Single file, has extension, trailing spaces."
},
Expand All @@ -368,7 +384,9 @@ testRule({
@import "df/fff";
`,
line: 2,
column: 23,
column: 22,
endLine: 2,
endColumn: 27,
message: messages.rejected("scss"),
description: "Single file, path with dir, has extension."
},
Expand All @@ -380,7 +398,9 @@ testRule({
@import "df\\fff";
`,
line: 2,
column: 23,
column: 22,
endLine: 2,
endColumn: 27,
message: messages.rejected("scss"),
description:
"Single file, path with dir, has extension, windows delimiters."
Expand All @@ -393,7 +413,9 @@ testRule({
@import "df/fff", '_1';
`,
line: 2,
column: 29,
column: 28,
endLine: 2,
endColumn: 33,
message: messages.rejected("scss"),
description: "Two files, path with dir, has extension."
},
Expand All @@ -405,7 +427,9 @@ testRule({
@import "colors.variables";
`,
line: 2,
column: 33,
column: 32,
endLine: 2,
endColumn: 37,
message: messages.rejected("scss"),
description: "Single file, has .scss extension and a dot in the name."
},
Expand All @@ -417,7 +441,9 @@ testRule({
@import "component.scss-theme";
`,
line: 2,
column: 26,
column: 36,
endLine: 2,
endColumn: 41,
message: messages.rejected("scss"),
description:
"Single file, has .scss extension and a .scss in the filename."
Expand Down
21 changes: 14 additions & 7 deletions src/rules/at-import-partial-extension/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const nodeJsPath = require("path");
const { utils } = require("stylelint");
const atRuleParamIndex = require("../../utils/atRuleParamIndex");
const namespace = require("../../utils/namespace");
const ruleUrl = require("../../utils/ruleUrl");

Expand Down Expand Up @@ -46,8 +47,8 @@ function rule(expectation, _, context) {
return;
}

root.walkAtRules("import", decl => {
const paths = decl.params
root.walkAtRules("import", atRule => {
const paths = atRule.params
.split(/["']\s*,/)
.filter(path => !mediaQueryTypesRE.test(path.trim()));

Expand All @@ -70,9 +71,10 @@ function rule(expectation, _, context) {
if (!extension && expectation === "always") {
utils.report({
message: messages.expected,
node: decl,
node: atRule,
result,
ruleName
ruleName,
word: pathStripped
});

return;
Expand All @@ -82,15 +84,20 @@ function rule(expectation, _, context) {
if (extension && isScssPartial && expectation === "never") {
if (context.fix) {
const extPattern = new RegExp(`\\.${extension}(['" ]*)$`, "g");
decl.params = decl.params.replace(extPattern, "$1");
atRule.params = atRule.params.replace(extPattern, "$1");

return;
}

const dotExt = `.${extension}`;
const index =
atRuleParamIndex(atRule) + atRule.params.lastIndexOf(dotExt);

utils.report({
message: messages.rejected(extension),
node: decl,
word: extension,
node: atRule,
index,
endIndex: index + dotExt.length,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

[note] I think the end position should include a dot (.) since the message also includes it:

Unexpected extension ".scss" in @import

result,
ruleName
});
Expand Down