diff options
author | XhmikosR <xhmikosr@gmail.com> | 2019-02-13 13:48:15 +0300 |
---|---|---|
committer | Martijn Cuppens <martijn.cuppens@gmail.com> | 2019-02-13 13:48:15 +0300 |
commit | e66e165b6e4b104266b6d95b926dff6d4999e90d (patch) | |
tree | 2d6f88a5ad04e3b9f804d831117b2831d3be48df | |
parent | 6bc081ecf0ba95ca6ac1a976741ef2cb7a521fa4 (diff) |
ES6-ify and lint all JS files. (#79)
* Enforce LF
* ES6-ify and lint all JS files.
-rw-r--r-- | examples/less/gulp/gulpfile.js | 8 | ||||
-rw-r--r-- | examples/less/node/index.js | 49 | ||||
-rw-r--r-- | examples/postcss/gulp/gulpfile.js | 5 | ||||
-rw-r--r-- | examples/postcss/node/index.js | 11 | ||||
-rw-r--r-- | examples/scss/gulp/gulpfile.js | 8 | ||||
-rw-r--r-- | examples/scss/node/index.js | 34 | ||||
-rw-r--r-- | examples/stylus/gulp/gulpfile.js | 8 | ||||
-rw-r--r-- | examples/stylus/node/index.js | 26 | ||||
-rw-r--r-- | postcss.js | 110 | ||||
-rw-r--r-- | test/lib/result.js | 30 | ||||
-rw-r--r-- | test/lib/tests.js | 4 | ||||
-rw-r--r-- | test/postcss/tests.js | 6 | ||||
-rw-r--r-- | test/test.js | 2 |
13 files changed, 165 insertions, 136 deletions
diff --git a/examples/less/gulp/gulpfile.js b/examples/less/gulp/gulpfile.js index 37e7afc..51600f9 100644 --- a/examples/less/gulp/gulpfile.js +++ b/examples/less/gulp/gulpfile.js @@ -1,14 +1,14 @@ 'use strict'; -const gulp = require('gulp'), - less = require('gulp-less'); +const gulp = require('gulp'); +const less = require('gulp-less'); -gulp.task('build', function () { +gulp.task('build', () => { return gulp.src('./src/**/*.less') .pipe(less()) .pipe(gulp.dest('./dest')); }); -gulp.task('watch', function () { +gulp.task('watch', () => { gulp.watch('./src/**/*.less', ['build']); }); diff --git a/examples/less/node/index.js b/examples/less/node/index.js index c6269fc..b263c93 100644 --- a/examples/less/node/index.js +++ b/examples/less/node/index.js @@ -1,24 +1,27 @@ -const fs = require('fs'), - less = require('less'), - str = fs.readFileSync(__dirname + '/src/main.less', 'utf8'); +'use strict'; -less - .render( - str, - {paths: [__dirname + '/src']} - ) - .then( - function (output) { - fs.writeFile(__dirname + '/dest/main.css', output.css, function (err) { - if (!err) { - console.log('Responsive font sizes generated.'); - } - else { - throw err; - } - }); - }, - function (error) { - throw error; - } - ); +const fs = require('fs'); +const path = require('path'); +const less = require('less'); + +const str = fs.readFileSync(path.join(__dirname, '/src/main.less'), 'utf8'); + +less.render( + str, + { + paths: [path.join(__dirname, '/src')] + } +).then( + output => { + fs.writeFile(path.join(__dirname, '/dest/main.css'), output.css, err => { + if (err) { + throw err; + } else { + console.log('Responsive font sizes generated.'); + } + }); + }, + error => { + throw error; + } +); diff --git a/examples/postcss/gulp/gulpfile.js b/examples/postcss/gulp/gulpfile.js index f4111d7..9c56767 100644 --- a/examples/postcss/gulp/gulpfile.js +++ b/examples/postcss/gulp/gulpfile.js @@ -1,6 +1,9 @@ +'use strict'; + const gulp = require('gulp'); const postcss = require('gulp-postcss'); const rfs = require('../../..'); + const options = { twoDimensional: false, baseFontSize: 20, @@ -21,6 +24,6 @@ gulp.task('build', () => { .pipe(gulp.dest('./dest')); }); -gulp.task('watch', function () { +gulp.task('watch', () => { gulp.watch('./src/**/*.css', ['build']); }); diff --git a/examples/postcss/node/index.js b/examples/postcss/node/index.js index f63b047..8ab9a1e 100644 --- a/examples/postcss/node/index.js +++ b/examples/postcss/node/index.js @@ -1,7 +1,11 @@ +'use strict'; + const fs = require('fs'); +const path = require('path'); const postcss = require('postcss'); const rfs = require('../../..'); -const css = fs.readFileSync(__dirname + '/src/main.css', 'utf8'); + +const css = fs.readFileSync(path.join(__dirname, '/src/main.css'), 'utf8'); const options = { twoDimensional: false, baseFontSize: 20, @@ -16,11 +20,10 @@ const options = { const processedCss = postcss(rfs(options)).process(css).css; -fs.writeFile(__dirname + '/dest/main.css', processedCss, (err) => { +fs.writeFile(path.join(__dirname, '/dest/main.css'), processedCss, err => { if (err) { throw err; - } - else { + } else { console.log('Responsive font sizes generated.'); } }); diff --git a/examples/scss/gulp/gulpfile.js b/examples/scss/gulp/gulpfile.js index 62782e9..f46a31d 100644 --- a/examples/scss/gulp/gulpfile.js +++ b/examples/scss/gulp/gulpfile.js @@ -1,14 +1,14 @@ 'use strict'; -const gulp = require('gulp'), - sass = require('gulp-sass'); +const gulp = require('gulp'); +const sass = require('gulp-sass'); -gulp.task('build', function () { +gulp.task('build', () => { return gulp.src('./src/**/*.scss') .pipe(sass().on('error', sass.logError)) .pipe(gulp.dest('./dest')); }); -gulp.task('watch', function () { +gulp.task('watch', () => { gulp.watch('./src/**/*.scss', ['build']); }); diff --git a/examples/scss/node/index.js b/examples/scss/node/index.js index dde62f0..77baa8e 100644 --- a/examples/scss/node/index.js +++ b/examples/scss/node/index.js @@ -1,22 +1,24 @@ +'use strict'; + const fs = require('fs'); +const path = require('path'); const sass = require('node-sass'); sass.render({ - file: __dirname + '/src/main.scss', outFile: __dirname + '/dest/main.css', - }, function (error, result) { // node-style callback from v3.0.0 onwards - if (!error) { - // No errors during the compilation, write this result on the disk - fs.writeFile(__dirname + '/dest/main.css', result.css, function (err) { - if (!err) { - console.log('Responsive font sizes generated.'); - } - else { - throw err; - } - }); - } - else { - throw error; - } + file: path.join(__dirname, '/src/main.scss'), + outFile: path.join(__dirname, '/dest/main.css') +}, (error, result) => { // Node-style callback from v3.0.0 onwards + if (error) { + throw error; + } else { + // No errors during the compilation, write this result on the disk + fs.writeFile(path.join(__dirname, '/dest/main.css'), result.css, err => { + if (err) { + throw err; + } else { + console.log('Responsive font sizes generated.'); + } + }); } +} ); diff --git a/examples/stylus/gulp/gulpfile.js b/examples/stylus/gulp/gulpfile.js index fb5d134..3ea42bd 100644 --- a/examples/stylus/gulp/gulpfile.js +++ b/examples/stylus/gulp/gulpfile.js @@ -1,14 +1,14 @@ 'use strict'; -const gulp = require('gulp'), - stylus = require('gulp-stylus'); +const gulp = require('gulp'); +const stylus = require('gulp-stylus'); -gulp.task('build', function () { +gulp.task('build', () => { return gulp.src('./src/**/*.styl') .pipe(stylus()) .pipe(gulp.dest('./dest')); }); -gulp.task('watch', function () { +gulp.task('watch', () => { gulp.watch('./src/**/*.styl', ['build']); }); diff --git a/examples/stylus/node/index.js b/examples/stylus/node/index.js index 427bb39..62ae685 100644 --- a/examples/stylus/node/index.js +++ b/examples/stylus/node/index.js @@ -1,18 +1,22 @@ -const fs = require('fs'), - stylus = require('stylus'), - str = fs.readFileSync(__dirname + '/src/main.styl', 'utf8'); +'use strict'; -stylus.render(str, {filename: __dirname + '/src/main.styl'}, function (err, css) { +const fs = require('fs'); +const path = require('path'); +const stylus = require('stylus'); + +const str = fs.readFileSync(path.join(__dirname, '/src/main.styl'), 'utf8'); + +stylus.render(str, { + filename: path.join(__dirname, '/src/main.styl') +}, (err, css) => { if (err) { throw err; - } - else { - fs.writeFile(__dirname + '/dest/main.css', css, function (err) { - if (!err) { - console.log('Responsive font sizes generated.'); - } - else { + } else { + fs.writeFile(path.join(__dirname, '/dest/main.css'), css, err => { + if (err) { throw err; + } else { + console.log('Responsive font sizes generated.'); } }); } @@ -8,7 +8,7 @@ const postcss = require('postcss'); -module.exports = postcss.plugin('postcss-rfs', function (opts) { +module.exports = postcss.plugin('postcss-rfs', opts => { const BREAKPOINT_ERROR = 'breakpoint option is invalid, it must be set in `px`, `rem` or `em`.'; const BREAKPOINT_UNIT_ERROR = 'breakpointUnit option is invalid, it must be `px`, `rem` or `em`.'; const BASE_FONT_SIZE_ERROR = 'baseFontSize option is invalid, it must be set in `px` or `rem`.'; @@ -28,16 +28,15 @@ module.exports = postcss.plugin('postcss-rfs', function (opts) { safariIframeResizeBugFix: false, enableResponsiveFontSizes: true }; + opts = Object.assign(defaultOptions, opts); if (typeof opts.baseFontSize !== 'number') { if (opts.baseFontSize.endsWith('px')) { opts.baseFontSize = parseFloat(opts.baseFontSize); - } - else if (opts.baseFontSize.endsWith('rem')) { + } else if (opts.baseFontSize.endsWith('rem')) { opts.baseFontSize = parseFloat(opts.baseFontSize) / opts.remValue; - } - else { + } else { console.error(BASE_FONT_SIZE_ERROR); } } @@ -45,26 +44,22 @@ module.exports = postcss.plugin('postcss-rfs', function (opts) { if (typeof opts.breakpoint !== 'number') { if (opts.breakpoint.endsWith('px')) { opts.breakpoint = parseFloat(opts.breakpoint); - } - else if (opts.breakpoint.endsWith('em')) { + } else if (opts.breakpoint.endsWith('em')) { opts.breakpoint = parseFloat(opts.breakpoint) * opts.remValue; - } - else { + } else { console.error(BREAKPOINT_ERROR); } } - return function (css) { - - css.walkRules(function (rule) { - + return css => { + css.walkRules(rule => { if (rule.selector.includes(DISABLE_RESPONSIVE_FONT_SIZE_SELECTOR)) { return; } - rule.walkDecls(function (decl) { + rule.walkDecls(decl => { // Skip if property is not in propList - if (opts.propList.indexOf(decl.prop) === -1) { + if (!opts.propList.includes(decl.prop)) { return; } @@ -80,18 +75,16 @@ module.exports = postcss.plugin('postcss-rfs', function (opts) { let value = parseFloat(decl.value); // Multiply by remValue if value is in rem - if (decl.value.indexOf('rem') > -1) { + if (decl.value.includes('rem')) { value *= opts.remValue; } // Render value in desired unit if (opts.fontSizeUnit === 'px') { - decl.value = toFixed(value, opts.unitPrecision) + 'px'; - } - else if (opts.fontSizeUnit === 'rem') { - decl.value = toFixed(value / opts.remValue, opts.unitPrecision) + 'rem'; - } - else { + decl.value = `${toFixed(value, opts.unitPrecision)}px`; + } else if (opts.fontSizeUnit === 'rem') { + decl.value = `${toFixed(value / opts.remValue, opts.unitPrecision)}rem`; + } else { console.error('fontSizeUnit option is not valid, it must be `px` or `rem`.'); } @@ -101,7 +94,7 @@ module.exports = postcss.plugin('postcss-rfs', function (opts) { } // Calculate font-size and font-size difference - let baseFontSize = opts.baseFontSize + (value - opts.baseFontSize) / opts.factor; + let baseFontSize = opts.baseFontSize + ((value - opts.baseFontSize) / opts.factor); const fontSizeDiff = value - baseFontSize; // Divide by remValue if needed @@ -111,7 +104,7 @@ module.exports = postcss.plugin('postcss-rfs', function (opts) { const viewportUnit = opts.twoDimensional ? 'vmin' : 'vw'; - value = 'calc(' + toFixed(baseFontSize, opts.unitPrecision) + opts.fontSizeUnit + ' + ' + toFixed((fontSizeDiff * 100 / opts.breakpoint), opts.unitPrecision) + viewportUnit + ')'; + value = `calc(${toFixed(baseFontSize, opts.unitPrecision)}${opts.fontSizeUnit} + ${toFixed(fontSizeDiff * 100 / opts.breakpoint, opts.unitPrecision)}${viewportUnit})`; const mediaQuery = postcss.atRule(renderMediaQuery(opts)); let rule_selector = rule.selector; @@ -121,23 +114,27 @@ module.exports = postcss.plugin('postcss-rfs', function (opts) { const selectors = rule.selector.split(','); let ruleSelector = ''; - for (let selector of selectors) { - ruleSelector += ENABLE_RESPONSIVE_FONT_SIZE_SELECTOR + ' ' + selector + ',\n'; - ruleSelector += selector + ENABLE_RESPONSIVE_FONT_SIZE_SELECTOR + ',\n'; + for (const selector of selectors) { + ruleSelector += `${ENABLE_RESPONSIVE_FONT_SIZE_SELECTOR} ${selector},\n`; + ruleSelector += `${selector + ENABLE_RESPONSIVE_FONT_SIZE_SELECTOR},\n`; } + rule_selector = ruleSelector.slice(0, -2); } - const mediaQueryRule = postcss.rule({ selector: rule_selector, source: rule.source }); - mediaQueryRule.append(decl.clone({value: value})); + + mediaQueryRule.append(decl.clone({value})); // Safari iframe resize bug: https://github.com/twbs/rfs/issues/14 if (opts.safariIframeResizeBugFix) { - mediaQueryRule.append(postcss.decl({ prop: 'min-width', value: '0vw' })); + mediaQueryRule.append(postcss.decl({ + prop: 'min-width', + value: '0vw' + })); } mediaQuery.append(mediaQueryRule); @@ -148,52 +145,58 @@ module.exports = postcss.plugin('postcss-rfs', function (opts) { const selectors = rule.selector.split(','); let ruleSelector = ''; - for (let selector of selectors) { - ruleSelector += selector + ',\n'; - ruleSelector += DISABLE_RESPONSIVE_FONT_SIZE_SELECTOR + ' ' + selector + ',\n'; - ruleSelector += selector + DISABLE_RESPONSIVE_FONT_SIZE_SELECTOR + ',\n'; + for (const selector of selectors) { + ruleSelector += `${selector},\n`; + ruleSelector += `${DISABLE_RESPONSIVE_FONT_SIZE_SELECTOR} ${selector},\n`; + ruleSelector += `${selector + DISABLE_RESPONSIVE_FONT_SIZE_SELECTOR},\n`; } - ruleSelector = ruleSelector.slice(0, - 2); - const dc_rule = postcss.rule({ + ruleSelector = ruleSelector.slice(0, -2); + + const dcRule = postcss.rule({ selector: ruleSelector, source: rule.source }); - dc_rule.append(decl.clone()); - rule.parent.insertAfter(rule, dc_rule); - decl.prev() || decl.next() ? decl.remove() : decl.parent.remove(); + dcRule.append(decl.clone()); + rule.parent.insertAfter(rule, dcRule); + + if (decl.prev() || decl.next()) { + decl.remove(); + } else { + decl.parent.remove(); + } } }); }); - }; - function renderMediaQuery (opts) { + function renderMediaQuery(opts) { const mediaQuery = { name: 'media' }; switch (opts.breakpointUnit) { case 'em': - case 'rem': + case 'rem': { const breakpoint = opts.breakpoint / opts.remValue; if (opts.twoDimensional) { - mediaQuery.params = '(max-width: ' + breakpoint + opts.breakpointUnit + '), (max-height: ' + breakpoint + opts.breakpointUnit + ')'; - } - else { - mediaQuery.params = '(max-width: ' + breakpoint + opts.breakpointUnit + ')'; + mediaQuery.params = `(max-width: ${breakpoint}${opts.breakpointUnit}), (max-height: ${breakpoint}${opts.breakpointUnit})`; + } else { + mediaQuery.params = `(max-width: ${breakpoint}${opts.breakpointUnit})`; } + break; + } case 'px': if (opts.twoDimensional) { - mediaQuery.params = '(max-width: ' + opts.breakpoint + 'px), (max-height: ' + opts.breakpoint + 'px)'; - } - else { - mediaQuery.params = '(max-width: ' + opts.breakpoint + 'px)'; + mediaQuery.params = `(max-width: ${opts.breakpoint}px), (max-height: ${opts.breakpoint}px)`; + } else { + mediaQuery.params = `(max-width: ${opts.breakpoint}px)`; } + break; default: @@ -204,9 +207,10 @@ module.exports = postcss.plugin('postcss-rfs', function (opts) { return mediaQuery; } - function toFixed (number, precision) { - const multiplier = Math.pow(10, precision + 1), - wholeNumber = Math.floor(number * multiplier); + function toFixed(number, precision) { + const multiplier = Math.pow(10, precision + 1); + const wholeNumber = Math.floor(number * multiplier); + return Math.round(wholeNumber / 10) * 10 / multiplier; } }); diff --git a/test/lib/result.js b/test/lib/result.js index 8f59a36..add6340 100644 --- a/test/lib/result.js +++ b/test/lib/result.js @@ -1,49 +1,53 @@ +'use strict'; + // Packages -const prettier = require("prettier"); -const fs = require("fs"); +const fs = require('fs'); +const prettier = require('prettier'); const sass = require('node-sass'); const less = require('less'); const stylus = require('stylus'); +// Postcss +const postcss = require('postcss'); +const rfs = require('../..'); +const postcsstests = require('../postcss/tests.js'); + // Strings const encoding = 'utf8'; const dir = `${__dirname}/../`; // Functions -const format = css => prettier.format(css, {parser: "css"}).replace(/(\n)(\n)/g, '$1'); +const format = css => prettier.format(css, {parser: 'css'}).replace(/(\n)(\n)/g, '$1'); const getFileContent = (folder, id, ext) => fs.readFileSync(`${dir}${folder}/${id}.${ext}`, {encoding}); -// Postcss -const postcss = require('postcss'); -const rfs = require('../../'); -const postcsstests = require('../postcss/tests.js'); const postcsscss = getFileContent('postcss', 'main', 'css'); // Exports module.exports = { // Return formatted expected result - expected: (id) => format(getFileContent('result', id, 'css')), + expected: id => format(getFileContent('result', id, 'css')), // Return parsed css - sass: (id) => format(sass.renderSync({file: `${dir}sass/${id}.scss`}).css.toString(encoding)), + sass: id => format(sass.renderSync({file: `${dir}sass/${id}.scss`}).css.toString(encoding)), // Return parsed css - less: (id) => { - return less.render(getFileContent('less', id, 'less'), {paths: [dir + 'less'], syncImport: true}).then((result) => { + less: id => { + return less.render(getFileContent('less', id, 'less'), {paths: [dir + 'less'], syncImport: true}).then(result => { return format(result.css); }); }, - stylus: (id) => { + stylus: id => { let formattedCSS = ''; stylus.render(getFileContent('stylus', id, 'styl'), {filename: `${dir}stylus/${id}.styl`}, (err, css) => { if (err) { throw err; } + formattedCSS = format(css); }); return formattedCSS; }, - postcss: (id) => format(postcss(rfs(postcsstests[id])).process(postcsscss).css) + postcss: id => format(postcss(rfs(postcsstests[id])).process(postcsscss).css) }; diff --git a/test/lib/tests.js b/test/lib/tests.js index 361b0bf..45dc97d 100644 --- a/test/lib/tests.js +++ b/test/lib/tests.js @@ -1,3 +1,5 @@ +'use strict'; + module.exports = [ { id: 'test-1', @@ -38,5 +40,5 @@ module.exports = [ { id: 'test-10', name: 'Include mixins multiple times' - }, + } ]; diff --git a/test/postcss/tests.js b/test/postcss/tests.js index 37fb0dd..b5f03d6 100644 --- a/test/postcss/tests.js +++ b/test/postcss/tests.js @@ -1,3 +1,5 @@ +'use strict'; + module.exports = { 'test-1': {}, 'test-2': { @@ -29,7 +31,7 @@ module.exports = { twoDimensional: true, factor: 5, class: true, - safariIframeResizeBugFix: true, + safariIframeResizeBugFix: true }, 'test-10': { // Not testable baseFontSize: '12px', @@ -39,6 +41,6 @@ module.exports = { twoDimensional: true, factor: 5, class: true, - safariIframeResizeBugFix: true, + safariIframeResizeBugFix: true } }; diff --git a/test/test.js b/test/test.js index 8340e86..9296599 100644 --- a/test/test.js +++ b/test/test.js @@ -1,3 +1,5 @@ +/* eslint-env mocha */ + 'use strict'; const assert = require('assert'); |