Skip to content

Commit 7656eff

Browse files
committedMar 24, 2018
Adopt airbnb eslint preset
1 parent 1293169 commit 7656eff

File tree

5 files changed

+349
-552
lines changed

5 files changed

+349
-552
lines changed
 

‎.eslintrc

+3-153
Original file line numberDiff line numberDiff line change
@@ -4,159 +4,9 @@ env:
44
mocha: true
55
node: true
66

7-
# globals:
8-
#########################
9-
## Only add globals if you're absolutely certain they need to be globals
10-
##########################
11-
# console: true
12-
13-
#########################
14-
## set to 0 to allow
15-
## set to 1 to disallow as warning
16-
## set to 2 to disallow as error
17-
#########################
187
rules:
19-
#########################
20-
## Optional Rules
21-
#########################
22-
# Disallow use of `console`
23-
no-console: 2
24-
25-
# Disallow warning comments
26-
no-warning-comments:
27-
- 1
28-
- terms:
29-
- todo
30-
- fixme
31-
location: anywhere
32-
33-
# Warns when variables are defined but never used
34-
no-unused-vars: 1
35-
36-
# Enforces comma style (first or last)
37-
comma-style:
38-
- 2
39-
- last
40-
41-
# Enforces one true `this` variable
42-
consistent-this:
8+
max-len:
439
- 2
44-
- self
45-
# Allows dangling underscores in identifiers
46-
no-underscore-dangle: 2
47-
48-
# Enforces function expressions to have a name
49-
func-names: 0
50-
51-
# Set maximum depth of nested callbacks
52-
max-nested-callbacks:
53-
- 1
54-
- 3
55-
56-
#########################
57-
## Core Rules
58-
##########################
59-
# Enforces camel case names
60-
camelcase: 2
61-
62-
# Prohibit use of == and != in favor of === and !==
63-
eqeqeq: 2
64-
65-
# Suppresses warnings about == null comparisons
66-
no-eq-null: 2
67-
68-
# No mixing tabs and spaces, with 2 spaces only
69-
no-mixed-spaces-and-tabs: 2
70-
71-
# Prohibits use of a variable before it is defined
72-
no-use-before-define: 2
73-
74-
# Requires capitalized names for constructor functions
75-
new-cap: 2
76-
77-
# Prohibits use of explicitly undeclared variables
78-
no-undef: 2
79-
80-
# Enforces Use Strict at the top of function scope
81-
strict:
82-
- 2
83-
- global
84-
85-
# Requires variable declarations to be at the top
86-
vars-on-top: 2
87-
88-
# Enforce curly braces around blocks in loops and conditionals
89-
curly: 2
90-
91-
# Prohibits the use of immediate function invocations w/o wrapping in parentheses
92-
wrap-iife: 2
93-
94-
# Prohibits `argument.caller` and `argument.callee`
95-
no-caller: 2
96-
97-
# Requires all `for in` loops to filter object's items
98-
guard-for-in: 2
99-
100-
# Prohibits comparing a variable against itself
101-
no-self-compare: 2
10+
- 120
10211

103-
# Prohibits use of `undefined` variable
104-
no-undefined: 0
105-
106-
# Prohibits nested ternaries
107-
no-nested-ternary: 2
108-
109-
# Enforces a space before blocks
110-
space-before-blocks:
111-
- 2
112-
- always
113-
114-
# Enforces spaces following keywords
115-
keyword-spacing:
116-
- 2
117-
- after: true
118-
119-
# Enforces quoted property names
120-
quote-props:
121-
- 2
122-
- always
123-
124-
# Enforces padded blocks
125-
padded-blocks:
126-
- 1
127-
- never
128-
129-
# Enforce functions as expressions
130-
func-style:
131-
- 2
132-
- expression
133-
134-
# Require brace style
135-
brace-style:
136-
- 2
137-
- stroustrup
138-
139-
# Prohibits Yoda conditions
140-
yoda:
141-
- 2
142-
- never
143-
144-
# Enforce use of single quotation marks for strings.
145-
quotes:
146-
- 2
147-
- single
148-
149-
# Disallow or enforce spaces inside of curly braces in objects.
150-
object-curly-spacing:
151-
- 2
152-
- always
153-
154-
# Disallow or enforce spaces inside of brackets.
155-
array-bracket-spacing:
156-
- 2
157-
- never
158-
159-
# Disallow or enforce spaces inside of computed properties.
160-
computed-property-spacing:
161-
- 2
162-
- never
12+
extends: "airbnb-base"

‎index.js

+125-153
Original file line numberDiff line numberDiff line change
@@ -1,186 +1,158 @@
1-
'use strict';
1+
const chalk = require('chalk');
2+
const PluginError = require('plugin-error');
3+
const replaceExtension = require('replace-ext');
4+
const stripAnsi = require('strip-ansi');
5+
const through = require('through2');
6+
const clonedeep = require('lodash.clonedeep');
7+
const path = require('path');
8+
const applySourceMap = require('vinyl-sourcemaps-apply');
29

3-
var chalk = require('chalk');
4-
var PluginError = require('plugin-error');
5-
var replaceExtension = require('replace-ext');
6-
var stripAnsi = require('strip-ansi');
7-
var through = require('through2');
8-
var clonedeep = require('lodash.clonedeep');
9-
var path = require('path');
10-
var applySourceMap = require('vinyl-sourcemaps-apply');
11-
12-
var PLUGIN_NAME = 'gulp-sass';
10+
const PLUGIN_NAME = 'gulp-sass';
1311

1412
//////////////////////////////
1513
// Main Gulp Sass function
1614
//////////////////////////////
17-
var gulpSass = function gulpSass(options, sync) {
18-
return through.obj(function(file, enc, cb) {
19-
var opts,
20-
filePush,
21-
errorM,
22-
callback,
23-
result;
24-
25-
if (file.isNull()) {
26-
return cb(null, file);
27-
}
28-
if (file.isStream()) {
29-
return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
30-
}
31-
if (path.basename(file.path).indexOf('_') === 0) {
32-
return cb();
33-
}
34-
if (!file.contents.length) {
35-
file.path = replaceExtension(file.path, '.css');
36-
return cb(null, file);
15+
const gulpSass = (options, sync) => through.obj((file, enc, cb) => { // eslint-disable-line consistent-return
16+
if (file.isNull()) {
17+
return cb(null, file);
18+
}
19+
20+
if (file.isStream()) {
21+
return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
22+
}
23+
24+
if (path.basename(file.path).indexOf('_') === 0) {
25+
return cb();
26+
}
27+
28+
if (!file.contents.length) {
29+
file.path = replaceExtension(file.path, '.css'); // eslint-disable-line no-param-reassign
30+
return cb(null, file);
31+
}
32+
33+
const opts = clonedeep(options || {});
34+
opts.data = file.contents.toString();
35+
36+
// we set the file path here so that libsass can correctly resolve import paths
37+
opts.file = file.path;
38+
39+
// Ensure `indentedSyntax` is true if a `.sass` file
40+
if (path.extname(file.path) === '.sass') {
41+
opts.indentedSyntax = true;
42+
}
43+
44+
// Ensure file's parent directory in the include path
45+
if (opts.includePaths) {
46+
if (typeof opts.includePaths === 'string') {
47+
opts.includePaths = [opts.includePaths];
3748
}
49+
} else {
50+
opts.includePaths = [];
51+
}
52+
53+
opts.includePaths.unshift(path.dirname(file.path));
54+
55+
// Generate Source Maps if plugin source-map present
56+
if (file.sourceMap) {
57+
opts.sourceMap = file.path;
58+
opts.omitSourceMapUrl = true;
59+
opts.sourceMapContents = true;
60+
}
61+
62+
//////////////////////////////
63+
// Handles returning the file to the stream
64+
//////////////////////////////
65+
const filePush = (sassObj) => {
66+
let sassMap;
67+
let sassMapFile;
68+
let sassFileSrc;
69+
let sassFileSrcPath;
70+
let sourceFileIndex;
71+
72+
// Build Source Maps!
73+
if (sassObj.map) {
74+
// Transform map into JSON
75+
sassMap = JSON.parse(sassObj.map.toString());
76+
// Grab the stdout and transform it into stdin
77+
sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin');
78+
// Grab the base file name that's being worked on
79+
sassFileSrc = file.relative;
80+
// Grab the path portion of the file that's being worked on
81+
sassFileSrcPath = path.dirname(sassFileSrc);
82+
if (sassFileSrcPath) {
83+
// Prepend the path to all files in the sources array except the file that's being worked on
84+
sourceFileIndex = sassMap.sources.indexOf(sassMapFile);
85+
sassMap.sources = sassMap.sources.map((source, index) => { // eslint-disable-line arrow-body-style
86+
return index === sourceFileIndex ? source : path.join(sassFileSrcPath, source);
87+
});
88+
}
3889

90+
// Remove 'stdin' from souces and replace with filenames!
91+
sassMap.sources = sassMap.sources.filter(src => src !== 'stdin' && src);
3992

40-
opts = clonedeep(options || {});
41-
opts.data = file.contents.toString();
93+
// Replace the map file with the original file name (but new extension)
94+
sassMap.file = replaceExtension(sassFileSrc, '.css');
95+
// Apply the map
96+
applySourceMap(file, sassMap);
97+
}
4298

43-
// we set the file path here so that libsass can correctly resolve import paths
44-
opts.file = file.path;
99+
file.contents = sassObj.css; // eslint-disable-line no-param-reassign
100+
file.path = replaceExtension(file.path, '.css'); // eslint-disable-line no-param-reassign
45101

46-
// Ensure `indentedSyntax` is true if a `.sass` file
47-
if (path.extname(file.path) === '.sass') {
48-
opts.indentedSyntax = true;
49-
}
102+
cb(null, file);
103+
};
50104

51-
// Ensure file's parent directory in the include path
52-
if (opts.includePaths) {
53-
if (typeof opts.includePaths === 'string') {
54-
opts.includePaths = [opts.includePaths];
55-
}
56-
}
57-
else {
58-
opts.includePaths = [];
59-
}
105+
//////////////////////////////
106+
// Handles error message
107+
//////////////////////////////
108+
const errorM = (error) => {
109+
const filePath = (error.file === 'stdin' ? file.path : error.file) || file.path;
110+
const relativePath = path.relative(process.cwd(), filePath);
111+
const message = [chalk.underline(relativePath), error.formatted].join('\n');
60112

61-
opts.includePaths.unshift(path.dirname(file.path));
113+
error.messageFormatted = message; // eslint-disable-line no-param-reassign
114+
error.messageOriginal = error.message; // eslint-disable-line no-param-reassign
115+
error.message = stripAnsi(message); // eslint-disable-line no-param-reassign
116+
error.relativePath = relativePath; // eslint-disable-line no-param-reassign
62117

63-
// Generate Source Maps if plugin source-map present
64-
if (file.sourceMap) {
65-
opts.sourceMap = file.path;
66-
opts.omitSourceMapUrl = true;
67-
opts.sourceMapContents = true;
68-
}
118+
return cb(new PluginError(PLUGIN_NAME, error));
119+
};
69120

121+
if (sync !== true) {
70122
//////////////////////////////
71-
// Handles returning the file to the stream
123+
// Async Sass render
72124
//////////////////////////////
73-
filePush = function filePush(sassObj) {
74-
var sassMap,
75-
sassMapFile,
76-
sassFileSrc,
77-
sassFileSrcPath,
78-
sourceFileIndex;
79-
80-
// Build Source Maps!
81-
if (sassObj.map) {
82-
// Transform map into JSON
83-
sassMap = JSON.parse(sassObj.map.toString());
84-
// Grab the stdout and transform it into stdin
85-
sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin');
86-
// Grab the base file name that's being worked on
87-
sassFileSrc = file.relative;
88-
// Grab the path portion of the file that's being worked on
89-
sassFileSrcPath = path.dirname(sassFileSrc);
90-
if (sassFileSrcPath) {
91-
// Prepend the path to all files in the sources array except the file that's being worked on
92-
sourceFileIndex = sassMap.sources.indexOf(sassMapFile);
93-
sassMap.sources = sassMap.sources.map(function(source, index) {
94-
return (index === sourceFileIndex) ? source : path.join(sassFileSrcPath, source);
95-
});
96-
}
97-
98-
// Remove 'stdin' from souces and replace with filenames!
99-
sassMap.sources = sassMap.sources.filter(function(src) {
100-
if (src !== 'stdin') {
101-
return src;
102-
}
103-
});
104-
105-
// Replace the map file with the original file name (but new extension)
106-
sassMap.file = replaceExtension(sassFileSrc, '.css');
107-
// Apply the map
108-
applySourceMap(file, sassMap);
125+
const callback = (error, obj) => { // eslint-disable-line consistent-return
126+
if (error) {
127+
return errorM(error);
109128
}
110-
111-
file.contents = sassObj.css;
112-
file.path = replaceExtension(file.path, '.css');
113-
114-
cb(null, file);
129+
filePush(obj);
115130
};
116131

132+
gulpSass.compiler.render(opts, callback);
133+
} else {
117134
//////////////////////////////
118-
// Handles error message
135+
// Sync Sass render
119136
//////////////////////////////
120-
errorM = function errorM(error) {
121-
var relativePath = '',
122-
filePath = error.file === 'stdin' ? file.path : error.file,
123-
message = '';
124-
125-
filePath = filePath ? filePath : file.path;
126-
relativePath = path.relative(process.cwd(), filePath);
127-
128-
message += chalk.underline(relativePath) + '\n';
129-
message += error.formatted;
130-
131-
error.messageFormatted = message;
132-
error.messageOriginal = error.message;
133-
error.message = stripAnsi(message);
134-
135-
error.relativePath = relativePath;
136-
137-
return cb(new PluginError(
138-
PLUGIN_NAME, error
139-
));
140-
};
141-
142-
if (sync !== true) {
143-
//////////////////////////////
144-
// Async Sass render
145-
//////////////////////////////
146-
callback = function(error, obj) {
147-
if (error) {
148-
return errorM(error);
149-
}
150-
filePush(obj);
151-
};
152-
153-
gulpSass.compiler.render(opts, callback);
137+
try {
138+
filePush(gulpSass.compiler.renderSync(opts));
139+
} catch (error) {
140+
return errorM(error);
154141
}
155-
else {
156-
//////////////////////////////
157-
// Sync Sass render
158-
//////////////////////////////
159-
try {
160-
result = gulpSass.compiler.renderSync(opts);
161-
162-
filePush(result);
163-
}
164-
catch (error) {
165-
return errorM(error);
166-
}
167-
}
168-
});
169-
};
142+
}
143+
});
170144

171145
//////////////////////////////
172146
// Sync Sass render
173147
//////////////////////////////
174-
gulpSass.sync = function sync(options) {
175-
return gulpSass(options, true);
176-
};
148+
gulpSass.sync = options => gulpSass(options, true);
177149

178150
//////////////////////////////
179151
// Log errors nicely
180152
//////////////////////////////
181-
gulpSass.logError = function logError(error) {
182-
var message = new PluginError('sass', error.messageFormatted).toString();
183-
process.stderr.write(message + '\n');
153+
gulpSass.logError = (error) => {
154+
const message = new PluginError('sass', error.messageFormatted).toString();
155+
process.stderr.write(`${message}\n`);
184156
this.emit('end');
185157
};
186158

‎package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
"vinyl-sourcemaps-apply": "^0.2.0"
3535
},
3636
"devDependencies": {
37-
"eslint": "^2.9.0",
3837
"autoprefixer": "^8.1.0",
38+
"eslint": "^4.18.2",
39+
"eslint-config-airbnb-base": "^12.1.0",
40+
"eslint-plugin-import": "^2.9.0",
3941
"globule": "^1.0.0",
4042
"gulp": "^3.8.11",
4143
"gulp-postcss": "^7.0.1",

‎test/lint.js

+17-20
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
'use strict';
1+
const eslint = require('eslint');
2+
const should = require('should');
23

3-
var eslint = require('eslint');
4-
var should = require('should');
4+
describe('code style guide', () => {
5+
it('index.js should follow our lint style guide', (done) => {
6+
const cli = new eslint.CLIEngine({ rules: { 'spaced-comment': 0 } });
7+
const formatter = cli.getFormatter();
8+
const report = cli.executeOnFiles(['index.js']);
59

6-
var cli = new eslint.CLIEngine();
7-
var formatter = cli.getFormatter();
8-
9-
var report;
10-
11-
describe('code style guide', function() {
12-
it('index.js should follow our lint style guide', function(done) {
13-
report = cli.executeOnFiles(['index.js']);
1410
if (report.errorCount > 0 || report.warningCount > 0) {
1511
console.log(formatter(report.results));
1612
}
@@ -20,8 +16,11 @@ describe('code style guide', function() {
2016
done();
2117
});
2218

23-
it('test/main.js should follow our lint style guide', function(done) {
24-
report = cli.executeOnFiles(['test/main.js']);
19+
it('test/main.js should follow our lint style guide', (done) => {
20+
const cli = new eslint.CLIEngine();
21+
const formatter = cli.getFormatter();
22+
const report = cli.executeOnFiles(['test/main.js']);
23+
2524
if (report.errorCount > 0 || report.warningCount > 0) {
2625
console.log(formatter(report.results));
2726
}
@@ -31,13 +30,11 @@ describe('code style guide', function() {
3130
done();
3231
});
3332

34-
it('test/lint.js should follow our lint style guide', function(done) {
35-
cli = new eslint.CLIEngine({
36-
'rules': {
37-
'no-console': 0
38-
}
39-
});
40-
report = cli.executeOnFiles(['test/lint.js']);
33+
it('test/lint.js should follow our lint style guide', (done) => {
34+
const cli = new eslint.CLIEngine({ rules: { 'no-console': 0 } });
35+
const formatter = cli.getFormatter();
36+
const report = cli.executeOnFiles(['test/lint.js']);
37+
4138
if (report.errorCount > 0 || report.warningCount > 0) {
4239
console.log(formatter(report.results));
4340
}

‎test/main.js

+201-225
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.