/* eslint-disable */
const {
  createMessageConnection,
  StreamMessageReader,
  StreamMessageWriter,
} = require("vscode-jsonrpc");

const { exec } = require("child_process");

const connection = createMessageConnection(
  new StreamMessageReader(process.stdin),
  new StreamMessageWriter(process.stdout),
);

function getDiagnostics(openedPath) {
  exec(
    `${process.env.ESLINT_BINARY} ${openedPath} -f json`,
    { cwd: process.env.ESLINT_CWD },
    (err, stdout, stderr) => {
      if (stderr) {
        // Note: err is non-null when eslint finds diagnostics in the code
        // (because the executable returns 1)
        // To check for actual execution errors, we need to read stderr
        void err;
        console.error("Error running Eslint: ", stderr.toString());
      } else {
        try {
          const json = JSON.parse(stdout.toString());
          connection.sendNotification("textDocument/publishDiagnostics", {
            uri: openedPath,
            diagnostics: json[0].messages.map(message => ({
              source: "eslint",
              severity: message.severity > 1 ? 1 : 2,
              message: message.message,
              range: {
                start: {
                  line: message.line - 1,
                  character: message.column - 1,
                },
                end: {
                  line: message.endLine - 1,
                  character: message.endColumn - 1,
                },
              },
            })),
          });
        } catch (e) {
          console.error("Error in Eslint Language Server: ", e);
        }
      }
    },
  );
}

connection.onRequest("initialize", async () => {
  connection.onNotification("textDocument/didOpen", didOpenEvent => {
    getDiagnostics(didOpenEvent.textDocument.uri);
  });

  connection.onNotification("textDocument/didSave", didSaveEvent => {
    getDiagnostics(didSaveEvent.textDocument.uri);
  });
});

connection.listen();

@@ -167,24 +167,55 @@
 process%22);%0A%0A
+const path = require(%22path%22);%0A%0A
 const connec
@@ -348,45 +348,242 @@
 ion 
-getDiagnostics(openedPath) %7B%0A
+isValidEslintFile(filePath) %7B%0A  const fileName = path.basename(filePath);%0A  const jsRegex = /%5C.jsx?/g;%0A%0A  return fileName.match(jsRegex) != null;%0A%7D%0A%0Afunction getDiagnostics(openedPath) %7B%0A  if (isValidEslintFile(openedPath))%0A  
   exec(%0A
     
@@ -578,16 +578,18 @@
   exec(%0A
+  
     %60$%7Bp
@@ -642,16 +642,18 @@
 n%60,%0A    
+  
 %7B cwd: p
@@ -681,16 +681,18 @@
  %7D,%0A    
+ 
+ 
 (err, st
@@ -716,16 +716,18 @@
 %7B%0A      
+  
 if (stde
@@ -728,24 +728,26 @@
  (stderr) %7B%0A
+  
         // N
@@ -817,16 +817,18 @@
         
+  
 // (beca
@@ -857,16 +857,18 @@
 urns 1)%0A
+  
         
@@ -939,16 +939,18 @@
         
+  
 void err
@@ -951,16 +951,18 @@
 id err;%0A
+  
         
@@ -1027,16 +1027,18 @@
 ;%0A      
+  
 %7D else %7B
@@ -1038,16 +1038,18 @@
  else %7B%0A
+  
         
@@ -1060,24 +1060,26 @@
 %7B%0A          
+  
 const json =
@@ -1102,32 +1102,34 @@
 ut.toString());%0A
+  
           connec
@@ -1199,16 +1199,18 @@
         
+ 
+ 
 uri: ope
@@ -1218,16 +1218,18 @@
 edPath,%0A
+  
         
@@ -1294,16 +1294,18 @@
         
+ 
+ 
 source: 
@@ -1314,16 +1314,18 @@
 slint%22,%0A
+  
         
@@ -1384,16 +1384,18 @@
         
+ 
+ 
 message:
@@ -1426,16 +1426,18 @@
         
+  
 range: %7B
@@ -1453,16 +1453,18 @@
         
+ 
+ 
 start: %7B
@@ -1460,24 +1460,26 @@
    start: %7B%0A
+  
             
@@ -1500,32 +1500,34 @@
 ssage.line - 1,%0A
+  
                 
@@ -1567,35 +1567,39 @@
                 
+  
 %7D,%0A
+  
                 
@@ -1615,32 +1615,34 @@
                 
+  
 line: message.en
@@ -1644,32 +1644,34 @@
 ge.endLine - 1,%0A
+  
                 
@@ -1714,35 +1714,39 @@
                 
+  
 %7D,%0A
+  
               %7D,
@@ -1762,13 +1762,17 @@
     
+  
 %7D)),%0A
+  
     
@@ -1789,16 +1789,18 @@
         
+  
 %7D catch 
@@ -1801,24 +1801,26 @@
 catch (e) %7B%0A
+  
           co
@@ -1876,26 +1876,30 @@
 e);%0A        
-%7D%0A
+  %7D%0A  
       %7D%0A    
@@ -1898,19 +1898,23 @@
   %7D%0A    
+  
 %7D,%0A
+  
   );%0A%7D%0A%0A

Add regex to check js files in eslint lsp (#471)