Skip to content

Commit c43d218

Browse files
committed
chore: eslint v7+ tested working
1 parent fa373a9 commit c43d218

10 files changed

Lines changed: 133 additions & 102 deletions

File tree

package-lock.json

Lines changed: 2 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-node/ESLint/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ exports.ESLINT_ERROR_MODULE_NOT_FOUND = "ESLINT_MODULE_NOT_FOUND";
33
exports.ESLINT_ERROR_LINT_FAILED = "ESLINT_LINT_FAILED";
44

55
exports.OPERATION_LINT_TEXT = "LINT_TEXT";
6+
exports.OPERATION_GET_LOADED_VERSION = "GET_LOADED_VERSION";
67
exports.OPERATION_QUIT = "QUIT";
78
exports.OPERATION_RESPONSE = "ESLINT_RESPONSE";
89

src-node/ESLint/runner.js

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const readline = require('readline');
22
const path = require('path');
33
const fsPromises = require('fs').promises;
44
const {ESLINT_ERROR_LINT_FAILED, ESLINT_ERROR_MODULE_LOAD_FAILED, ESLINT_ERROR_MODULE_NOT_FOUND,
5-
OPERATION_LINT_TEXT, OPERATION_QUIT, OPERATION_RESPONSE}
5+
OPERATION_LINT_TEXT, OPERATION_QUIT, OPERATION_RESPONSE, OPERATION_GET_LOADED_VERSION
6+
}
67
= require("./constants");
78

89
if (!process.argv[2]) {
@@ -34,38 +35,20 @@ async function checkExists(directoryPath, isDir = true) {
3435
}
3536

3637
// Dynamically require the ESLint module
37-
let ESLintModule;
38-
// if the user changed his eslint version while we were caching an old eslint version, we need to update.
39-
// Function to observe version changes in ESLint
40-
async function observeVersionChanges() {
41-
try {
42-
const packageJsonPath = path.join(ESLintModulePath, 'package.json');
43-
const packageJson = await fsPromises.readFile(packageJsonPath, 'utf8');
44-
const packageData = JSON.parse(packageJson);
45-
const currentEsLintModuleVersion = packageData.version;
46-
47-
if (ESLintModule && currentEsLintModuleVersion && ESLintModule.version !== currentEsLintModuleVersion) {
48-
console.error('ESLint runner: ESLint version has changed. Requesting restart with new version...');
49-
process.exit(0);
50-
}
51-
} catch (error) {
52-
console.error('ESLint runner: Error reading ESLint version:', error.message);
53-
}
54-
}
38+
let ESLintCached;
5539

5640
async function getESLintModule() {
57-
if(ESLintModule){
58-
observeVersionChanges();
59-
return ESLintModule;
41+
if(ESLintCached){
42+
return ESLintCached;
6043
}
6144
try{
6245
const directoryExists = await checkExists(ESLintModulePath);
6346
if(!directoryExists){
6447
return null;
6548
}
66-
const {ESLint} = require(ESLintModulePath);
67-
ESLintModule = ESLint;
68-
return ESLint;
49+
const ESLintModule = require(ESLintModulePath);
50+
ESLintCached = ESLintModule.ESLint;
51+
return ESLintCached;
6952
} catch (e) {
7053
console.error("ESLint runner: failed to load ESLintModule");
7154
}
@@ -115,7 +98,8 @@ async function lintTextWithPath(text, fullFilePath) {
11598
if(lintFilePath) {
11699
const text = fs.readFileSync(lintFilePath, { encoding: 'utf8' });
117100
lintTextWithPath(text, lintFilePath)
118-
.then(console.log);
101+
.then(console.log)
102+
.catch(console.error);
119103
}
120104

121105
const rl = readline.createInterface({
@@ -149,6 +133,12 @@ rl.on('line', (input) => {
149133
});
150134
} else if(eslintRequest.operation === OPERATION_QUIT) {
151135
process.exit(0);
136+
} else if(eslintRequest.operation === OPERATION_GET_LOADED_VERSION) {
137+
sendToPHNode({
138+
operation: OPERATION_RESPONSE,
139+
requestID: eslintRequest.requestID,
140+
esLintVersion: ESLintCached && ESLintCached.version
141+
});
152142
} else {
153143
console.error("ESLint runner: Unknown operation: ", eslintRequest);
154144
}

src-node/ESLint/service.js

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const { spawn, exec } = require('child_process');
22
const readline = require('readline');
33
const path = require('path');
4-
const {OPERATION_RESPONSE, OPERATION_QUIT, OPERATION_LINT_TEXT}
4+
const fsPromises = require('fs').promises;
5+
const {OPERATION_RESPONSE, OPERATION_QUIT, OPERATION_LINT_TEXT, OPERATION_GET_LOADED_VERSION}
56
= require("./constants");
67

78
let requestID = 1;
@@ -77,14 +78,15 @@ async function createESLintService(projectFullPath) {
7778
const eslintResponse = JSON.parse(line);
7879
if(eslintResponse.operation === OPERATION_RESPONSE) {
7980
const sentRequestID = eslintResponse.requestID;
80-
const promiseResoover = queuedReq.get(sentRequestID);
81-
if(!promiseResoover){
81+
const promiseResolver = queuedReq.get(sentRequestID);
82+
if(!promiseResolver){
8283
console.error("ESLint service: request ID not found to process request!!: ", eslintResponse);
8384
return;
8485
}
86+
queuedReq.delete(sentRequestID);
8587
delete eslintResponse.requestID;
8688
delete eslintResponse.operation;
87-
promiseResoover.resolve(eslintResponse);
89+
promiseResolver.resolve(eslintResponse);
8890
} else {
8991
console.error("ESLint service: Unknown operation: ", eslintResponse);
9092
}
@@ -103,7 +105,50 @@ async function createESLintService(projectFullPath) {
103105
});
104106
}
105107

108+
async function _getESLintLoadedVersion() {
109+
if(!eslintServiceProcess){
110+
return null;
111+
}
112+
return new Promise((resolve, reject) => {
113+
sendToESLintProcess({
114+
requestID: newRequestCallback(resolve, reject),
115+
operation: OPERATION_GET_LOADED_VERSION
116+
});
117+
});
118+
}
119+
120+
// if the user changed his eslint version while we were caching an old eslint version, we need to update.
121+
// Function to observe version changes in ESLint
122+
async function observeVersionChanges(projectFullPath) {
123+
try {
124+
if(!eslintServiceProcess){
125+
return;
126+
}
127+
const currentlyLoadedVersion = await _getESLintLoadedVersion();
128+
if(!currentlyLoadedVersion || !currentlyLoadedVersion.esLintVersion){
129+
return;
130+
}
131+
132+
const ESLintModulePath = path.join(projectFullPath, "node_modules", "eslint");
133+
const packageJsonPath = path.join(ESLintModulePath, 'package.json');
134+
const packageJson = await fsPromises.readFile(packageJsonPath, 'utf8');
135+
const packageData = JSON.parse(packageJson);
136+
const currentEsLintModuleVersion = packageData.version;
137+
if (currentEsLintModuleVersion && currentlyLoadedVersion.esLintVersion !== currentEsLintModuleVersion) {
138+
console.log(`ESLint runner: ESLint version has changed from ${currentlyLoadedVersion.esLintVersion}`+
139+
` to ${currentEsLintModuleVersion}. Restarting with new version...`);
140+
sendToESLintProcess({
141+
operation: OPERATION_QUIT
142+
});
143+
eslintServiceProcess = null;
144+
}
145+
} catch (error) {
146+
console.error('ESLint runner: Error reading ESLint version:', error.message);
147+
}
148+
}
149+
106150
async function lintFile(text, fullFilePath, projectFullPath) {
151+
observeVersionChanges(projectFullPath);
107152
if(currentProjectPath !== projectFullPath) {
108153
// on project change, we should create a new es linter
109154
currentProjectPath = projectFullPath;

src-node/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)