Skip to content

Commit 6a7399a

Browse files
authored
Disable all tslint and eslint rules in generated code (#328)
* Disable all tslint rules in generated code so as to play well with all tslint configurations. * Add tslint config * git-ignore renamed files * Disable all eslint rules in generated TypeScript code like we do for tslint * Use a more recent version of Node.js to avoid error "Module.createRequire is not a function" when running eslint. * Fix package conflict * Maybe we don't need the 'js' command. Try testing for just 'node'. * Check for npm * Use 'node' instead of 'js' in atdts tests
1 parent 67405ec commit 6a7399a

File tree

11 files changed

+113
-49
lines changed

11 files changed

+113
-49
lines changed

.circleci/setup-system

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ set -eu
88

99
sudo apt-get update
1010

11+
# Add repo to install a sufficiently-recent version of node (for atdts tests)
12+
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
13+
1114
# This includes compilers and libraries to support the various target
1215
# languages of atd.
1316
#
1417
sudo apt-get install -y \
1518
default-jdk \
1619
nodejs \
17-
npm \
1820
python3 \
1921
python3-pip \
2022
python-is-python3 \
@@ -49,5 +51,8 @@ python3 -m pytest --version
4951
echo 'check mypy'
5052
python3 -m mypy --version
5153

52-
echo 'check js'
53-
js --version
54+
echo 'check Node.js'
55+
node --version
56+
57+
echo 'check npm'
58+
npm --version

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* atdts: Eliminate the type alias `type Int = number` since it was
99
more confusing than helpful. Occurrences of `Int` are replaced
1010
by `number /*int*/`.
11+
* atdts: Disable all tslint and eslint rules in generated code so as
12+
to play well with all tslint and eslint configurations.
1113

1214
2.10.0 (2022-08-09)
1315
-------------------

atdts/src/lib/Codegen.ml

+13-9
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,19 @@ let double_esc s =
181181
escape_string_content Double s
182182

183183
let runtime_start atd_filename =
184-
sprintf {|// Generated by atdts from type definitions in '%s'.
185-
//
186-
// Type-safe translations from/to JSON
187-
//
188-
// For each type 'Foo', there is a pair of functions:
189-
// - 'writeFoo': convert a 'Foo' value into a JSON-compatible value.
190-
// - 'readFoo': convert a JSON-compatible value into a TypeScript value
191-
// of type 'Foo'.
192-
|}
184+
sprintf {|/*
185+
Generated by atdts from type definitions in '%s'.
186+
187+
Type-safe translations from/to JSON
188+
189+
For each type 'Foo', there is a pair of functions:
190+
- 'writeFoo': convert a 'Foo' value into a JSON-compatible value.
191+
- 'readFoo': convert a JSON-compatible value into a TypeScript value
192+
of type 'Foo'.
193+
*/
194+
195+
/* tslint:disable */
196+
/* eslint-disable */|}
193197
atd_filename
194198

195199
let runtime_end = {|

atdts/test/ts-expected/everything.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
// Generated by atdts from type definitions in 'everything.atd'.
2-
//
3-
// Type-safe translations from/to JSON
4-
//
5-
// For each type 'Foo', there is a pair of functions:
6-
// - 'writeFoo': convert a 'Foo' value into a JSON-compatible value.
7-
// - 'readFoo': convert a JSON-compatible value into a TypeScript value
8-
// of type 'Foo'.
1+
/*
2+
Generated by atdts from type definitions in 'everything.atd'.
93
4+
Type-safe translations from/to JSON
5+
6+
For each type 'Foo', there is a pair of functions:
7+
- 'writeFoo': convert a 'Foo' value into a JSON-compatible value.
8+
- 'readFoo': convert a JSON-compatible value into a TypeScript value
9+
of type 'Foo'.
10+
*/
11+
12+
/* tslint:disable */
13+
/* eslint-disable */
1014

1115
export type Anything = any
1216

atdts/test/ts-tests/.eslintrc.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"es2021": true
5+
},
6+
"extends": [
7+
"eslint:recommended",
8+
"plugin:@typescript-eslint/recommended"
9+
],
10+
"overrides": [
11+
],
12+
"parser": "@typescript-eslint/parser",
13+
"parserOptions": {
14+
"ecmaVersion": "latest",
15+
"sourceType": "module"
16+
},
17+
"plugins": [
18+
"@typescript-eslint"
19+
],
20+
"rules": {
21+
}
22+
}

atdts/test/ts-tests/.gitignore

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
/package-lock.json
33

44
# Test output
5-
/a_str
6-
/b_str
7-
/b_str2
5+
/aStr
6+
/bStr
7+
/bStr2

atdts/test/ts-tests/Makefile

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
JS ?= js
1+
JS ?= node
22

33
.PHONY: test
44
test:
55
# Install TypeScript and JavaScript dependencies locally
66
npm install
7+
$(MAKE) lint
78
# Compile TypeScript (handwritten + generated by atdts) to JavaScript
89
npm run tsc
910
# Run the resulting code (compiled from TypeScript)
1011
$(JS) manual_sample.js
1112
$(JS) test_atdts.js
1213

14+
# Check for warnings from tslint and eslint, especially in the generated code
15+
.PHONY: lint
16+
lint:
17+
npm run lint
18+
1319
.PHONY: clean
1420
clean:
1521
git clean -dfX

atdts/test/ts-tests/manual_sample.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// Handwritten code that serves as a model for generated code.
22

3+
/*
4+
TODO: don't disable tslint warnings here and change the code
5+
such that it's mostly compliant with the recommended set of warnings.
6+
*/
7+
8+
/* tslint:disable */
9+
/* eslint-disable */
10+
311
/*
412
export class Root_ {
513
kind: 'Root';
@@ -92,7 +100,7 @@ main({ kind: 'Thing', value: 42 });
92100
// main(new Root_());
93101
main({ kind: 'Root' });
94102
console.log(KindFromJSON(KindToJSON({ kind: 'Thing', value: 42 })))
95-
//_atd_missing_json_field('a', 'b')
103+
// _atd_missing_json_field('a', 'b')
96104

97105
////////////////////// Constant runtime library /////////////////////////////
98106

atdts/test/ts-tests/package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
{
22
"scripts": {
3-
"tsc": "tsc"
3+
"tsc": "tsc",
4+
"lint": "tslint --project tsconfig.json && eslint *.ts"
45
},
56
"devDependencies": {
67
"@types/node": "^17.0.25",
7-
"npx": "^10.2.2",
8+
"@typescript-eslint/eslint-plugin": "^5.49.0",
9+
"@typescript-eslint/parser": "^5.49.0",
10+
"eslint": "^8.32.0",
811
"typescript": "^4.6.3"
12+
},
13+
"dependencies": {
14+
"tslint": "^6.1.3"
915
}
1016
}

atdts/test/ts-tests/test_atdts.ts

+26-22
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
// Test JSON reading and writing against expectations.
22

3+
/* tslint:disable no-console */
4+
35
import * as API from "./everything"
46
import * as fs from "fs"
57

8+
/* eslint-disable @typescript-eslint/no-unused-vars */
69
// Check that types are exported
710
const aaa: API.Option<string> = null
11+
/* eslint-enable @typescript-eslint/no-unused-vars */
812

9-
function assert(is_true: boolean, errmsg: string) {
10-
if (!is_true) {
13+
function assert(isTrue: boolean, errmsg: string) {
14+
if (!isTrue) {
1115
throw new Error(errmsg)
1216
}
1317
}
@@ -21,7 +25,7 @@ function save(file: string, data: string) {
2125
}
2226

2327
function test_everything() {
24-
const a_obj : API.Root = {
28+
const aObj : API.Root = {
2529
id: "abc",
2630
this_: 100,
2731
items: [[], [1, 2]],
@@ -73,16 +77,16 @@ function test_everything() {
7377
any_b: [ [], [[[]]], true, {}, null ]
7478
},
7579
}
76-
const a_str = JSON.stringify(API.writeRoot(a_obj), null, 2)
77-
save('a_str', a_str)
80+
const aStr = JSON.stringify(API.writeRoot(aObj), null, 2)
81+
save('aStr', aStr)
7882

7983
console.log(
80-
`----- a_str (converted from original TS object) -----
81-
${a_str}`
84+
`----- aStr (converted from original TS object) -----
85+
${aStr}`
8286
)
8387

8488
// expected output (copy-pasted from an earlier run)
85-
const b_str =
89+
const bStr =
8690
`{
8791
"ID": "abc",
8892
"this": 100,
@@ -202,27 +206,27 @@ ${a_str}`
202206
]
203207
}
204208
}`
205-
save('b_str', b_str)
206-
const b_obj = API.readRoot(JSON.parse(a_str))
207-
const b_str2 = JSON.stringify(API.writeRoot(b_obj), null, 2)
208-
save('b_str2', b_str2)
209+
save('bStr', bStr)
210+
const bObj = API.readRoot(JSON.parse(aStr))
211+
const bStr2 = JSON.stringify(API.writeRoot(bObj), null, 2)
212+
save('bStr2', bStr2)
209213

210214
assert(
211-
b_str === b_str2,
215+
bStr === bStr2,
212216
`JSON mismatch:
213-
----- expected (b_str) -----
214-
${b_str}
215-
----- actual (b_str2) -----
216-
${b_str2}
217+
----- expected (bStr) -----
218+
${bStr}
219+
----- actual (bStr2) -----
220+
${bStr2}
217221
---------------------------`
218222
)
219223
assert(
220-
b_str2 === a_str,
224+
bStr2 === aStr,
221225
`JSON mismatch:
222-
----- expected (b_str2) -----
223-
${b_str2}
224-
----- actual (a_str) -----
225-
${a_str}
226+
----- expected (bStr2) -----
227+
${bStr2}
228+
----- actual (aStr) -----
229+
${aStr}
226230
--------------------------`
227231
)
228232
}

atdts/test/ts-tests/tslint.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "tslint:recommended"
3+
}

0 commit comments

Comments
 (0)