From 868ce9d5c7678714a812d9378c4da8c5b5d5b691 Mon Sep 17 00:00:00 2001 From: Ryan Tsao Date: Fri, 21 Aug 2015 22:02:56 -0700 Subject: [PATCH] Fix lint errors and enforced consistency --- .eslintrc | 3 +- package.json | 2 +- src/file-system-loader.js | 60 +++++++++++++++++----------------- src/index.js | 29 ++++++++--------- src/parser.js | 68 ++++++++++++++++++++------------------- 5 files changed, 83 insertions(+), 79 deletions(-) diff --git a/.eslintrc b/.eslintrc index ca8ae3b..f268d48 100644 --- a/.eslintrc +++ b/.eslintrc @@ -21,6 +21,7 @@ "jsx": true }, "rules": { - "quotes": "single" + "quotes": [2, "single"], + "space-in-parens": [2, "always"] } } diff --git a/package.json b/package.json index ecec1f9..c23f35d 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "lint": "eslint src", "build": "babel --out-dir lib src", "autotest": "chokidar src test -c 'npm test'", - "test": "mocha --compilers js:babel/register", + "test": "npm run lint && mocha --compilers js:babel/register", "prepublish": "npm run build" }, "repository": { diff --git a/src/file-system-loader.js b/src/file-system-loader.js index 2a3f47d..3f88c40 100644 --- a/src/file-system-loader.js +++ b/src/file-system-loader.js @@ -1,6 +1,6 @@ -import Core from './index.js' -import fs from 'fs' -import path from 'path' +import Core from './index.js'; +import fs from 'fs'; +import path from 'path'; // Sorts dependencies in the following way: // AAA comes before AA and A @@ -11,56 +11,58 @@ import path from 'path' // - After all their dependencies const traceKeySorter = ( a, b ) => { if ( a.length < b.length ) { - return a < b.substring( 0, a.length ) ? -1 : 1 + return a < b.substring( 0, a.length ) ? -1 : 1; } else if ( a.length > b.length ) { - return a.substring( 0, b.length ) <= b ? -1 : 1 + return a.substring( 0, b.length ) <= b ? -1 : 1; } else { - return a < b ? -1 : 1 + return a < b ? -1 : 1; } }; export default class FileSystemLoader { constructor( root, plugins ) { - this.root = root - this.sources = {} - this.importNr = 0 - this.core = new Core(plugins) + this.root = root; + this.sources = {}; + this.importNr = 0; + this.core = new Core( plugins ); this.tokensByFile = {}; } fetch( _newPath, relativeTo, _trace ) { - let newPath = _newPath.replace( /^["']|["']$/g, "" ), - trace = _trace || String.fromCharCode( this.importNr++ ) + let newPath = _newPath.replace( /^["']|["']$/g, '' ); + let trace = _trace || String.fromCharCode( this.importNr++ ); return new Promise( ( resolve, reject ) => { - let relativeDir = path.dirname( relativeTo ), - rootRelativePath = path.resolve( relativeDir, newPath ), - fileRelativePath = path.resolve( path.join( this.root, relativeDir ), newPath ) + let relativeDir = path.dirname( relativeTo ); + let rootRelativePath = path.resolve( relativeDir, newPath ); + let fileRelativePath = path.resolve( path.join( this.root, relativeDir ), newPath ); // if the path is not relative or absolute, try to resolve it in node_modules - if (newPath[0] !== '.' && newPath[0] !== '/') { + if ( newPath[0] !== '.' && newPath[0] !== '/' ) { try { - fileRelativePath = require.resolve(newPath); + fileRelativePath = require.resolve( newPath ); + } + catch ( e ) { + console.error( e ); } - catch (e) {} } - const tokens = this.tokensByFile[fileRelativePath] - if (tokens) { return resolve(tokens) } + const tokens = this.tokensByFile[fileRelativePath]; + if ( tokens ) { return resolve( tokens ); } - fs.readFile( fileRelativePath, "utf-8", ( err, source ) => { - if ( err ) reject( err ) + fs.readFile( fileRelativePath, 'utf-8', ( err, source ) => { + if ( err ) { reject( err ); } this.core.load( source, rootRelativePath, trace, this.fetch.bind( this ) ) .then( ( { injectableSource, exportTokens } ) => { - this.sources[trace] = injectableSource - this.tokensByFile[fileRelativePath] = exportTokens - resolve( exportTokens ) - }, reject ) - } ) - } ) + this.sources[trace] = injectableSource; + this.tokensByFile[fileRelativePath] = exportTokens; + resolve( exportTokens ); + }, reject ); + } ); + } ); } get finalSource() { return Object.keys( this.sources ).sort( traceKeySorter ).map( s => this.sources[s] ) - .join( "" ) + .join( '' ); } } diff --git a/src/index.js b/src/index.js index f5994ef..24f7bc2 100644 --- a/src/index.js +++ b/src/index.js @@ -1,29 +1,28 @@ -import postcss from 'postcss' -import localByDefault from 'postcss-modules-local-by-default' -import extractImports from 'postcss-modules-extract-imports' -import scope from 'postcss-modules-scope' +import postcss from 'postcss'; +import localByDefault from 'postcss-modules-local-by-default'; +import extractImports from 'postcss-modules-extract-imports'; +import scope from 'postcss-modules-scope'; -import Parser from './parser' +import Parser from './parser'; export default class Core { constructor( plugins ) { - this.plugins = plugins || Core.defaultPlugins + this.plugins = plugins || Core.defaultPlugins; } load( sourceString, sourcePath, trace, pathFetcher ) { - let parser = new Parser( pathFetcher, trace ) + let parser = new Parser( pathFetcher, trace ); return postcss( this.plugins.concat( [parser.plugin] ) ) - .process( sourceString, { from: "/" + sourcePath } ) + .process( sourceString, { from: '/' + sourcePath } ) .then( result => { - return { injectableSource: result.css, exportTokens: parser.exportTokens } - } ) + return { injectableSource: result.css, exportTokens: parser.exportTokens }; + } ); } } - // These three plugins are aliased under this package for simplicity. -Core.localByDefault = localByDefault -Core.extractImports = extractImports -Core.scope = scope -Core.defaultPlugins = [localByDefault, extractImports, scope] +Core.localByDefault = localByDefault; +Core.extractImports = extractImports; +Core.scope = scope; +Core.defaultPlugins = [localByDefault, extractImports, scope]; diff --git a/src/parser.js b/src/parser.js index 536e9cd..10dc756 100644 --- a/src/parser.js +++ b/src/parser.js @@ -1,66 +1,68 @@ -const importRegexp = /^:import\((.+)\)$/ +const importRegexp = /^:import\((.+)\)$/; export default class Parser { constructor( pathFetcher, trace ) { - this.pathFetcher = pathFetcher - this.plugin = this.plugin.bind( this ) - this.exportTokens = {} - this.translations = {} - this.trace = trace + this.pathFetcher = pathFetcher; + this.plugin = this.plugin.bind( this ); + this.exportTokens = {}; + this.translations = {}; + this.trace = trace; } - plugin( css, result ) { + plugin( css ) { return Promise.all( this.fetchAllImports( css ) ) - .then( _ => this.linkImportedSymbols( css ) ) - .then( _ => this.extractExports( css ) ) + .then( () => this.linkImportedSymbols( css ) ) + .then( () => this.extractExports( css ) ); } fetchAllImports( css ) { - let imports = [] + let imports = []; css.each( node => { - if ( node.type == "rule" && node.selector.match( importRegexp ) ) { - imports.push( this.fetchImport( node, css.source.input.from, imports.length ) ) + if ( node.type === 'rule' && node.selector.match( importRegexp ) ) { + imports.push( this.fetchImport( node, css.source.input.from, imports.length ) ); } - } ) - return imports + } ); + return imports; } linkImportedSymbols( css ) { css.eachDecl( decl => { - Object.keys(this.translations).forEach( translation => { - decl.value = decl.value.replace(translation, this.translations[translation]) - } ) - }) + Object.keys( this.translations ).forEach( translation => { + decl.value = decl.value.replace( translation, this.translations[translation] ); + } ); + } ); } extractExports( css ) { css.each( node => { - if ( node.type == "rule" && node.selector == ":export" ) this.handleExport( node ) - } ) + if ( node.type === 'rule' && node.selector === ':export' ) { + this.handleExport( node ); + } + } ); } handleExport( exportNode ) { exportNode.each( decl => { - if ( decl.type == 'decl' ) { - Object.keys(this.translations).forEach( translation => { - decl.value = decl.value.replace(translation, this.translations[translation]) - } ) - this.exportTokens[decl.prop] = decl.value + if ( decl.type === 'decl' ) { + Object.keys( this.translations ).forEach( translation => { + decl.value = decl.value.replace( translation, this.translations[translation] ); + } ); + this.exportTokens[decl.prop] = decl.value; } - } ) - exportNode.removeSelf() + } ); + exportNode.removeSelf(); } fetchImport( importNode, relativeTo, depNr ) { let file = importNode.selector.match( importRegexp )[1], - depTrace = this.trace + String.fromCharCode(depNr) + depTrace = this.trace + String.fromCharCode( depNr ); return this.pathFetcher( file, relativeTo, depTrace ).then( exports => { importNode.each( decl => { - if ( decl.type == 'decl' ) { - this.translations[decl.prop] = exports[decl.value] + if ( decl.type === 'decl' ) { + this.translations[decl.prop] = exports[decl.value]; } - } ) - importNode.removeSelf() - }, err => console.log( err ) ) + } ); + importNode.removeSelf(); + }, err => console.log( err ) ); } }