Skip to content

Commit

Permalink
Parser fix arguments with spaces (#19)
Browse files Browse the repository at this point in the history
* Bump @onflow/fcl to 0.0.76

* Update tests to cover case with spaces in arguments definitions

* Fix arguments extraction regexp

* Refactor type checker

* Refactor

* Update CHANGELOG

* Add test to check spaces in signers definition
  • Loading branch information
Maksim Daunarovich authored Sep 22, 2021
1 parent 9c050e8 commit c1776f5
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 70 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 0.1.8 - 2021-09-22
- Fixed parser issue, when extra spaces are present in argument definition

### 0.1.7 - 2021-09-20
- Added support for Optionals
- Added support for multi-line arguments
Expand Down
28 changes: 28 additions & 0 deletions dev-test/test/args.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ describe("optional arguments", () => {
return emulator.stop();
});

it("failing split", async () => {
const input = ["Hello"];
const cadence = `
pub fun main(message: String):String {
return message
}
`;

const args = () => mapValuesToCode(cadence, input);
const output = await query({ cadence, args });

expect(output).toBe(input[0]);
});

it("basic - UFix64 and Address", async () => {
const input = [1337, "0x01"];
const cadence = `
pub fun main(number: UFix64, address: Address):UFix64{
return number
}
`;

const args = () => mapValuesToCode(cadence, input);
const output = await query({ cadence, args });

expect(output).toBe(toFixedValue(input[0]));
});

it("optionals - String? - no value", async () => {
const input = null;
const cadence = `
Expand Down
94 changes: 50 additions & 44 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flow-cadut",
"version": "0.1.7",
"version": "0.1.8",
"description": "Flow Cadence Template Utilities",
"author": "Maksim Daunarovich",
"license": "Apache-2.0",
Expand Down Expand Up @@ -39,15 +39,15 @@
"dependencies": {
"@babel/preset-env": "^7.14.5",
"@onflow/config": "0.0.2",
"@onflow/fcl": "^0.0.71",
"@onflow/fcl": "^0.0.76",
"@onflow/types": "^0.0.4",
"esm": "^3.2.25",
"handlebars": "^4.7.7",
"handlebars-loader": "^1.7.1",
"prettier": "^2.3.0",
"rimraf": "^3.0.2",
"simple-git": "^2.40.0",
"yargs": "^15.4.1",
"prettier": "^2.3.0"
"yargs": "^15.4.1"
},
"devDependencies": {
"@babel/cli": "^7.14.3",
Expand Down
5 changes: 4 additions & 1 deletion src/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
isArray,
isDictionary,
isComplexType,
wrongType,
} from "./type-checker";

import { removeSpaces } from "./strings";
Expand Down Expand Up @@ -90,6 +91,8 @@ export const reportMissing = (itemType = "items", found, required, prefix = "")
export const raw = (type) => type.slice(0, -1);

export const resolveBasicType = (type) => {
if (wrongType(type)) return false;

if (type.includes("?")) {
return t.Optional(t[raw(type)]);
}
Expand Down Expand Up @@ -141,7 +144,7 @@ export const mapArgument = (type, value) => {
}

case isAddress(type): {
const prefixedAddress = withPrefix(value)
const prefixedAddress = withPrefix(value);
return fcl.arg(prefixedAddress, resolvedType);
}

Expand Down
4 changes: 2 additions & 2 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ export const extractSigners = (code) => {
};

export const extractScriptArguments = (code) => {
return extract(code, `(?:fun\\s+main\\(\\s*)([^\\)]*)(?:\\))`);
return extract(code, `(?:fun\\s+main\\s*\\(\\s*)([^\\)]*)(?:\\))`);
};

export const extractTransactionArguments = (code) => {
return extract(code, `(?:transaction\\(\\s*)([^\\)]*)(?:\\))`);
return extract(code, `(?:transaction\\s*\\(\\s*)([^\\)]*)(?:\\))`);
};

export const extractContractName = (code) => {
Expand Down
13 changes: 11 additions & 2 deletions src/type-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
* limitations under the License.
*/

export const wrongType = (type) => !type || typeof type != "string";

export const isBasicNumType = (type) => {
if (wrongType(type)) return false;
return type.startsWith("Int") || type.startsWith("UInt") || type.startsWith("Word");
};

export const isFixedNumType = (type) => {
if (wrongType(type)) return false;
return type.startsWith("Fix64") || type.startsWith("UFix64");
};

Expand All @@ -30,6 +34,8 @@ export const isBoolean = (type) => type === "Bool";
export const isAddress = (type) => type === "Address" || type === "Address?";

export const isBasicType = (type) => {
if (wrongType(type)) return false;

let fixedType = type.endsWith("?") ? type.slice(0, -1) : type;
return (
isBasicNumType(fixedType) ||
Expand All @@ -40,12 +46,15 @@ export const isBasicType = (type) => {
};

export const isArray = (type) => {
if (wrongType(type)) return false;

const clearType = type.replace(/\s/g, "");
const result = clearType.startsWith("[") && clearType.endsWith("]");
return result;
return clearType.startsWith("[") && clearType.endsWith("]");
};

export const isDictionary = (type) => {
if (wrongType(type)) return false;

const clearType = type.replace(/\s/g, "");
return clearType.startsWith("{") && clearType.endsWith("}");
};
Expand Down
Loading

0 comments on commit c1776f5

Please sign in to comment.