Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/stylelint.ts
4770 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import es from 'event-stream';
7
import vfs from 'vinyl-fs';
8
import { stylelintFilter } from './filters.ts';
9
import { getVariableNameValidator } from './lib/stylelint/validateVariableNames.ts';
10
11
interface FileWithLines {
12
__lines?: string[];
13
relative: string;
14
contents: Buffer;
15
}
16
17
type Reporter = (message: string, isError: boolean) => void;
18
19
/**
20
* Stylelint gulpfile task
21
*/
22
export default function gulpstylelint(reporter: Reporter): NodeJS.ReadWriteStream {
23
const variableValidator = getVariableNameValidator();
24
let errorCount = 0;
25
const monacoWorkbenchPattern = /\.monaco-workbench/;
26
const restrictedPathPattern = /^src[\/\\]vs[\/\\](base|platform|editor)[\/\\]/;
27
const layerCheckerDisablePattern = /\/\*\s*stylelint-disable\s+layer-checker\s*\*\//;
28
29
return es.through(function (this, file: FileWithLines) {
30
const lines = file.__lines || file.contents.toString('utf8').split(/\r\n|\r|\n/);
31
file.__lines = lines;
32
33
const isRestrictedPath = restrictedPathPattern.test(file.relative);
34
35
// Check if layer-checker is disabled for the entire file
36
const isLayerCheckerDisabled = lines.some(line => layerCheckerDisablePattern.test(line));
37
38
lines.forEach((line, i) => {
39
variableValidator(line, (unknownVariable: string) => {
40
reporter(file.relative + '(' + (i + 1) + ',1): Unknown variable: ' + unknownVariable, true);
41
errorCount++;
42
});
43
44
if (isRestrictedPath && !isLayerCheckerDisabled && monacoWorkbenchPattern.test(line)) {
45
reporter(file.relative + '(' + (i + 1) + ',1): The class .monaco-workbench cannot be used in files under src/vs/{base,platform,editor} because only src/vs/workbench applies it', true);
46
errorCount++;
47
}
48
});
49
50
this.emit('data', file);
51
}, function () {
52
if (errorCount > 0) {
53
reporter('All valid variable names are in `build/lib/stylelint/vscode-known-variables.json`\nTo update that file, run `./scripts/test-documentation.sh|bat.`', false);
54
}
55
this.emit('end');
56
});
57
}
58
59
function stylelint(): NodeJS.ReadWriteStream {
60
return vfs
61
.src(Array.from(stylelintFilter), { base: '.', follow: true, allowEmpty: true })
62
.pipe(gulpstylelint((message, isError) => {
63
if (isError) {
64
console.error(message);
65
} else {
66
console.info(message);
67
}
68
}))
69
.pipe(es.through(function () { /* noop, important for the stream to end */ }));
70
}
71
72
if (import.meta.main) {
73
stylelint().on('error', (err: Error) => {
74
console.error();
75
console.error(err);
76
process.exit(1);
77
});
78
}
79
80