Skip to content

Commit

Permalink
style: use prettier (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Dec 13, 2018
1 parent bc3b848 commit 2adcca3
Show file tree
Hide file tree
Showing 29 changed files with 1,219 additions and 978 deletions.
18 changes: 8 additions & 10 deletions .editorconfig
@@ -1,15 +1,13 @@
# This file is for unifying the coding style for different editors and IDEs.
# More information at http://EditorConfig.org

# No .editorconfig files above the root directory
root = true
# editorconfig.org

[*]
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[package.json]
indent_size = 2
[*.md]
insert_final_newline = true
trim_trailing_whitespace = false
2 changes: 1 addition & 1 deletion .eslintignore
@@ -1,5 +1,5 @@
/node_modules
# Compiled by webpack
test/output

# Fake node_modules folder for tests
test/node_modules
17 changes: 11 additions & 6 deletions .eslintrc.js
@@ -1,8 +1,13 @@
module.exports = {
root: true,
extends: ['@webpack-contrib/eslint-config-webpack'],
rules: {
// Remove strict rule in next major
"strict": "off",
},
root: true,
plugins: ['prettier'],
extends: ['@webpack-contrib/eslint-config-webpack'],
rules: {
// Remove strict rule in next major
strict: 'off',
'prettier/prettier': [
'error',
{ singleQuote: true, trailingComma: 'es5', arrowParens: 'always' },
],
},
};
3 changes: 3 additions & 0 deletions .gitattributes
@@ -0,0 +1,3 @@
package-lock.json -diff
* text=auto
bin/* eol=lf
27 changes: 14 additions & 13 deletions .gitignore
@@ -1,19 +1,20 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results
*.log
npm-debug.log*
.eslintcache

npm-debug.log
/coverage
/dist
/local
/reports
/node_modules
coverage

.DS_Store
Thumbs.db
.idea
*.iml
.vscode
*.sublime-project
*.sublime-workspace
.nyc_output
test/output
5 changes: 5 additions & 0 deletions .prettierrc
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"trailingComma": "es5",
"arrowParens": "always"
}
77 changes: 39 additions & 38 deletions lib/formatSassError.js
@@ -1,8 +1,8 @@
"use strict";
'use strict';

const path = require("path");
const os = require("os");
const fs = require("fs");
const path = require('path');
const os = require('os');
const fs = require('fs');

// A typical sass error looks like this
// const SassError = {
Expand All @@ -20,36 +20,37 @@ const fs = require("fs");
* @param {string} resourcePath
*/
function formatSassError(err, resourcePath) {
// Instruct webpack to hide the JS stack from the console
// Usually you're only interested in the SASS stack in this case.
// eslint-disable-next-line no-param-reassign
err.hideStack = true;

// The file property is missing in rare cases.
// No improvement in the error is possible.
if (!err.file) {
return;
}
// Instruct webpack to hide the JS stack from the console
// Usually you're only interested in the SASS stack in this case.
// eslint-disable-next-line no-param-reassign
err.hideStack = true;

let msg = err.message;
// The file property is missing in rare cases.
// No improvement in the error is possible.
if (!err.file) {
return;
}

if (err.file === "stdin") {
// eslint-disable-next-line no-param-reassign
err.file = resourcePath;
}
let msg = err.message;

// node-sass returns UNIX-style paths
if (err.file === 'stdin') {
// eslint-disable-next-line no-param-reassign
err.file = path.normalize(err.file);
err.file = resourcePath;
}

// The 'Current dir' hint of node-sass does not help us, we're providing
// additional information by reading the err.file property
msg = msg.replace(/\s*Current dir:\s*/, "");
// node-sass returns UNIX-style paths
// eslint-disable-next-line no-param-reassign
err.file = path.normalize(err.file);

// eslint-disable-next-line no-param-reassign
err.message = `${getFileExcerptIfPossible(err) +
msg.charAt(0).toUpperCase() + msg.slice(1) + os.EOL
} in ${ err.file } (line ${ err.line }, column ${ err.column })`;
// The 'Current dir' hint of node-sass does not help us, we're providing
// additional information by reading the err.file property
msg = msg.replace(/\s*Current dir:\s*/, '');

// eslint-disable-next-line no-param-reassign
err.message = `${getFileExcerptIfPossible(err) +
msg.charAt(0).toUpperCase() +
msg.slice(1) +
os.EOL} in ${err.file} (line ${err.line}, column ${err.column})`;
}

/**
Expand All @@ -62,17 +63,17 @@ function formatSassError(err, resourcePath) {
* @returns {string}
*/
function getFileExcerptIfPossible(err) {
try {
const content = fs.readFileSync(err.file, "utf8");
try {
const content = fs.readFileSync(err.file, 'utf8');

return `${os.EOL +
content.split(os.EOL)[err.line - 1] + os.EOL +
new Array(err.column - 1).join(" ") }^${ os.EOL
} `;
} catch (ignoreErr) {
// If anything goes wrong here, we don't want any errors to be reported to the user
return "";
}
return `${os.EOL +
content.split(os.EOL)[err.line - 1] +
os.EOL +
new Array(err.column - 1).join(' ')}^${os.EOL} `;
} catch (ignoreErr) {
// If anything goes wrong here, we don't want any errors to be reported to the user
return '';
}
}

module.exports = formatSassError;
87 changes: 44 additions & 43 deletions lib/importsToResolve.js
@@ -1,8 +1,8 @@
"use strict";
'use strict';

const path = require("path");
const path = require('path');

const utils = require("loader-utils");
const utils = require('loader-utils');

const matchModuleImport = /^~([^/]+|@[^/]+[/][^/]+)$/;

Expand All @@ -16,46 +16,47 @@ const matchModuleImport = /^~([^/]+|@[^/]+[/][^/]+)$/;
* @returns {Array<string>}
*/
function importsToResolve(url) {
const request = utils.urlToRequest(url);
// Keep in mind: ext can also be something like '.datepicker' when the true extension is omitted and the filename contains a dot.
// @see https://github.com/webpack-contrib/sass-loader/issues/167
const ext = path.extname(request);

if (matchModuleImport.test(url)) {
return [request, url];
}

// libsass' import algorithm works like this:

// In case there is a file extension...
// - If the file is a CSS-file, do not include it all, but just link it via @import url().
// - The exact file name must match (no auto-resolving of '_'-modules).
if (ext === ".css") {
return [];
}
if (ext === ".scss" || ext === ".sass") {
return [request, url];
}

// In case there is no file extension...
// - Prefer modules starting with '_'.
// - File extension precedence: .scss, .sass, .css.
const basename = path.basename(request);

if (basename.charAt(0) === "_") {
return [
`${ request }.scss`, `${ request }.sass`, `${ request }.css`,
url
];
}

const dirname = path.dirname(request);

return [
`${ dirname }/_${ basename }.scss`, `${ dirname }/_${ basename }.sass`, `${ dirname }/_${ basename }.css`,
`${ request }.scss`, `${ request }.sass`, `${ request }.css`,
url
];
const request = utils.urlToRequest(url);
// Keep in mind: ext can also be something like '.datepicker' when the true extension is omitted and the filename contains a dot.
// @see https://github.com/webpack-contrib/sass-loader/issues/167
const ext = path.extname(request);

if (matchModuleImport.test(url)) {
return [request, url];
}

// libsass' import algorithm works like this:

// In case there is a file extension...
// - If the file is a CSS-file, do not include it all, but just link it via @import url().
// - The exact file name must match (no auto-resolving of '_'-modules).
if (ext === '.css') {
return [];
}
if (ext === '.scss' || ext === '.sass') {
return [request, url];
}

// In case there is no file extension...
// - Prefer modules starting with '_'.
// - File extension precedence: .scss, .sass, .css.
const basename = path.basename(request);

if (basename.charAt(0) === '_') {
return [`${request}.scss`, `${request}.sass`, `${request}.css`, url];
}

const dirname = path.dirname(request);

return [
`${dirname}/_${basename}.scss`,
`${dirname}/_${basename}.sass`,
`${dirname}/_${basename}.css`,
`${request}.scss`,
`${request}.sass`,
`${request}.css`,
url,
];
}

module.exports = importsToResolve;

0 comments on commit 2adcca3

Please sign in to comment.