File size: 3,085 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const childProcess = require( 'child_process' );
const fs = require( 'fs' );
const path = require( 'path' );

const componentsStyle = fs.readFileSync( 'assets/stylesheets/_components.scss', {
	encoding: 'utf8',
} );
const components = componentsStyle
	.split( '\n' )
	.filter( ( line ) => line.startsWith( '@import' ) )
	.map( ( component ) => component.substring( 9, component.length - 2 ) );
console.log( `Scoring ${ components.length } components...` );

const zero = { score: 0 };

function hasImports( f ) {
	if ( f.includes( '@import' ) ) {
		return {
			score: 5,
			name: 'contains @import',
		};
	}
	return zero;
}

function hasNonCompliantToplevelSelectors( f, name ) {
	let topLevelSelectors;
	const re = /^\.([\w_\-.]+)/gm;
	let violations = 0;
	while ( ( topLevelSelectors = re.exec( f ) ) !== null ) {
		const classes = topLevelSelectors[ 0 ].split( '.' ).filter( Boolean );

		if ( ! classes.some( ( cls ) => cls.startsWith( name ) ) ) {
			// suspect
			//console.log( '  saw %s\n  expected %s', topLevelSelectors[0], name );
			++violations;
		}
	}

	if ( violations ) {
		return {
			score: violations,
			name: 'non-compliant top level selectors',
		};
	}

	return zero;
}

function overridenByOthers( f, name, componentPath ) {
	const results = childProcess.spawnSync(
		'git',
		[
			'grep',
			'-l',
			'-F',
			`.${ name }`,
			'--',
			`"*.scss"`,
			`":^${ componentPath }"`,
			':^assets/stylesheets/shared/functions/_z-index.scss',
		],
		{ encoding: 'utf8', shell: true }
	);
	const r = results.stdout.split( '\n' );
	const componentsFullPath = components.map( ( c ) => 'client/' + c + '.scss' );
	const matches = r.filter( ( p ) => componentsFullPath.includes( p ) );
	const matchCount = matches.length;
	if ( matchCount > 0 ) {
		return {
			score: matchCount,
			name: `styles overriden by ${ matchCount } consumers:\n\t${ matches.join( '\n\t' ) }`,
		};
	}
	return zero;
}

function score( c ) {
	const styles = fs.readFileSync( 'client/' + c + '.scss', { encoding: 'utf8' } );
	const name = path.basename( c.replace( /\/style$/, '' ) );
	const componentPath = `client/${ path.dirname( c ) }/`;
	const checks = [ hasImports, hasNonCompliantToplevelSelectors, overridenByOthers ];
	const scores = checks.map( ( check ) => check( styles, name, componentPath ) );
	scores.score = scores.reduce( ( totalScore, { score: s } ) => totalScore + s, 0 );
	scores.summary = scores
		.filter( ( s ) => s.name )
		.map( ( s ) => s.name )
		.join( ', ' );
	return scores;
}

const scored = components.map( ( c ) => ( { component: c, scores: score( c ) } ) );

scored.sort( ( a, b ) => {
	const scoreDiff = b.scores.score - a.scores.score;
	if ( scoreDiff ) {
		return scoreDiff;
	}
	return a.component.localeCompare( b.component, 'en', { sensitivity: 'base' } );
} );

let currentScore = null;

for ( const s of scored ) {
	if ( currentScore !== s.scores.score ) {
		console.log( '' );
		console.log( 'SCORE:', s.scores.score );
		currentScore = s.scores.score;
	}
	console.log( s.component );
	if ( s.scores.summary ) {
		console.log( '  ', s.scores.summary );
	}
}