Skip to content

Commit

Permalink
Add detection of main under more circumstances
Browse files Browse the repository at this point in the history
The cases that are intended to be added by this commit are:

- Code with no comments but ends in a semi colon before the declaration of main.
 No comments was required to prevent `//` being included to comment out the line. This was done by not allowing `/` as part of the code before the `;` While this is more restrictive than necessary it didn't seem likely that the code before the main would include `/` so didn't seem worthwhile to take the performance hit to make the check less restrictive. To prevent long regex scans going multiple lines I also disallowed newlines in the code by including `\n` and `\r` as disallowed characters. (The whole point is code on the same line as main so this seemed fine and would help prevent unnecessarily matching all lines before main when it doesn't matter).
- Allow multiline comments within the arguments area of main that both start and end within the brackets.
  Not expecting a substantial performance cost from this one because this case is near the end of the matching.

I used the following test cases
```rust
fn main() { // Should work
    println!("Hello, world!");
}

//;fn main() { //Shouldn't work
    println!("Hello, world!");
}


use std; fn main(){ println!("Hello, world!"); } // Should work

const fn main() { // Should work
    println!("Hello, world!");
}

/* fn main() {} */ // Shouldn't work

fn main(/* comment */) { // Should work
   // snip
}
```
  • Loading branch information
c-git authored Nov 10, 2022
1 parent f28b502 commit 43cc728
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion ui/frontend/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const codeSelector = (state: State) => state.code;
const HAS_TESTS_RE = /^\s*#\s*\[\s*test\s*([^"]*)]/m;
export const hasTestsSelector = createSelector(codeSelector, code => !!code.match(HAS_TESTS_RE));

const HAS_MAIN_FUNCTION_RE = /^\s*(pub\s+)?\s*(const\s+)?\s*(async\s+)?\s*fn\s+main\s*\(\s*\)/m;
const HAS_MAIN_FUNCTION_RE = /^([^\n\r\/]*;)?\s*(pub\s+)?\s*(const\s+)?\s*(async\s+)?\s*fn\s+main\s*\(\s*(\/\*.*\*\/)?\s*\)/m;
export const hasMainFunctionSelector = createSelector(codeSelector, code => !!code.match(HAS_MAIN_FUNCTION_RE));

const CRATE_TYPE_RE = /^\s*#!\s*\[\s*crate_type\s*=\s*"([^"]*)"\s*]/m;
Expand Down

0 comments on commit 43cc728

Please sign in to comment.