Skip to content

Commit 4070086

Browse files
committed
lint errors fixed
1 parent 7e8382c commit 4070086

File tree

8 files changed

+86
-104
lines changed

8 files changed

+86
-104
lines changed

index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
'use strict';
2-
31
module.exports = require('./dist');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"scripts": {
2525
"start": "esw -w .",
2626
"lint": "eslint .",
27-
"pretest": "npm run -s lint || true",
27+
"pretest": "npm run -s lint",
2828
"test": "npm run -s build && mocha --compilers js:babel/register",
2929
"build": "babel src --out-dir dist",
3030
"prepublish": "in-publish && npm run -s build || in-install"

src/guard.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
if (global._cssModulesPolyfill) {
42
throw new Error('only one instance of css-modules/polyfill is allowed');
53
}

src/hook.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
'use strict';
2-
31
/**
42
* @param {function} compile
53
*/
6-
module.exports = function (compile) {
7-
require.extensions['.css'] = function (m, filename) {
8-
var tokens = compile(filename);
4+
export default function attachHook(compile) {
5+
require.extensions['.css'] = function hook(m, filename) {
6+
const tokens = compile(filename);
97
return m._compile('module.exports = ' + JSON.stringify(tokens), filename);
108
};
11-
};
9+
}

src/index.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
2-
31
import './guard';
42
import hook from './hook';
53
import postcss from 'postcss';
6-
import { basename, dirname, join, relative, resolve } from 'path';
4+
import { dirname, join, relative, resolve } from 'path';
75
import { readFileSync } from 'fs';
86

97
import ExtractImports from 'postcss-modules-extract-imports';
@@ -22,7 +20,7 @@ let rootDir;
2220
* @return {object}
2321
*/
2422
function load(sourceString, sourcePath, trace, pathFetcher) {
25-
let result = postcss(plugins.concat(new Parser({ pathFetcher, trace })))
23+
const result = postcss(plugins.concat(new Parser({ pathFetcher, trace })))
2624
.process(sourceString, {from: sourcePath})
2725
.root;
2826

@@ -35,25 +33,25 @@ hook(filename => {
3533
let importNr = 0;
3634

3735
const fetch = (_newPath, _relativeTo, _trace) => {
38-
let newPath = _newPath.replace(/^["']|["']$/g, '');
39-
let trace = _trace || String.fromCharCode(importNr++);
36+
const newPath = _newPath.replace(/^["']|["']$/g, '');
37+
const trace = _trace || String.fromCharCode(importNr++);
4038

41-
let relativeDir = dirname(_relativeTo);
42-
let rootRelativePath = resolve(relativeDir, newPath);
43-
let fileRelativePath = resolve(join(root, relativeDir), newPath);
39+
const relativeDir = dirname(_relativeTo);
40+
const rootRelativePath = resolve(relativeDir, newPath);
41+
const fileRelativePath = resolve(join(root, relativeDir), newPath);
4442

4543
const tokens = tokensByFile[fileRelativePath];
4644
if (tokens) {
4745
return tokens;
4846
}
4947

50-
let source = readFileSync(fileRelativePath, 'utf-8');
51-
let exportTokens = load(source, rootRelativePath, trace, fetch);
48+
const source = readFileSync(fileRelativePath, 'utf-8');
49+
const exportTokens = load(source, rootRelativePath, trace, fetch);
5250

5351
tokensByFile[fileRelativePath] = exportTokens;
5452

5553
return exportTokens;
56-
}
54+
};
5755

5856
return fetch(relative(root, filename), '/');
5957
});
@@ -63,10 +61,8 @@ hook(filename => {
6361
* @param {array} opts.u
6462
* @param {array} opts.use
6563
*/
66-
export default function configure(opts) {
67-
opts = opts || {};
68-
69-
let customPlugins = opts.u || opts.use;
64+
export default function configure(opts = {}) {
65+
const customPlugins = opts.u || opts.use;
7066
plugins = Array.isArray(customPlugins)
7167
? customPlugins
7268
: [LocalByDefault, ExtractImports, Scope];

src/parser.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
'use strict';
2-
31
import { plugin } from 'postcss';
42

5-
const importRegexp = /^:import\((.+)\)$/
6-
7-
export default plugin('parser', function (opts) {
8-
opts = opts || {};
3+
const importRegexp = /^:import\((.+)\)$/;
94

10-
let exportTokens = {};
11-
let translations = {};
5+
export default plugin('parser', function parser(opts = {}) {
6+
const exportTokens = {};
7+
const translations = {};
128

139
const fetchImport = (importNode, relativeTo, depNr) => {
14-
let file = importNode.selector.match( importRegexp )[1];
15-
let depTrace = opts.trace + String.fromCharCode(depNr);
16-
let exports = opts.pathFetcher(file, relativeTo, depTrace);
10+
const file = importNode.selector.match(importRegexp)[1];
11+
const depTrace = opts.trace + String.fromCharCode(depNr);
12+
const exports = opts.pathFetcher(file, relativeTo, depTrace);
1713

1814
importNode.each(decl => {
1915
if (decl.type === 'decl') {
@@ -22,7 +18,7 @@ export default plugin('parser', function (opts) {
2218
});
2319

2420
importNode.removeSelf();
25-
}
21+
};
2622

2723
const fetchAllImports = css => {
2824
let imports = 0;
@@ -32,27 +28,27 @@ export default plugin('parser', function (opts) {
3228
fetchImport(node, css.source.input.from, imports++);
3329
}
3430
});
35-
}
31+
};
3632

3733
const linkImportedSymbols = css => css.eachDecl(decl => {
3834
Object.keys(translations).forEach(translation => {
39-
decl.value = decl.value.replace(translation, translations[translation])
35+
decl.value = decl.value.replace(translation, translations[translation]);
4036
});
4137
});
4238

4339
const handleExport = exportNode => {
4440
exportNode.each(decl => {
4541
if (decl.type === 'decl') {
4642
Object.keys(translations).forEach(translation => {
47-
decl.value = decl.value.replace(translation, translations[translation])
43+
decl.value = decl.value.replace(translation, translations[translation]);
4844
});
4945

5046
exportTokens[decl.prop] = decl.value;
5147
}
5248
});
5349

5450
exportNode.removeSelf();
55-
}
51+
};
5652

5753
const extractExports = css => css.each(node => {
5854
if (node.type === 'rule' && node.selector === ':export') handleExport(node);
@@ -63,5 +59,5 @@ export default plugin('parser', function (opts) {
6359
linkImportedSymbols(css);
6460
extractExports(css);
6561
css.tokens = exportTokens;
66-
}
62+
};
6763
});

test/test-cases.js

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,59 @@
1-
'use strict';
1+
import assert from 'assert';
2+
import fs from 'fs';
3+
import path from 'path';
4+
import FileSystemLoader from 'css-modules-loader-core/lib/file-system-loader';
25

3-
import assert from 'assert'
4-
import fs from 'fs'
5-
import path from 'path'
6-
import FileSystemLoader from 'css-modules-loader-core/lib/file-system-loader'
7-
8-
let normalize = ( str ) => {
9-
return str.replace( /\r\n?/g, '\n' );
10-
}
6+
const normalize = str => {
7+
return str.replace(/\r\n?/g, '\n');
8+
};
119

1210
const pipelines = {
1311
'test-cases': undefined,
14-
'cssi': []
15-
}
12+
'cssi': [],
13+
};
1614

17-
describe('css-modules-loader-core', function () {
15+
describe('css-modules-loader-core', () => {
1816
Object.keys( pipelines ).forEach( dirname => {
1917
describe( dirname, () => {
20-
let testDir = path.join( __dirname, dirname )
18+
const testDir = path.join(__dirname, dirname);
2119

22-
fs.readdirSync( testDir ).forEach( testCase => {
23-
if ( fs.existsSync( path.join( testDir, testCase, 'source.css' ) ) ) {
20+
fs.readdirSync(testDir).forEach(testCase => {
21+
if (fs.existsSync(path.join(testDir, testCase, 'source.css'))) {
2422
it( 'should ' + testCase.replace( /-/g, ' ' ), done => {
25-
let expected = normalize( fs.readFileSync( path.join( testDir, testCase, 'expected.css' ), 'utf-8' ) )
26-
let loader = new FileSystemLoader( testDir, pipelines[dirname] )
27-
let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, 'expected.json' ), 'utf-8' ) )
28-
29-
loader.fetch( `${testCase}/source.css`, '/' ).then( tokens => {
30-
assert.equal( loader.finalSource, expected )
31-
assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) )
32-
} ).then( done, done )
33-
} );
23+
const expected = normalize(fs.readFileSync(path.join(testDir, testCase, 'expected.css'), 'utf-8'));
24+
const loader = new FileSystemLoader(testDir, pipelines[dirname]);
25+
const expectedTokens = JSON.parse(fs.readFileSync(path.join(testDir, testCase, 'expected.json'), 'utf-8'));
26+
27+
loader.fetch(`${testCase}/source.css`, '/').then(tokens => {
28+
assert.equal(loader.finalSource, expected);
29+
assert.equal(JSON.stringify( tokens ), JSON.stringify(expectedTokens));
30+
}).then(done, done);
31+
});
3432
}
35-
} );
36-
} );
37-
} )
33+
});
34+
});
35+
});
3836

3937
// special case for testing multiple sources
4038
describe( 'multiple sources', () => {
41-
let testDir = path.join( __dirname, 'test-cases' )
42-
let testCase = 'multiple-sources';
43-
let dirname = 'test-cases';
39+
const testDir = path.join(__dirname, 'test-cases');
40+
const testCase = 'multiple-sources';
41+
const dirname = 'test-cases';
4442

4543
if ( fs.existsSync( path.join( testDir, testCase, 'source1.css' ) ) ) {
4644
it( 'should ' + testCase.replace( /-/g, ' ' ), done => {
47-
let expected = normalize( fs.readFileSync( path.join( testDir, testCase, 'expected.css' ), 'utf-8' ) )
48-
let loader = new FileSystemLoader( testDir, pipelines[dirname] )
49-
let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, 'expected.json' ), 'utf-8' ) )
45+
const expected = normalize(fs.readFileSync(path.join(testDir, testCase, 'expected.css'), 'utf-8'));
46+
const loader = new FileSystemLoader(testDir, pipelines[dirname]);
47+
const expectedTokens = JSON.parse(fs.readFileSync(path.join(testDir, testCase, 'expected.json'), 'utf-8'));
5048

51-
loader.fetch( `${testCase}/source1.css`, '/' ).then( tokens1 => {
52-
loader.fetch( `${testCase}/source2.css`, '/' ).then( tokens2 => {
53-
assert.equal( loader.finalSource, expected )
49+
loader.fetch(`${testCase}/source1.css`, '/').then(tokens1 => {
50+
loader.fetch(`${testCase}/source2.css`, '/').then(tokens2 => {
51+
assert.equal(loader.finalSource, expected);
5452
const tokens = Object.assign({}, tokens1, tokens2);
55-
assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) )
56-
} ).then( done, done )
57-
})
58-
} );
53+
assert.equal(JSON.stringify(tokens), JSON.stringify(expectedTokens));
54+
}).then(done, done);
55+
});
56+
});
5957
}
60-
} );
58+
});
6159
});

test/test-hook.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
1-
'use strict';
2-
3-
import fs from 'fs'
4-
import path from 'path'
1+
import fs from 'fs';
2+
import path from 'path';
53
import { equal } from 'assert';
64

75
import hook from '../';
86

97
const pipelines = {
108
'test-cases': undefined,
11-
'cssi': []
12-
}
9+
'cssi': [],
10+
};
1311

14-
describe('css-modules-require-hook', function () {
12+
describe('css-modules-require-hook', () => {
1513
Object.keys(pipelines).forEach(dirname => {
1614
describe(dirname, () => {
17-
let testDir = path.join(__dirname, dirname);
15+
const testDir = path.join(__dirname, dirname);
1816

1917
fs.readdirSync(testDir).forEach(testCase => {
2018
if (fs.existsSync(path.join(testDir, testCase, 'source.css'))) {
2119
it('should ' + testCase.replace(/-/g, ' '), () => {
2220
hook({
2321
root: testDir,
24-
use: pipelines[dirname]
22+
use: pipelines[dirname],
2523
});
2624

27-
let expectedTokens = JSON.parse(fs.readFileSync(path.join(testDir, testCase, 'expected.json'), 'utf-8'));
28-
let tokens = require(`${testDir}/${testCase}/source.css`);
25+
const expectedTokens = JSON.parse(fs.readFileSync(path.join(testDir, testCase, 'expected.json'), 'utf-8'));
26+
const tokens = require(`${testDir}/${testCase}/source.css`);
2927

3028
equal(JSON.stringify(tokens), JSON.stringify(expectedTokens));
3129
});
@@ -36,21 +34,21 @@ describe('css-modules-require-hook', function () {
3634

3735
// special case for testing multiple sources
3836
describe('multiple sources', () => {
39-
let testDir = path.join(__dirname, 'test-cases');
40-
let testCase = 'multiple-sources';
41-
let dirname = 'test-cases';
37+
const testDir = path.join(__dirname, 'test-cases');
38+
const testCase = 'multiple-sources';
39+
const dirname = 'test-cases';
4240

4341
if (fs.existsSync(path.join(testDir, testCase, 'source1.css'))) {
4442
it('should ' + testCase.replace(/-/g, ' '), () => {
4543
hook({
4644
root: testDir,
47-
use: pipelines[dirname]
45+
use: pipelines[dirname],
4846
});
4947

50-
let expectedTokens = JSON.parse(fs.readFileSync(path.join(testDir, testCase, 'expected.json'), 'utf-8'));
51-
let tokens1 = require(`${testDir}/${testCase}/source1.css`);
52-
let tokens2 = require(`${testDir}/${testCase}/source2.css`);
53-
let tokens = Object.assign({}, tokens1, tokens2);
48+
const expectedTokens = JSON.parse(fs.readFileSync(path.join(testDir, testCase, 'expected.json'), 'utf-8'));
49+
const tokens1 = require(`${testDir}/${testCase}/source1.css`);
50+
const tokens2 = require(`${testDir}/${testCase}/source2.css`);
51+
const tokens = Object.assign({}, tokens1, tokens2);
5452

5553
equal(JSON.stringify(tokens), JSON.stringify(expectedTokens));
5654
});

0 commit comments

Comments
 (0)