Skip to content

Commit 7b8bf05

Browse files
Regexp issues, during signers extraction (#224)
* Add parsing utility methods * Use extractSigners method from utils * Clear console.log calls * Add new line at the eof Co-authored-by: Mackenzie <[email protected]>
1 parent aa62eab commit 7b8bf05

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

src/components/Arguments/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ const Arguments: React.FC<ArgumentsProps> = (props) => {
279279

280280
// Language server throws "input is not literal" without quotes
281281
if (type === `String`) {
282-
console.log({ value });
283282
value = `\"${value.replace(/"/g, '\\"')}\"`;
284283
}
285284

src/components/CadenceEditor.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import React from 'react';
22
import styled from '@emotion/styled';
33
import { keyframes } from '@emotion/core';
44
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
5+
const { MonacoServices } = require('monaco-languageclient/lib/monaco-services');
6+
import { extractSigners } from "util/parser";
7+
58

69
import configureCadence, { CADENCE_LANGUAGE_ID } from 'util/cadence';
710
import {
@@ -357,10 +360,6 @@ class CadenceEditor extends React.Component<
357360
return [];
358361
}
359362

360-
extractSigners(code: string): number {
361-
return this.extract(code, 'prepare').filter((item) => !!item).length;
362-
}
363-
364363
hover(highlight: Highlight): void {
365364
const { startLine, startColumn, endLine, endColumn, color } = highlight;
366365
const model = this.editor.getModel();
@@ -426,7 +425,7 @@ class CadenceEditor extends React.Component<
426425
const list = args[activeId] || [];
427426

428427
/// Extract number of signers from code
429-
const signers = this.extractSigners(code);
428+
const signers = extractSigners(code).length
430429
const problemsList: ProblemsList = problems[activeId] || {
431430
error: [],
432431
warning: [],

src/util/parse-contract-name.ts

Whitespace-only changes.

src/util/parser.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export const collapseSpaces = (input: string) => input.replace(/\s+/g, " ");
2+
export const stripNewLines = (input: string) => input.replace(/\r\n|\n|\r/g, " ");
3+
4+
export const generateSchema = (argsDefinition: string) =>
5+
argsDefinition
6+
.split(",")
7+
.map((item) => item.replace(/\s*/g, ""))
8+
.filter((item) => item !== "");
9+
10+
export const stripComments = (code: string) => {
11+
const commentsRegExp = /(\/\*[\s\S]*?\*\/)|(\/\/.*)/g;
12+
return code.replace(commentsRegExp, "");
13+
};
14+
15+
export const extract = (code: string, keyWord: string) => {
16+
const noComments = stripComments(code);
17+
const target = collapseSpaces(noComments.replace(/[\n\r]/g, ""));
18+
19+
if (target) {
20+
const regexp = new RegExp(keyWord, "g");
21+
const match = regexp.exec(target);
22+
23+
if (match) {
24+
if (match[1] === "") {
25+
return [];
26+
}
27+
return generateSchema(match[1]);
28+
}
29+
}
30+
return [];
31+
};
32+
33+
export const extractSigners = (code: string) => {
34+
return extract(code, `(?:prepare\\s*\\(\\s*)([^\\)]*)(?:\\))`);
35+
};

0 commit comments

Comments
 (0)