diff --git a/app/package-lock.json b/app/package-lock.json index 3400b23..5cb4d41 100644 --- a/app/package-lock.json +++ b/app/package-lock.json @@ -11819,9 +11819,9 @@ "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=" }, "typescript": { - "version": "3.5.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.5.2.tgz", - "integrity": "sha512-7KxJovlYhTX5RaRbUdkAXN1KUZ8PwWlTzQdHV6xNqvuFOs7+WBo10TQUqT19Q/Jz2hk5v9TQDIhyLhhJY4p5AA==" + "version": "3.4.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.5.tgz", + "integrity": "sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw==" }, "uglify-js": { "version": "3.4.10", diff --git a/app/package.json b/app/package.json index eb1bda7..5a1772f 100644 --- a/app/package.json +++ b/app/package.json @@ -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": { diff --git a/app/src/App.tsx b/app/src/App.tsx index 8ba41d8..c0fe5b0 100644 --- a/app/src/App.tsx +++ b/app/src/App.tsx @@ -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 { @@ -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) => { - 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 = () => (

Simple Linter

+ {/* tslint:disable-next-line: max-line-length */}

This utility lints source code to verify if it conforms Google's Style Guide. It uses checkstyle and cpplint for the linting. Currently implemented languages: Java, C++

+ {/* tslint:disable-next-line: max-line-length */}

Add sample code:

+ {/* tslint:disable-next-line: max-line-length */}

Language:

{this.state.hasSyntacticErrors &&

There are syntax errors! Please correct them.

}
{
) -} + 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) => { + 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; diff --git a/app/src/api.ts b/app/src/api.ts index 6b360c1..fa0f537 100644 --- a/app/src/api.ts +++ b/app/src/api.ts @@ -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 => { 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); \ No newline at end of file +export const getLintResults = debounce(_getLintResults, 100, true); diff --git a/app/src/index.tsx b/app/src/index.tsx index 87d1be5..0e76424 100644 --- a/app/src/index.tsx +++ b/app/src/index.tsx @@ -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(, document.getElementById('root')); +ReactDOM.render(, 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. diff --git a/app/src/samples.ts b/app/src/samples.ts index c5f3d2c..9b51d3a 100644 --- a/app/src/samples.ts +++ b/app/src/samples.ts @@ -1,18 +1,19 @@ + export const sampleCppProgram = `#include 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>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; @@ -26,14 +27,14 @@ int main() } else if(arr[middle] == num) { - cout< last) { cout<