Skip to content

Commit

Permalink
lint fixes and others
Browse files Browse the repository at this point in the history
  • Loading branch information
rajivramv committed Jun 19, 2019
1 parent 5198c95 commit 6f7fb49
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 95 deletions.
6 changes: 3 additions & 3 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"react-dom": "^16.8.6",
"react-scripts": "3.0.1",
"tslint": "^5.17.0",
"typescript": "3.5.2",
"typescript": "^3.4.5",
"underscore": "^1.9.1"
},
"scripts": {
Expand Down
78 changes: 40 additions & 38 deletions app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { Component, Fragment, ChangeEvent } from 'react';
import React, { ChangeEvent, Component, Fragment } from "react";
import AceEditor from "react-ace";
import { sampleCppProgram, sampleJavaProgram } from "./samples";
import { getLintResults } from "./api";
import "./App.css";
import { sampleCppProgram, sampleJavaProgram } from "./samples";

import "brace/mode/java";
import "brace/mode/c_cpp";
import "brace/mode/java";
import "brace/theme/monokai";

export interface IAnnotation {
Expand All @@ -23,55 +23,32 @@ interface IAppState {
}

class App extends Component<{}, IAppState> {
state = {
lang: "",
public state = {
annotations: [],
code: "",
hasSyntacticErrors: false
}

setJavaCode = () => {
this.setState({ code: sampleJavaProgram, lang: "java", annotations: [], hasSyntacticErrors: false })
}

setCppCode = () => {
this.setState({ code: sampleCppProgram, lang: "cpp", annotations: [], hasSyntacticErrors: false })
}

setLanguage = (e: ChangeEvent<HTMLSelectElement>) => {
this.setState({ lang: e.target.value })
}
hasSyntacticErrors: false,
lang: ""
};

componentDidUpdate = (prevProps: {}, prevState: IAppState) => {
public componentDidUpdate = (prevProps: {}, prevState: IAppState) => {
if (prevState.code !== this.state.code || prevState.lang !== this.state.lang) {
this.refreshLint()
}
}

refreshLint = async () => {
try {
const annotations = await getLintResults(this.state.code, this.state.lang);
this.setState({ annotations, hasSyntacticErrors: false });
} catch(e) {
this.setState({ hasSyntacticErrors: true })
this.refreshLint();
}
}

onCodeChange = async (value: string) => {
this.setState({ code: value })
this.refreshLint()
}

render = () => (
public render = () => (
<Fragment>
<h1>Simple Linter</h1>
{/* tslint:disable-next-line: max-line-length */}
<p>This utility lints source code to verify if it conforms <a href="https://github.com/google/styleguide">Google's Style Guide</a>. It uses <a href="https://checkstyle.org/index.html">checkstyle</a> and <a href="https://github.com/google/styleguide/tree/gh-pages/cpplint">cpplint</a> for the linting. Currently implemented languages: <strong>Java, C++</strong></p>
{/* tslint:disable-next-line: max-line-length */}
<p>Add sample code: <button onClick={this.setJavaCode}>Java</button><button onClick={this.setCppCode}>C++</button></p>
{/* tslint:disable-next-line: max-line-length */}
<p>Language: <select onChange={this.setLanguage} value={this.state.lang}><option value="java">Java</option><option value="cpp">C++</option></select></p>
{this.state.hasSyntacticErrors && <p className="error"> There are syntax errors! Please correct them. </p>}
<div id="code-linter-container">
<AceEditor
mode={this.state.lang === "cpp" ? "c_cpp": "java"}
mode={this.state.lang === "cpp" ? "c_cpp" : "java"}
theme="monokai"
onChange={this.onCodeChange}
name="code-linter"
Expand All @@ -84,7 +61,32 @@ class App extends Component<{}, IAppState> {
</div>
</Fragment>
)
}
private setJavaCode = () => {
this.setState({ code: sampleJavaProgram, lang: "java", annotations: [], hasSyntacticErrors: false });
}

private setCppCode = () => {
this.setState({ code: sampleCppProgram, lang: "cpp", annotations: [], hasSyntacticErrors: false });
}

private setLanguage = (e: ChangeEvent<HTMLSelectElement>) => {
this.setState({ lang: e.target.value });
}

private refreshLint = async () => {
try {
const annotations = await getLintResults(this.state.code, this.state.lang);
this.setState({ annotations, hasSyntacticErrors: false });
} catch (e) {
this.setState({ hasSyntacticErrors: true });
}
}

private onCodeChange = async (value: string) => {
this.setState({ code: value });
this.refreshLint();
}

}

export default App;
83 changes: 52 additions & 31 deletions app/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,63 @@
import { debounce } from "underscore";
import { IAnnotation } from "./App";
const CPP_ERROR_REGEX = /^[a-zA-Z0-9/\-.]+.cpp:([0-9]+): (.*)/;
const JAVA_ERROR_REGEX = /^[a-zA-Z0-9/\-.[\] ]+.java:([0-9]+):[0-9:]* (.*)/;

// tslint:disable-next-line: variable-name
const _getLintResults = (code: string, lang: string): Promise<IAnnotation[]> => {
const options = {
method: 'post',
body: `code=${encodeURI(code)}&lang=${lang}`,
headers: {
'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: `code=${encodeURI(code)}&lang=${lang}`
}
method: "post"
};
return new Promise((resolve, reject) => {
fetch(`${process.env.REACT_APP_API_URI}/lint`, options).then((res) => {
res.json().then(json => {
switch(json.lang) {
case "cpp":
const errors = json.errors.split("\n").filter((line: string) => line.length !== 0).map((line: string) => {
const match = line.match(CPP_ERROR_REGEX);
if (match != null) {
return { row: Number(match[1]) - 1, column: 0, type: "error", text: match[2] }
} else return {}
});
resolve(errors);
break;
case "java":
if (json.errors.length > 0)
reject("There are syntactic errors");
console.log(json)
break;

}

fetch(`${process.env.REACT_APP_API_URI}/lint`, options)
.then(res => {
res.json().then(json => {
switch (json.lang) {
case "cpp":
const cppErrors = json.errors
.split("\n")
.filter((line: string) => line.length !== 0)
.map((line: string) => {
const match = line.match(CPP_ERROR_REGEX);
if (match != null) {
return { row: Number(match[1]) - 1, column: 0, type: "error", text: match[2] };
} else {
return {};
}
});
resolve(cppErrors);
break;
case "java":
if (json.errors.length > 0) {
reject("There are syntactic errors");
return;
}
const javaErrors = json.completed
.replace("Starting audit...", "")
.replace("Audit done.", "")
.split("\n")
.filter((line: string) => line.length !== 0)
.map((line: string) => {
const match = line.match(JAVA_ERROR_REGEX);
if (match != null) {
return { row: Number(match[1]) - 1, column: 0, type: "error", text: match[2] };
} else {
return {};
}
});
resolve(javaErrors);
break;
}
});
})
}).catch(err => {
reject(err)
})
})
.catch(err => {
reject(err);
});
});
};

}

export const getLintResults = debounce(_getLintResults, 100, true);
export const getLintResults = debounce(_getLintResults, 100, true);
12 changes: 6 additions & 6 deletions app/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import "./index.css";
import * as serviceWorker from "./serviceWorker";

ReactDOM.render(<App />, document.getElementById('root'));
ReactDOM.render(<App />, document.getElementById("root"));

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
Expand Down
32 changes: 16 additions & 16 deletions app/src/samples.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@

export const sampleCppProgram = `#include <iostream>
using namespace std;
int main()
{
int count, i, arr[30], num, first, last, middle;
cout<<"how many elements would you like to enter?:";
cout<<"how many elements would you like to enter?:";
cin>>count;
for (i=0; i<count; i++)
{
cout<<"Enter number "<<(i+1)<<": ";
cout<<"Enter number "<<(i+1)<<": ";
cin>>arr[i];
}
cout<<"Enter the number that you want to search:";
cout<<"Enter the number that you want to search:";
cin>>num;
first = 0;
last = count-1;
Expand All @@ -26,14 +27,14 @@ int main()
}
else if(arr[middle] == num)
{
cout<<num<<" found in the array at the location "<<middle+1<<"\n";
break;
}
else {
last = middle - 1;
}
middle = (first + last)/2;
}
cout<<num<<" found in the array at the location "<<middle+1<<"\n";
break;
}
else {
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<num<<" not found in the array";
Expand All @@ -44,29 +45,28 @@ int main()
export const sampleJavaProgram = `public class ComplexNumber{
//for real and imaginary parts of complex numbers
double real, img;
//constructor to initialize the complex number
ComplexNumber(double r, double i){
this.real = r;
this.img = i;
}
public static ComplexNumber sum(ComplexNumber c1, ComplexNumber c2)
{
//creating a temporary complex number to hold the sum of two numbers
ComplexNumber temp = new ComplexNumber(0, 0);
temp.real = c1.real + c2.real;
temp.img = c1.img + c2.img;
//returning the output complex number
return temp;
}
public static void main(String args[]) {
ComplexNumber c1 = new ComplexNumber(5.5, 4);
ComplexNumber c2 = new ComplexNumber(1.2, 3.5);
ComplexNumber temp = sum(c1, c2);
System.out.printf("Sum is: "+ temp.real+" + "+ temp.img +"i");
}
}
`;
`;

0 comments on commit 6f7fb49

Please sign in to comment.