Skip to content
This repository was archived by the owner on Apr 30, 2021. It is now read-only.

Commit 967a95a

Browse files
committed
Script to fetch usages in scss files and merge
* Find all usages in scss files and get the line number. * Script to merge these with usages collected with postcss, which contain some info that the scss script can't access.
1 parent d433388 commit 967a95a

File tree

6 files changed

+86
-19
lines changed

6 files changed

+86
-19
lines changed

index.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,15 +306,5 @@ module.exports = postcss.plugin("postcss-css-variables", function(options) {
306306
opts.exportVarUsagesTo[ varName ].usages.push( ...allVars[ varName ].usages );
307307
} );
308308
}
309-
310-
//console.log('map', map);
311-
312-
/* * /
313-
}
314-
catch(e) {
315-
//console.log('e', e.message);
316-
console.log('e', e.message, e.stack);
317-
}
318-
/* */
319309
};
320310
});

lib/balanced-var.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const balanced = require( 'balanced-match' );
33
// Check for balanced `var(` and `)` pairs inside `value`, and return the 3 fragments:
44
// `body` (inside), `pre` (before), `post` (after) of the found wrapper
55
function balancedVar(value) {
6-
var match = balanced('(', ')', value)
6+
const match = balanced( '(', ')', value );
77
if ( !match ) {
88
return;
99
}
@@ -26,7 +26,7 @@ function balancedVar(value) {
2626
};
2727
}
2828
// Check inside post
29-
var postMatch = balancedVar( match.post );
29+
const postMatch = balancedVar( match.post );
3030

3131
if ( postMatch ) {
3232
// Reconstruct pre

lib/merge-var-usages.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const mergeVarUsages = ( inCss, inScss ) => Object.keys( inCss ).map( cssVar => ({
2+
...inCss[ cssVar ],
3+
sourceUsages: ! inScss[ cssVar ] ? null : inScss[ cssVar ].usages,
4+
}));
5+
6+
module.exports = mergeVarUsages;

lib/resolve-value.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,9 @@ const resolveValue = ( decl, map, { ignorePseudoScope = false, _debugIsInternal
3838
collectVar( {
3939
name: variableName,
4040
usage: {
41-
defaultValue: defaultValue.join(),
42-
// declaration: decl,
43-
property: decl.prop,
4441
selector: decl.parent.selector,
45-
// // Best name I could fine for what is the result of balancedVar: it contains the part before, inside
46-
// // and after the inside of the var statement, from which we know the position inside of the line.
47-
// linePosition: match,
42+
property: decl.prop,
43+
defaultValue: defaultValue.join(''),
4844
},
4945
} );
5046
}

lib/scss-var-usages.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const shallowEqualObjects = require( "shallow-equal/objects");
2+
const fs = require('fs');
3+
const glob = require('glob');
4+
const balancedVar = require( './balanced-var' );
5+
6+
const grabVars = ( value ) => {
7+
let remainingVariableValue = value;
8+
let match;
9+
const lineVars = [];
10+
11+
while ( (match = balancedVar( remainingVariableValue )) ) {
12+
// Split at the comma to find variable name and fallback value
13+
// There may be other commas in the values so this isn't necessarily just 2 pieces
14+
const arguments = match.body.split( ',' ).map( str => str.trim() );
15+
16+
const [variableName, ...defaultValue] = arguments;
17+
18+
lineVars.push( {
19+
name: variableName,
20+
defaultValue: defaultValue.join(),
21+
} );
22+
23+
remainingVariableValue = (match.pre || '') + match.body.replace( variableName, '' ) + (match.post || '');
24+
}
25+
26+
return lineVars;
27+
}
28+
29+
function scssVarUsages( sourceDir ) {
30+
// Store values as we iterate
31+
const allVars = {};
32+
33+
const scssFiles = glob.sync(`${sourceDir.replace(/\/$/, '')}/**/*.scss`);
34+
// Create an array of all the *.module.SCSS files and loop through it
35+
scssFiles.forEach((file) => {
36+
// Read the file, convert to string and then split each line
37+
const scssLines = fs.readFileSync(file).toString().split('\n');
38+
39+
let lineNumber = 1;
40+
41+
scssLines.forEach( line => {
42+
lineNumber++;
43+
const cssValueRgx = /^\s*([\w\-]+): (.*)/;
44+
const declarationParts = cssValueRgx.exec( line );
45+
if ( declarationParts === null ) {
46+
return;
47+
}
48+
const [, property, value] = declarationParts;
49+
50+
const lineVars = grabVars( value ).map( cssVar => ({
51+
file,
52+
lineNumber,
53+
property,
54+
...cssVar,
55+
}) );
56+
57+
lineVars.forEach( cssVar => {
58+
const { name, ...usage } = cssVar;
59+
if ( !allVars[ name ] ) {
60+
allVars[ name ] = {
61+
usages: [],
62+
};
63+
}
64+
if ( ! allVars[ name ].usages.some( existingUsage => shallowEqualObjects( existingUsage, usage ) ) ) {
65+
allVars[ name ].usages.push( usage );
66+
}
67+
} );
68+
});
69+
});
70+
71+
return allVars;
72+
}
73+
74+
module.exports = scssVarUsages;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"balanced-match": "^1.0.0",
1818
"escape-string-regexp": "^1.0.3",
1919
"extend": "^3.0.1",
20-
"postcss": "^6.0.8"
20+
"postcss": "^6.0.8",
21+
"shallow-equal": "^1.2.1"
2122
},
2223
"devDependencies": {
2324
"bluebird": "^3.5.0",

0 commit comments

Comments
 (0)