Skip to content

Commit

Permalink
write our own little gulp-eslint which takes the eslint from our work…
Browse files Browse the repository at this point in the history
…space root (#230115)
  • Loading branch information
sandy081 authored Sep 30, 2024
1 parent ef44e67 commit 841d51d
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 1,239 deletions.
10 changes: 2 additions & 8 deletions build/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@ const vfs = require('vinyl-fs');
const { eslintFilter } = require('./filters');

function eslint() {
const gulpeslint = require('gulp-eslint');
const eslint = require('./gulp-eslint');
return vfs
.src(eslintFilter, { base: '.', follow: true, allowEmpty: true })
.pipe(
gulpeslint({
configFile: '.eslintrc.json'
})
)
.pipe(gulpeslint.formatEach('compact'))
.pipe(
gulpeslint.results((results) => {
eslint((results) => {
if (results.warningCount > 0 || results.errorCount > 0) {
throw new Error('eslint failed with warnings and/or errors');
}
Expand Down
78 changes: 78 additions & 0 deletions build/gulp-eslint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

'use strict';

const { ESLint } = require('eslint');
const { Transform } = require('stream');
const { relative } = require('path');
const fancyLog = require('fancy-log');

/**
* @param {Function} action - A function to handle all ESLint results
* @returns {stream} gulp file stream
*/
function eslint(action) {
const linter = new ESLint();
const formatter = linter.loadFormatter('compact');

const results = [];
results.errorCount = 0;
results.warningCount = 0;

return transform(
async (file, enc, cb) => {
const filePath = relative(process.cwd(), file.path);

if (file.isNull()) {
cb(null, file);
return;
}

if (file.isStream()) {
cb(new Error('vinyl files with Stream contents are not supported'));
return;
}

try {
// TODO: Should this be checked?
if (await linter.isPathIgnored(filePath)) {
cb(null, file);
return;
}

const result = (await linter.lintText(file.contents.toString(), { filePath }))[0];
results.push(result);
results.errorCount += result.errorCount;
results.warningCount += result.warningCount;

const message = (await formatter).format([result]);
if (message) {
fancyLog(message);
}
cb(null, file);
} catch (error) {
cb(error);
}
},
(done) => {
try {
action(results);
done();
} catch (error) {
done(error);
}
});
}

function transform(transform, flush) {
return new Transform({
objectMode: true,
transform,
flush
});
}

module.exports = eslint;
10 changes: 2 additions & 8 deletions build/hygiene.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const copyrightHeaderLines = [
];

function hygiene(some, linting = true) {
const gulpeslint = require('gulp-eslint');
const eslint = require('./gulp-eslint');
const gulpstylelint = require('./stylelint');
const formatter = require('./lib/formatter');

Expand Down Expand Up @@ -172,13 +172,7 @@ function hygiene(some, linting = true) {
result
.pipe(filter(eslintFilter))
.pipe(
gulpeslint({
configFile: '.eslintrc.json'
})
)
.pipe(gulpeslint.formatEach('compact'))
.pipe(
gulpeslint.results((results) => {
eslint((results) => {
errorCount += results.warningCount;
errorCount += results.errorCount;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ export class ActiveLineMarker {
this._update(previous && (previous.codeElement || previous.element));
}

_update(before: HTMLElement | undefined) {
private _update(before: HTMLElement | undefined) {
this._unmarkActiveElement(this._current);
this._markActiveElement(before);
this._current = before;
}

_unmarkActiveElement(element: HTMLElement | undefined) {
private _unmarkActiveElement(element: HTMLElement | undefined) {
if (!element) {
return;
}
element.classList.toggle('code-active-line', false);
}

_markActiveElement(element: HTMLElement | undefined) {
private _markActiveElement(element: HTMLElement | undefined) {
if (!element) {
return;
}
Expand Down
34 changes: 17 additions & 17 deletions extensions/markdown-language-features/preview-src/csp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,45 @@ import { getStrings } from './strings';
* Shows an alert when there is a content security policy violation.
*/
export class CspAlerter {
private didShow = false;
private didHaveCspWarning = false;
private _didShow = false;
private _didHaveCspWarning = false;

private messaging?: MessagePoster;
private _messaging?: MessagePoster;

constructor(
private readonly settingsManager: SettingsManager,
private readonly _settingsManager: SettingsManager,
) {
document.addEventListener('securitypolicyviolation', () => {
this.onCspWarning();
this._onCspWarning();
});

window.addEventListener('message', (event) => {
if (event && event.data && event.data.name === 'vscode-did-block-svg') {
this.onCspWarning();
this._onCspWarning();
}
});
}

public setPoster(poster: MessagePoster) {
this.messaging = poster;
if (this.didHaveCspWarning) {
this.showCspWarning();
this._messaging = poster;
if (this._didHaveCspWarning) {
this._showCspWarning();
}
}

private onCspWarning() {
this.didHaveCspWarning = true;
this.showCspWarning();
private _onCspWarning() {
this._didHaveCspWarning = true;
this._showCspWarning();
}

private showCspWarning() {
private _showCspWarning() {
const strings = getStrings();
const settings = this.settingsManager.settings;
const settings = this._settingsManager.settings;

if (this.didShow || settings.disableSecurityWarnings || !this.messaging) {
if (this._didShow || settings.disableSecurityWarnings || !this._messaging) {
return;
}
this.didShow = true;
this._didShow = true;

const notification = document.createElement('a');
notification.innerText = strings.cspAlertMessageText;
Expand All @@ -59,7 +59,7 @@ export class CspAlerter {
notification.setAttribute('role', 'button');
notification.setAttribute('aria-label', strings.cspAlertMessageLabel);
notification.onclick = () => {
this.messaging!.postMessage('showPreviewSecuritySelector', { source: settings.source });
this._messaging!.postMessage('showPreviewSecuritySelector', { source: settings.source });
};
document.body.appendChild(notification);
}
Expand Down
20 changes: 10 additions & 10 deletions extensions/markdown-language-features/preview-src/loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import { MessagePoster } from './messaging';

export class StyleLoadingMonitor {
private unloadedStyles: string[] = [];
private finishedLoading: boolean = false;
private _unloadedStyles: string[] = [];
private _finishedLoading: boolean = false;

private poster?: MessagePoster;
private _poster?: MessagePoster;

constructor() {
const onStyleLoadError = (event: any) => {
const source = event.target.dataset.source;
this.unloadedStyles.push(source);
this._unloadedStyles.push(source);
};

window.addEventListener('DOMContentLoaded', () => {
Expand All @@ -25,18 +25,18 @@ export class StyleLoadingMonitor {
});

window.addEventListener('load', () => {
if (!this.unloadedStyles.length) {
if (!this._unloadedStyles.length) {
return;
}
this.finishedLoading = true;
this.poster?.postMessage('previewStyleLoadError', { unloadedStyles: this.unloadedStyles });
this._finishedLoading = true;
this._poster?.postMessage('previewStyleLoadError', { unloadedStyles: this._unloadedStyles });
});
}

public setPoster(poster: MessagePoster): void {
this.poster = poster;
if (this.finishedLoading) {
poster.postMessage('previewStyleLoadError', { unloadedStyles: this.unloadedStyles });
this._poster = poster;
if (this._finishedLoading) {
poster.postMessage('previewStyleLoadError', { unloadedStyles: this._unloadedStyles });
}
}
}
Loading

0 comments on commit 841d51d

Please sign in to comment.