Skip to content

Commit e017f1c

Browse files
TrySoundmightyaleksey
authored andcommitted
Add jest and prettier (#145)
1 parent 7f4028c commit e017f1c

15 files changed

+3634
-277
lines changed

.eslintrc

Lines changed: 0 additions & 23 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
.DS_Store
1+
node_modules
22
coverage
33
lib
4-
node_modules
5-
yarn.lock

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ node_js:
33
- "4"
44
- "6"
55
- "node"
6-
script: npm run travis
76

87
after_success:
98
- cat ./coverage/lcov.info | node_modules/.bin/coveralls --verbose

package.json

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,32 @@
33
"version": "1.2.0",
44
"description": "A CSS Modules transform to extract local aliases for inline imports",
55
"main": "lib/index.js",
6+
"files": [
7+
"lib"
8+
],
69
"scripts": {
7-
"lint": "eslint src",
810
"build": "babel --out-dir lib src",
9-
"watch": "chokidar src -c 'npm run build'",
10-
"posttest": "npm run lint && npm run build",
11-
"test": "mocha --compilers js:babel/register",
12-
"autotest": "chokidar src test -c 'npm test'",
13-
"precover": "npm run lint && npm run build",
14-
"cover": "babel-istanbul cover node_modules/.bin/_mocha",
15-
"travis": "npm run cover -- --report lcovonly",
16-
"prepublish": "npm run build"
11+
"test": "jest --coverage",
12+
"precommit": "lint-staged",
13+
"prepublish": "yarn run test && yarn run build"
14+
},
15+
"lint-staged": {
16+
"*.js": [
17+
"prettier --single-quote --no-semi --write",
18+
"git add"
19+
]
20+
},
21+
"babel": {
22+
"presets": [
23+
[
24+
"env",
25+
{
26+
"targets": {
27+
"node": 4
28+
}
29+
}
30+
]
31+
]
1732
},
1833
"repository": {
1934
"type": "git",
@@ -24,9 +39,6 @@
2439
"postcss",
2540
"plugin"
2641
],
27-
"files": [
28-
"lib"
29-
],
3042
"author": "Glen Maddern",
3143
"license": "ISC",
3244
"bugs": {
@@ -37,14 +49,15 @@
3749
"postcss": "^6.0.1"
3850
},
3951
"devDependencies": {
40-
"babel": "^5.4.7",
52+
"babel-cli": "^6.24.1",
4153
"babel-eslint": "^7.2.2",
42-
"babel-istanbul": "^0.4.0",
43-
"babelify": "^7.3.0",
44-
"chokidar-cli": "^1.0.1",
54+
"babel-jest": "^20.0.3",
55+
"babel-preset-env": "^1.5.1",
4556
"codecov.io": "^0.1.2",
4657
"coveralls": "^2.11.2",
47-
"eslint": "^1.5.0",
48-
"mocha": "^3.1.2"
58+
"husky": "^0.13.3",
59+
"jest": "^20.0.3",
60+
"lint-staged": "^3.4.2",
61+
"prettier": "^1.3.1"
4962
}
5063
}

src/index.js

Lines changed: 87 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import postcss from 'postcss';
2-
import topologicalSort from './topologicalSort';
1+
const postcss = require('postcss')
2+
const topologicalSort = require('./topologicalSort')
33

4-
const declWhitelist = ['composes'];
5-
const declFilter = new RegExp( `^(${declWhitelist.join( '|' )})$` );
6-
const matchImports = /^(.+?)\s+from\s+(?:"([^"]+)"|'([^']+)'|(global))$/;
7-
const icssImport = /^:import\((?:"([^"]+)"|'([^']+)')\)/;
4+
const declWhitelist = ['composes']
5+
const declFilter = new RegExp(`^(${declWhitelist.join('|')})$`)
6+
const matchImports = /^(.+?)\s+from\s+(?:"([^"]+)"|'([^']+)'|(global))$/
7+
const icssImport = /^:import\((?:"([^"]+)"|'([^']+)')\)/
88

9-
const VISITED_MARKER = 1;
9+
const VISITED_MARKER = 1
1010

1111
function createParentName(rule, root) {
12-
return `__${root.index(rule.parent)}_${rule.selector}`;
12+
return `__${root.index(rule.parent)}_${rule.selector}`
1313
}
1414

1515
function serializeImports(imports) {
16-
return imports.map(importPath => '`' + importPath + '`').join(', ');
16+
return imports.map(importPath => '`' + importPath + '`').join(', ')
1717
}
1818

1919
/**
@@ -38,132 +38,143 @@ function serializeImports(imports) {
3838
* }
3939
*/
4040
function addImportToGraph(importId, parentId, graph, visited) {
41-
const siblingsId = parentId + '_' + 'siblings';
42-
const visitedId = parentId + '_' + importId;
41+
const siblingsId = parentId + '_' + 'siblings'
42+
const visitedId = parentId + '_' + importId
4343

4444
if (visited[visitedId] !== VISITED_MARKER) {
45-
if (!Array.isArray(visited[siblingsId])) visited[siblingsId] = [];
45+
if (!Array.isArray(visited[siblingsId])) visited[siblingsId] = []
4646

47-
const siblings = visited[siblingsId];
47+
const siblings = visited[siblingsId]
4848

4949
if (Array.isArray(graph[importId]))
50-
graph[importId] = graph[importId].concat(siblings);
51-
else
52-
graph[importId] = siblings.slice();
50+
graph[importId] = graph[importId].concat(siblings)
51+
else graph[importId] = siblings.slice()
5352

54-
visited[visitedId] = VISITED_MARKER;
55-
siblings.push(importId);
53+
visited[visitedId] = VISITED_MARKER
54+
siblings.push(importId)
5655
}
5756
}
5857

59-
const processor = postcss.plugin('modules-extract-imports', function (options = {}) {
60-
const failOnWrongOrder = options.failOnWrongOrder;
58+
module.exports = postcss.plugin('modules-extract-imports', function(
59+
options = {}
60+
) {
61+
const failOnWrongOrder = options.failOnWrongOrder
6162

6263
return css => {
63-
const graph = {};
64-
const visited = {};
64+
const graph = {}
65+
const visited = {}
6566

66-
const existingImports = {};
67-
const importDecls = {};
68-
const imports = {};
67+
const existingImports = {}
68+
const importDecls = {}
69+
const imports = {}
6970

70-
let importIndex = 0;
71+
let importIndex = 0
7172

7273
const createImportedName = typeof options.createImportedName !== 'function'
73-
? (importName/*, path*/) => `i__imported_${importName.replace(/\W/g, '_')}_${importIndex++}`
74-
: options.createImportedName;
74+
? (importName /*, path*/) =>
75+
`i__imported_${importName.replace(/\W/g, '_')}_${importIndex++}`
76+
: options.createImportedName
7577

7678
// Check the existing imports order and save refs
7779
css.walkRules(rule => {
78-
const matches = icssImport.exec(rule.selector);
80+
const matches = icssImport.exec(rule.selector)
7981

8082
if (matches) {
81-
const [/*match*/, doubleQuotePath, singleQuotePath] = matches;
82-
const importPath = doubleQuotePath || singleQuotePath;
83+
const [, /*match*/ doubleQuotePath, singleQuotePath] = matches
84+
const importPath = doubleQuotePath || singleQuotePath
8385

84-
addImportToGraph(importPath, 'root', graph, visited);
86+
addImportToGraph(importPath, 'root', graph, visited)
8587

86-
existingImports[importPath] = rule;
88+
existingImports[importPath] = rule
8789
}
88-
});
90+
})
8991

9092
// Find any declaration that supports imports
9193
css.walkDecls(declFilter, decl => {
92-
let matches = decl.value.match(matchImports);
93-
let tmpSymbols;
94+
let matches = decl.value.match(matchImports)
95+
let tmpSymbols
9496

9597
if (matches) {
96-
let [/*match*/, symbols, doubleQuotePath, singleQuotePath, global] = matches;
98+
let [
99+
,
100+
/*match*/ symbols,
101+
doubleQuotePath,
102+
singleQuotePath,
103+
global
104+
] = matches
97105

98106
if (global) {
99107
// Composing globals simply means changing these classes to wrap them in global(name)
100-
tmpSymbols = symbols.split(/\s+/).map(s => `global(${s})`);
108+
tmpSymbols = symbols.split(/\s+/).map(s => `global(${s})`)
101109
} else {
102-
const importPath = doubleQuotePath || singleQuotePath;
103-
const parentRule = createParentName(decl.parent, css);
110+
const importPath = doubleQuotePath || singleQuotePath
111+
const parentRule = createParentName(decl.parent, css)
104112

105-
addImportToGraph(importPath, parentRule, graph, visited);
113+
addImportToGraph(importPath, parentRule, graph, visited)
106114

107-
importDecls[importPath] = decl;
108-
imports[importPath] = imports[importPath] || {};
115+
importDecls[importPath] = decl
116+
imports[importPath] = imports[importPath] || {}
109117

110118
tmpSymbols = symbols.split(/\s+/).map(s => {
111119
if (!imports[importPath][s]) {
112-
imports[importPath][s] = createImportedName(s, importPath);
120+
imports[importPath][s] = createImportedName(s, importPath)
113121
}
114122

115-
return imports[importPath][s];
116-
});
123+
return imports[importPath][s]
124+
})
117125
}
118126

119-
decl.value = tmpSymbols.join(' ');
127+
decl.value = tmpSymbols.join(' ')
120128
}
121-
});
129+
})
122130

123-
const importsOrder = topologicalSort(graph, failOnWrongOrder);
131+
const importsOrder = topologicalSort(graph, failOnWrongOrder)
124132

125133
if (importsOrder instanceof Error) {
126-
const importPath = importsOrder.nodes.find(importPath => importDecls.hasOwnProperty(importPath));
127-
const decl = importDecls[importPath];
134+
const importPath = importsOrder.nodes.find(importPath =>
135+
importDecls.hasOwnProperty(importPath)
136+
)
137+
const decl = importDecls[importPath]
128138

129-
const errMsg = 'Failed to resolve order of composed modules ' + serializeImports(importsOrder.nodes) + '.';
139+
const errMsg =
140+
'Failed to resolve order of composed modules ' +
141+
serializeImports(importsOrder.nodes) +
142+
'.'
130143

131144
throw decl.error(errMsg, {
132145
plugin: 'modules-extract-imports',
133-
word: 'composes',
134-
});
146+
word: 'composes'
147+
})
135148
}
136149

137-
let lastImportRule;
150+
let lastImportRule
138151
importsOrder.forEach(path => {
139-
const importedSymbols = imports[path];
140-
let rule = existingImports[path];
152+
const importedSymbols = imports[path]
153+
let rule = existingImports[path]
141154

142155
if (!rule && importedSymbols) {
143156
rule = postcss.rule({
144157
selector: `:import("${path}")`,
145-
raws: {after: '\n'},
146-
});
158+
raws: { after: '\n' }
159+
})
147160

148-
if (lastImportRule)
149-
css.insertAfter(lastImportRule, rule);
150-
else
151-
css.prepend(rule);
161+
if (lastImportRule) css.insertAfter(lastImportRule, rule)
162+
else css.prepend(rule)
152163
}
153164

154-
lastImportRule = rule;
165+
lastImportRule = rule
155166

156-
if (!importedSymbols) return;
167+
if (!importedSymbols) return
157168

158169
Object.keys(importedSymbols).forEach(importedSymbol => {
159-
rule.append(postcss.decl({
160-
value: importedSymbol,
161-
prop: importedSymbols[importedSymbol],
162-
raws: {before: '\n '},
163-
}));
164-
});
165-
});
166-
};
167-
});
168-
169-
export default processor;
170+
rule.append(
171+
postcss.decl({
172+
value: importedSymbol,
173+
prop: importedSymbols[importedSymbol],
174+
raws: { before: '\n ' }
175+
})
176+
)
177+
})
178+
})
179+
}
180+
})

0 commit comments

Comments
 (0)