Skip to content

Commit bc3b848

Browse files
chore: migrate on defaults eslint config (#643)
1 parent a80cdb1 commit bc3b848

22 files changed

+489
-128
lines changed

.eslintrc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
root: true,
3+
extends: ['@webpack-contrib/eslint-config-webpack'],
4+
rules: {
5+
// Remove strict rule in next major
6+
"strict": "off",
7+
},
8+
};

.eslintrc.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

.nycrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"lines": 97,
1010
"statements": 97,
1111
"functions": 100,
12-
"branches": 91,
12+
"branches": 90,
1313
"check-coverage": true
1414
}

lib/formatSassError.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ const os = require("os");
55
const fs = require("fs");
66

77
// A typical sass error looks like this
8-
const SassError = { // eslint-disable-line no-unused-vars
9-
message: "invalid property name",
10-
column: 14,
11-
line: 1,
12-
file: "stdin",
13-
status: 1
14-
};
8+
// const SassError = {
9+
// message: "invalid property name",
10+
// column: 14,
11+
// line: 1,
12+
// file: "stdin",
13+
// status: 1
14+
// };
1515

1616
/**
1717
* Enhances the sass error with additional information about what actually went wrong.
@@ -22,6 +22,7 @@ const SassError = { // eslint-disable-line no-unused-vars
2222
function formatSassError(err, resourcePath) {
2323
// Instruct webpack to hide the JS stack from the console
2424
// Usually you're only interested in the SASS stack in this case.
25+
// eslint-disable-next-line no-param-reassign
2526
err.hideStack = true;
2627

2728
// The file property is missing in rare cases.
@@ -33,18 +34,22 @@ function formatSassError(err, resourcePath) {
3334
let msg = err.message;
3435

3536
if (err.file === "stdin") {
37+
// eslint-disable-next-line no-param-reassign
3638
err.file = resourcePath;
3739
}
40+
3841
// node-sass returns UNIX-style paths
42+
// eslint-disable-next-line no-param-reassign
3943
err.file = path.normalize(err.file);
4044

4145
// The 'Current dir' hint of node-sass does not help us, we're providing
4246
// additional information by reading the err.file property
4347
msg = msg.replace(/\s*Current dir:\s*/, "");
4448

45-
err.message = getFileExcerptIfPossible(err) +
46-
msg.charAt(0).toUpperCase() + msg.slice(1) + os.EOL +
47-
" in " + err.file + " (line " + err.line + ", column " + err.column + ")";
49+
// eslint-disable-next-line no-param-reassign
50+
err.message = `${getFileExcerptIfPossible(err) +
51+
msg.charAt(0).toUpperCase() + msg.slice(1) + os.EOL
52+
} in ${ err.file } (line ${ err.line }, column ${ err.column })`;
4853
}
4954

5055
/**
@@ -60,11 +65,11 @@ function getFileExcerptIfPossible(err) {
6065
try {
6166
const content = fs.readFileSync(err.file, "utf8");
6267

63-
return os.EOL +
68+
return `${os.EOL +
6469
content.split(os.EOL)[err.line - 1] + os.EOL +
65-
new Array(err.column - 1).join(" ") + "^" + os.EOL +
66-
" ";
67-
} catch (err) {
70+
new Array(err.column - 1).join(" ") }^${ os.EOL
71+
} `;
72+
} catch (ignoreErr) {
6873
// If anything goes wrong here, we don't want any errors to be reported to the user
6974
return "";
7075
}

lib/importsToResolve.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"use strict";
22

33
const path = require("path");
4+
45
const utils = require("loader-utils");
56

6-
const matchModuleImport = /^~([^\/]+|@[^\/]+[\/][^\/]+)$/;
7+
const matchModuleImport = /^~([^/]+|@[^/]+[/][^/]+)$/;
78

89
/**
910
* When libsass tries to resolve an import, it uses a special algorithm.

lib/loader.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
"use strict";
22

33
const path = require("path");
4+
45
const async = require("neo-async");
6+
7+
const pify = require("pify");
8+
9+
const semver = require("semver");
10+
511
const formatSassError = require("./formatSassError");
612
const webpackImporter = require("./webpackImporter");
713
const normalizeOptions = require("./normalizeOptions");
8-
const pify = require("pify");
9-
const semver = require("semver");
1014

1115
let nodeSassJobQueue = null;
1216

@@ -20,7 +24,7 @@ function sassLoader(content) {
2024
const callback = this.async();
2125
const isSync = typeof callback !== "function";
2226
const self = this;
23-
const resourcePath = this.resourcePath;
27+
const { resourcePath } = this;
2428

2529
function addNormalizedDependency(file) {
2630
// node-sass returns POSIX paths
@@ -43,36 +47,48 @@ function sassLoader(content) {
4347
return;
4448
}
4549

50+
// eslint-disable-next-line import/no-extraneous-dependencies, global-require
4651
const render = getRenderFuncFromSassImpl(options.implementation || require("node-sass"));
4752

4853
render(options, (err, result) => {
4954
if (err) {
5055
formatSassError(err, this.resourcePath);
51-
err.file && this.dependency(err.file);
56+
57+
if (err.file) {
58+
this.dependency(err.file);
59+
}
60+
5261
callback(err);
5362
return;
5463
}
5564

5665
if (result.map && result.map !== "{}") {
66+
// eslint-disable-next-line no-param-reassign
5767
result.map = JSON.parse(result.map);
5868
// result.map.file is an optional property that provides the output filename.
5969
// Since we don't know the final filename in the webpack build chain yet, it makes no sense to have it.
70+
// eslint-disable-next-line no-param-reassign
6071
delete result.map.file;
6172
// The first source is 'stdin' according to node-sass because we've used the data input.
6273
// Now let's override that value with the correct relative path.
6374
// Since we specified options.sourceMap = path.join(process.cwd(), "/sass.map"); in normalizeOptions,
6475
// we know that this path is relative to process.cwd(). This is how node-sass works.
76+
// eslint-disable-next-line no-param-reassign
6577
result.map.sources[0] = path.relative(process.cwd(), resourcePath);
6678
// node-sass returns POSIX paths, that's why we need to transform them back to native paths.
6779
// This fixes an error on windows where the source-map module cannot resolve the source maps.
6880
// @see https://github.com/webpack-contrib/sass-loader/issues/366#issuecomment-279460722
81+
// eslint-disable-next-line no-param-reassign
6982
result.map.sourceRoot = path.normalize(result.map.sourceRoot);
83+
// eslint-disable-next-line no-param-reassign
7084
result.map.sources = result.map.sources.map(path.normalize);
7185
} else {
86+
// eslint-disable-next-line no-param-reassign
7287
result.map = null;
7388
}
7489

7590
result.stats.includedFiles.forEach(addNormalizedDependency);
91+
7692
callback(null, result.css.toString(), result.map);
7793
});
7894
}
@@ -84,29 +100,30 @@ function sassLoader(content) {
84100
* @returns {Function}
85101
*/
86102
function getRenderFuncFromSassImpl(module) {
87-
const info = module.info;
103+
const { info } = module;
88104
const components = info.split("\t");
89105

90106
if (components.length < 2) {
91-
throw new Error("Unknown Sass implementation \"" + info + "\".");
107+
throw new Error(`Unknown Sass implementation "${ info }".`);
92108
}
93109

94-
const implementation = components[0];
95-
const version = components[1];
110+
const [implementation, version] = components;
96111

97112
if (!semver.valid(version)) {
98-
throw new Error("Invalid Sass version \"" + version + "\".");
113+
throw new Error(`Invalid Sass version "${ version }".`);
99114
}
100115

101116
if (implementation === "dart-sass") {
102117
if (!semver.satisfies(version, "^1.3.0")) {
103-
throw new Error("Dart Sass version " + version + " is incompatible with ^1.3.0.");
118+
throw new Error(`Dart Sass version ${ version } is incompatible with ^1.3.0.`);
104119
}
120+
105121
return module.render.bind(module);
106122
} else if (implementation === "node-sass") {
107123
if (!semver.satisfies(version, "^4.0.0")) {
108-
throw new Error("Node Sass version " + version + " is incompatible with ^4.0.0.");
124+
throw new Error(`Node Sass version ${ version } is incompatible with ^4.0.0.`);
109125
}
126+
110127
// There is an issue with node-sass when async custom importers are used
111128
// See https://github.com/sass/node-sass/issues/857#issuecomment-93594360
112129
// We need to use a job queue to make sure that one thread is always available to the UV lib
@@ -118,7 +135,8 @@ function getRenderFuncFromSassImpl(module) {
118135

119136
return nodeSassJobQueue.push.bind(nodeSassJobQueue);
120137
}
121-
throw new Error("Unknown Sass implementation \"" + implementation + "\".");
138+
139+
throw new Error(`Unknown Sass implementation "${ implementation }".`);
122140
}
123141

124142
module.exports = sassLoader;

lib/normalizeOptions.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"use strict";
22

33
const os = require("os");
4+
const path = require("path");
5+
46
const utils = require("loader-utils");
57
const cloneDeep = require("clone-deep");
6-
const path = require("path");
8+
79
const proxyCustomImporters = require("./proxyCustomImporters");
810

911
/**
@@ -19,7 +21,7 @@ const proxyCustomImporters = require("./proxyCustomImporters");
1921
*/
2022
function normalizeOptions(loaderContext, content, webpackImporter) {
2123
const options = cloneDeep(utils.getOptions(loaderContext)) || {};
22-
const resourcePath = loaderContext.resourcePath;
24+
const { resourcePath } = loaderContext;
2325

2426
options.data = options.data ? (options.data + os.EOL + content) : content;
2527

lib/proxyCustomImporters.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
* @returns {Array<Function>}
1616
*/
1717
function proxyCustomImporters(importer, resourcePath) {
18-
return [].concat(importer).map((importer) => {
19-
return function (url, prev, done) {
18+
return [].concat(importer).map(
19+
// eslint-disable-next-line no-shadow
20+
(importer) => function customImporter() {
2021
return importer.apply(
21-
this, // eslint-disable-line no-invalid-this
22+
this,
23+
// eslint-disable-next-line prefer-rest-params
2224
Array.from(arguments)
2325
.map((arg, i) => i === 1 && arg === "stdin" ? resourcePath : arg)
2426
);
25-
};
26-
});
27+
});
2728
}
2829

2930
module.exports = proxyCustomImporters;

lib/webpackImporter.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
*/
1818

1919
const path = require("path");
20+
2021
const tail = require("lodash.tail");
22+
2123
const importsToResolve = require("./importsToResolve");
2224

2325
const matchCss = /\.css$/;
@@ -41,6 +43,7 @@ function webpackImporter(resourcePath, resolve, addNormalizedDependency) {
4143
);
4244
}
4345

46+
// eslint-disable-next-line no-shadow
4447
function startResolving(dir, importsToResolve) {
4548
return importsToResolve.length === 0 ?
4649
Promise.reject() :
@@ -63,8 +66,9 @@ function webpackImporter(resourcePath, resolve, addNormalizedDependency) {
6366
startResolving(
6467
dirContextFrom(prev),
6568
importsToResolve(url)
66-
) // Catch all resolving errors, return the original file and pass responsibility back to other custom importers
67-
.catch(() => ({ file: url }))
69+
)
70+
// Catch all resolving errors, return the original file and pass responsibility back to other custom importers
71+
.catch(() => { return { file: url } })
6872
.then(done);
6973
};
7074
}

0 commit comments

Comments
 (0)