Skip to content

Commit

Permalink
fix: warning output (#1173)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Dec 25, 2023
1 parent 8a5e16c commit 0084b93
Show file tree
Hide file tree
Showing 8 changed files with 339 additions and 66 deletions.
14 changes: 10 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ async function getSassOptions(
const needEmitWarning = loaderOptions.warnRuleAsWarning !== false;
const logger = loaderContext.getLogger("sass-loader");
const formatSpan = (span) =>
`${span.url || "-"}:${span.start.line}:${span.start.column}: `;
`Warning on line ${span.start.line}, column ${span.start.column} of ${
span.url || "-"
}:${span.start.line}:${span.start.column}:\n`;
const formatDebugSpan = (span) =>
`[debug:${span.start.line}:${span.start.column}] `;

Expand All @@ -150,13 +152,17 @@ async function getSassOptions(
builtMessage += "Deprecation ";
}

if (loggerOptions.span && !loggerOptions.stack) {
builtMessage = formatSpan(loggerOptions.span);
if (loggerOptions.span) {
builtMessage += formatSpan(loggerOptions.span);
}

builtMessage += message;

if (loggerOptions.stack) {
if (loggerOptions.span && loggerOptions.span.context) {
builtMessage += `\n\n${loggerOptions.span.start.line} | ${loggerOptions.span.context}`;
}

if (loggerOptions.stack && loggerOptions.stack !== "null") {
builtMessage += `\n\n${loggerOptions.stack}`;
}

Expand Down
264 changes: 208 additions & 56 deletions test/__snapshots__/loader.test.js.snap

Large diffs are not rendered by default.

68 changes: 68 additions & 0 deletions test/__snapshots__/warnRuleAsWarning.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,73 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`loader should emit good formatted warning ('dart-sass', 'legacy' API, 'sass' syntax): css 1`] = `""`;

exports[`loader should emit good formatted warning ('dart-sass', 'legacy' API, 'sass' syntax): errors 1`] = `[]`;

exports[`loader should emit good formatted warning ('dart-sass', 'legacy' API, 'sass' syntax): logs 1`] = `[]`;

exports[`loader should emit good formatted warning ('dart-sass', 'legacy' API, 'sass' syntax): warnings 1`] = `
[
"ModuleWarning: Module Warning (from ../src/cjs.js):
Warning on line 5, column 3 of file:///test/sass/broken.sass:5:3:
This selector doesn't have any properties and won't be rendered.
5 | asdf
",
]
`;

exports[`loader should emit good formatted warning ('dart-sass', 'modern' API, 'sass' syntax): css 1`] = `""`;

exports[`loader should emit good formatted warning ('dart-sass', 'modern' API, 'sass' syntax): errors 1`] = `[]`;

exports[`loader should emit good formatted warning ('dart-sass', 'modern' API, 'sass' syntax): logs 1`] = `[]`;

exports[`loader should emit good formatted warning ('dart-sass', 'modern' API, 'sass' syntax): warnings 1`] = `
[
"ModuleWarning: Module Warning (from ../src/cjs.js):
Warning on line 5, column 3 of file:///test/sass/broken.sass:5:3:
This selector doesn't have any properties and won't be rendered.
5 | asdf
",
]
`;

exports[`loader should emit good formatted warning ('sass-embedded', 'legacy' API, 'sass' syntax): css 1`] = `""`;

exports[`loader should emit good formatted warning ('sass-embedded', 'legacy' API, 'sass' syntax): errors 1`] = `[]`;

exports[`loader should emit good formatted warning ('sass-embedded', 'legacy' API, 'sass' syntax): logs 1`] = `[]`;

exports[`loader should emit good formatted warning ('sass-embedded', 'legacy' API, 'sass' syntax): warnings 1`] = `
[
"ModuleWarning: Module Warning (from ../src/cjs.js):
Warning on line 5, column 3 of file:///test/sass/broken.sass:5:3:
This selector doesn't have any properties and won't be rendered.
5 | asdf
",
]
`;

exports[`loader should emit good formatted warning ('sass-embedded', 'modern' API, 'sass' syntax): css 1`] = `""`;

exports[`loader should emit good formatted warning ('sass-embedded', 'modern' API, 'sass' syntax): errors 1`] = `[]`;

exports[`loader should emit good formatted warning ('sass-embedded', 'modern' API, 'sass' syntax): logs 1`] = `[]`;

exports[`loader should emit good formatted warning ('sass-embedded', 'modern' API, 'sass' syntax): warnings 1`] = `
[
"ModuleWarning: Module Warning (from ../src/cjs.js):
Warning on line 5, column 3 of file:///test/sass/broken.sass:5:3:
This selector doesn't have any properties and won't be rendered.
5 | asdf
",
]
`;

exports[`loader should emit warning by default ('dart-sass', 'legacy' API, 'sass' syntax): css 1`] = `
"a {
color: red;
Expand Down
3 changes: 2 additions & 1 deletion test/helpers/getWarnings.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import normalizeErrors from "./normalizeErrors";

export default (stats) => normalizeErrors(stats.compilation.warnings.sort());
export default (stats, needVerbose) =>
normalizeErrors(stats.compilation.warnings.sort(), needVerbose);
10 changes: 7 additions & 3 deletions test/helpers/normalizeErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ function removeCWD(str) {
cwd = cwd.replace(/\\/g, "/");
}

return str.replace(new RegExp(cwd, "g"), "");
return str.replace(new RegExp(cwd, "g"), "").replace("file:////", "file:///");
}

export default (errors) =>
export default (errors, needVerbose) =>
errors.map((error) =>
removeCWD(error.toString().split("\n").slice(0, 2).join("\n"))
removeCWD(
needVerbose
? error.toString()
: error.toString().split("\n").slice(0, 2).join("\n")
)
);
4 changes: 2 additions & 2 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1853,7 +1853,7 @@ describe("loader", () => {

expect(codeFromBundle.css).toBe(codeFromSass.css);
expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getWarnings(stats, true)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});
}
Expand Down Expand Up @@ -2040,7 +2040,7 @@ describe("loader", () => {

expect(codeFromBundle.css).toBe(codeFromSass.css);
expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getWarnings(stats, true)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

Expand Down
6 changes: 6 additions & 0 deletions test/sass/broken.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@





asdf
36 changes: 36 additions & 0 deletions test/warnRuleAsWarning.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,42 @@ describe("loader", () => {
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(logs).toMatchSnapshot("logs");
});

if (syntax === "sass" && implementationName !== "node-sass") {
it(`should emit good formatted warning ('${implementationName}', '${api}' API, '${syntax}' syntax)`, async () => {
const testId = getTestId("broken", syntax);
const options = {
implementation,
api,
};
const compiler = getCompiler(testId, { loader: { options } });
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const logs = [];

for (const [name, value] of stats.compilation.logging) {
if (/sass-loader/.test(name)) {
logs.push(
value.map((i) => {
return {
type: i.type,
args: i.args.map((arg) =>
arg
.replace(url.pathToFileURL(__dirname), "file:///<cwd>")
.replace(/\\/g, "/")
),
};
})
);
}
}

expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats, true)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(logs).toMatchSnapshot("logs");
});
}
});
});
});

0 comments on commit 0084b93

Please sign in to comment.