Skip to content

Commit

Permalink
es2020 target in tsc to allow bigint literal (#176)
Browse files Browse the repository at this point in the history
* es2020 target in tsc to allow bigint literal

* set module resolution for es2020 target

* add a test

Co-authored-by: near-bulldozer[bot] <73298989+near-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
ailisp and near-bulldozer[bot] authored Aug 16, 2022
1 parent 1db2e87 commit 2e9e197
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async function build(argv) {

async function checkTsBuildWithTsc(sourceFileWithPath) {
console.log(`check TypeScript build of ${sourceFileWithPath} with tsc`)
await executeCommand(`${TSC} --noEmit --experimentalDecorators --target es5 ${sourceFileWithPath}`);
await executeCommand(`${TSC} --noEmit --experimentalDecorators --target es2020 --moduleResolution node ${sourceFileWithPath}`);
}

// Common build function
Expand Down
2 changes: 1 addition & 1 deletion examples/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"target": "es5",
"target": "es2020",
"noEmit": true
},
"exclude": [
Expand Down
37 changes: 37 additions & 0 deletions tests/__tests__/typescript.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Worker } from 'near-workspaces';
import test from 'ava';


test.before(async t => {
// Init the worker and start a Sandbox server
const worker = await Worker.init();

// Prepare sandbox for tests, create accounts, deploy contracts, etx.
const root = worker.rootAccount;

// Deploy the test contract.
const typescriptContract = await root.devDeploy(
'build/typescript.wasm',
);
await typescriptContract.call(typescriptContract, 'init', {})

// Test users
const ali = await root.createSubAccount('ali');

// Save state for test runs
t.context.worker = worker;
t.context.accounts = { root, typescriptContract, ali };
});

test.after(async t => {
await t.context.worker.tearDown().catch(error => {
console.log('Failed to tear down the worker:', error);
});
});


test('bigint', async t => {
const { typescriptContract } = t.context.accounts;
let r = await typescriptContract.view('bigint', '');
t.is(r, "3");
});
9 changes: 6 additions & 3 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"scripts": {
"postinstall": "cd .. && yarn link && cd tests && yarn link near-sdk-js",
"build": "yarn build:context-api && yarn build:math-api && yarn build:storage-api && yarn build:log-panic-api && yarn build:promise-api && yarn build:promise-batch-api && yarn build:function-params && yarn build:lookup-map && yarn build:lookup-set && yarn build:unordered-map && yarn build:unordered-set && yarn build:vector && yarn build:bytes",
"build": "yarn build:context-api && yarn build:math-api && yarn build:storage-api && yarn build:log-panic-api && yarn build:promise-api && yarn build:promise-batch-api && yarn build:function-params && yarn build:lookup-map && yarn build:lookup-set && yarn build:unordered-map && yarn build:unordered-set && yarn build:vector && yarn build:bytes && yarn build:typescript",
"build:context-api": "near-sdk-js build src/context_api.js build/context_api.wasm",
"build:math-api": "near-sdk-js build src/math_api.js build/math_api.wasm",
"build:storage-api": "near-sdk-js build src/storage_api.js build/storage_api.wasm",
Expand All @@ -20,6 +20,7 @@
"build:unordered-set": "near-sdk-js build src/unordered-set.js build/unordered-set.wasm",
"build:vector": "near-sdk-js build src/vector.js build/vector.wasm",
"build:bytes": "near-sdk-js build src/bytes.js build/bytes.wasm",
"build:typescript": "near-sdk-js build src/typescript.ts build/typescript.wasm",
"test": "ava",
"test:context-api": "ava __tests__/test_context_api.ava.js",
"test:math-api": "ava __tests__/test_math_api.ava.js",
Expand All @@ -32,12 +33,14 @@
"test:unordered-set": "ava __tests__/unordered-set.ava.js",
"test:unordered-map": "ava __tests__/unordered-map.ava.js",
"test:vector": "ava __tests__/vector.ava.js",
"test:bytes": "ava __tests__/bytes.ava.js"
"test:bytes": "ava __tests__/bytes.ava.js",
"test:typescript": "ava __tests__/typescript.ava.js"
},
"author": "Near Inc <hello@nearprotocol.com>",
"license": "Apache-2.0",
"devDependencies": {
"ava": "^4.2.0",
"near-workspaces": "3.1.0"
"near-workspaces": "3.1.0",
"typescript": "^4.7.4"
}
}
18 changes: 18 additions & 0 deletions tests/src/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
NearContract,
NearBindgen,
view,
} from 'near-sdk-js'

@NearBindgen
class TypeScriptTestContract extends NearContract {
@view
bigint() {
// JSON.stringify cannot seriaize a BigInt, need manually toString
return (1n + 2n).toString()
}

default() {
return new TypeScriptTestContract()
}
}
10 changes: 10 additions & 0 deletions tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"target": "es2020",
"noEmit": true
},
"exclude": [
"node_modules"
],
}
5 changes: 5 additions & 0 deletions tests/yarn.lock

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

0 comments on commit 2e9e197

Please sign in to comment.