Skip to content

Commit f13eba4

Browse files
torch2424Willem Wyndham
and
Willem Wyndham
authored
Added Prettier for code formatting (near#105)
* Moved to eslint and used suggested linting and formatting * Updated node version to stable and LTS * chore: removed and added TS required comments Also replaced `this` with namespace to make tsc happy Co-authored-by: Willem Wyndham <[email protected]>
1 parent 869b411 commit f13eba4

15 files changed

+1528
-940
lines changed

.eslintrc.js

+197
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
module.exports = {
2+
root: true,
3+
parser: "@typescript-eslint/parser",
4+
plugins: [
5+
"@typescript-eslint",
6+
],
7+
extends: [
8+
"eslint:recommended",
9+
"plugin:@typescript-eslint/eslint-recommended",
10+
"plugin:@typescript-eslint/recommended",
11+
],
12+
parserOptions: {
13+
ecmaVersion: 2020,
14+
sourceType: "module",
15+
ecmaFeatures: {}
16+
},
17+
18+
// === General rules =========================================================
19+
20+
rules: {
21+
// Omitted semicolons are hugely popular, yet within the compiler it makes
22+
// sense to be better safe than sorry.
23+
"semi": "error",
24+
25+
// Our code bases uses 2 spaces for indentation, and we enforce it here so
26+
// files don't mix spaces, tabs or different indentation levels.
27+
"indent": ["error", 2, {
28+
"SwitchCase": 1,
29+
"VariableDeclarator": "first",
30+
"offsetTernaryExpressions": true,
31+
"ignoredNodes": [ // FIXME: something's odd here
32+
"ConditionalExpression > *",
33+
"ConditionalExpression > * > *",
34+
"ConditionalExpression > * > * > *"
35+
]
36+
}],
37+
38+
// This is mostly visual style, making comments look uniform.
39+
"spaced-comment": ["error", "always", {
40+
"markers": ["/"], // triple-slash
41+
"exceptions": ["/"] // all slashes
42+
}],
43+
44+
// This tends to be annoying as it encourages developers to make everything
45+
// that is never reassigned a 'const', sometimes semantically incorrect so,
46+
// typically leading to huge diffs in follow-up PRs modifying affected code.
47+
"prefer-const": "off",
48+
49+
// It is perfectly fine to declare top-level variables with `var`, yet this
50+
// rule doesn't provide configuration options that would help.
51+
"no-var": "off",
52+
53+
// Quite often, dealing with multiple related cases at once or otherwise
54+
// falling through is exactly the point of using a switch.
55+
"no-fallthrough": "off",
56+
57+
// Typical false-positives here are `do { ... } while (true)` statements or
58+
// similar, but the only option provided here is not checking any loops.
59+
"no-constant-condition": ["error", { checkLoops: false }],
60+
61+
// Functions are nested in blocks occasionally, and there haven't been any
62+
// problems with this so far, so turning the check off.
63+
"no-inner-declarations": "off",
64+
65+
// Quite common in scenarios where an iteration starts at `current = this`.
66+
"@typescript-eslint/no-this-alias": "off",
67+
68+
// Disabled here, but enabled again for JavaScript files.
69+
"no-unused-vars": "off",
70+
71+
// Disabled here, but enabled again for TypeScript files.
72+
"@typescript-eslint/no-unused-vars": "off",
73+
74+
// Allow emptry functions for some of our base classes
75+
"@typescript-eslint/no-empty-function": "off"
76+
},
77+
overrides: [
78+
79+
// === TypeScript rules ====================================================
80+
81+
{
82+
files: [
83+
"**/assembly/**/*.ts"
84+
],
85+
rules: {
86+
// Enforcing to remove function parameters on stubs makes code less
87+
// maintainable, so we instead allow unused function parameters.
88+
"@typescript-eslint/no-unused-vars": [
89+
"warn", {
90+
"vars": "local",
91+
"varsIgnorePattern": "^_|^[A-Z](?:From|To)?$", // ignore type params
92+
"args": "none",
93+
"ignoreRestSiblings": false
94+
}
95+
],
96+
97+
// Namespaces are quite useful in AssemblyScript
98+
"@typescript-eslint/no-namespace": "off",
99+
100+
// There is actually codegen difference here
101+
"@typescript-eslint/no-array-constructor": "off",
102+
103+
// Sometimes it can't be avoided to add a @ts-ignore
104+
"@typescript-eslint/ban-ts-comment": "off",
105+
106+
// Utilized to achieve portability in some cases
107+
"@typescript-eslint/no-non-null-assertion": "off",
108+
}
109+
},
110+
111+
// === Compiler rules (extends AssemblyScript rules) =======================
112+
113+
{
114+
files: [
115+
"**/assembly/**/*.ts"
116+
],
117+
rules: {
118+
// There is an actual codegen difference here - TODO: revisit
119+
"no-cond-assign": "off",
120+
121+
// Not all types can be omitted in AS yet - TODO: revisit
122+
"@typescript-eslint/no-inferrable-types": "off",
123+
124+
// Used rarely to reference internals that are not user-visible
125+
"@typescript-eslint/triple-slash-reference": "off",
126+
127+
// The compiler has its own `Function` class for example
128+
"no-shadow-restricted-names": "off",
129+
"@typescript-eslint/ban-types": "off"
130+
}
131+
},
132+
133+
// === Standard Library rules (extends AssemblyScript rules) ===============
134+
135+
{
136+
files: [
137+
"**/assembly/**/*.ts"
138+
],
139+
rules: {
140+
// We are implementing with --noLib, so we shadow all the time
141+
"no-shadow-restricted-names": "off",
142+
143+
// Similarly, sometimes we need the return type to be String, not string
144+
"@typescript-eslint/ban-types": "off"
145+
}
146+
},
147+
148+
// === Standard Definition rules (extends TypeScript rules) ================
149+
150+
{
151+
files: [
152+
"**/assembly/**/*.d.ts"
153+
],
154+
rules: {
155+
// Often required to achieve compatibility with TypeScript
156+
"@typescript-eslint/no-explicit-any": "off",
157+
158+
// Interfaces can be stubs here, i.e. not yet fully implemented
159+
"@typescript-eslint/no-empty-interface": "off",
160+
161+
// Definitions make use of `object` to model rather unusual constraints
162+
"@typescript-eslint/ban-types": "off"
163+
}
164+
},
165+
166+
167+
168+
// === Test rules (extends TypeScript rules) ===============================
169+
170+
{
171+
files: [
172+
"**/assembly/__tests__/**/*.ts"
173+
],
174+
rules: {
175+
// Tests typically include unusual code patterns on purpose. This is
176+
// very likely not an extensive list, but covers what's there so far.
177+
"no-empty": "off",
178+
"no-cond-assign": "off",
179+
"no-compare-neg-zero": "off",
180+
"no-inner-declarations": "off",
181+
"no-constant-condition": "off",
182+
"use-isnan": "off",
183+
"@typescript-eslint/no-namespace": "off",
184+
"@typescript-eslint/no-unused-vars": "off",
185+
"@typescript-eslint/no-empty-function": "off",
186+
"@typescript-eslint/no-non-null-assertion": "off",
187+
"@typescript-eslint/no-extra-semi": "off",
188+
"@typescript-eslint/no-inferrable-types": "off",
189+
"@typescript-eslint/ban-types": "off",
190+
"@typescript-eslint/triple-slash-reference": "off",
191+
"@typescript-eslint/ban-ts-comment": "off",
192+
"@typescript-eslint/no-extra-non-null-assertion": "off",
193+
"@typescript-eslint/no-empty-interface": "off"
194+
}
195+
},
196+
]
197+
};

.travis.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
language: node_js
22
node_js:
3-
- "10"
4-
- "11"
5-
- "12"
3+
- "node"
4+
- "lts/*"
65

76
script:
87
- yarn test

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ Special thanks to https://github.com/MaxGraey/bignum.wasm for basic unit testing
88

99
This is developed for use in smart contracts written in AssemblyScript for https://github.com/nearprotocol/nearcore.
1010
This imposes such limitations:
11+
1112
- Float numbers not supported
1213
- We assume that memory never needs to be deallocated (cause these contracts are short-lived).
1314

1415
Note that this mostly just defines the way it's currently implemented. Contributors are welcome to fix limitations.
1516

16-
1717
# Usage
1818

1919
## Encoding JSON
@@ -38,7 +38,6 @@ let json: Uint8Array = encoder.serialize();
3838

3939
// Or get serialized data as string
4040
let jsonString: String = encoder.toString();
41-
4241
```
4342

4443
## Parsing JSON
@@ -114,7 +113,7 @@ let jsonObj: JSON.Object = JSON.parse(`{"hello": "world"}`);
114113
let world = jsonObj.getString("hello");
115114

116115
// If you don't know what the type of the value
117-
let unknown = jsonObj.getValue("hello")
116+
let unknown = jsonObj.getValue("hello");
118117

119-
unknown.isString // true;
118+
unknown.isString; // true;
120119
```

as-pect.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717
/** To enable wat file output, use the following flag. The filename is ignored, but required by the compiler. */
1818
"--textFile": ["output.wat"],
1919
"--runtime": ["stub"], // Acceptable values are: full, half, stub (arena), and none,
20-
"--baseDir": process.cwd()
20+
"--baseDir": process.cwd(),
2121
// "--runPasses": ["dce"]
2222
},
2323
/**

0 commit comments

Comments
 (0)