Skip to content

Commit

Permalink
Disable all tslint and eslint rules in generated code (#328)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mjambon authored Jan 24, 2023
1 parent 67405ec commit 6a7399a
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 49 deletions.
11 changes: 8 additions & 3 deletions .circleci/setup-system
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ set -eu

sudo apt-get update

# Add repo to install a sufficiently-recent version of node (for atdts tests)
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -

# This includes compilers and libraries to support the various target
# languages of atd.
#
sudo apt-get install -y \
default-jdk \
nodejs \
npm \
python3 \
python3-pip \
python-is-python3 \
Expand Down Expand Up @@ -49,5 +51,8 @@ python3 -m pytest --version
echo 'check mypy'
python3 -m mypy --version

echo 'check js'
js --version
echo 'check Node.js'
node --version

echo 'check npm'
npm --version
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* atdts: Eliminate the type alias `type Int = number` since it was
more confusing than helpful. Occurrences of `Int` are replaced
by `number /*int*/`.
* atdts: Disable all tslint and eslint rules in generated code so as
to play well with all tslint and eslint configurations.

2.10.0 (2022-08-09)
-------------------
Expand Down
22 changes: 13 additions & 9 deletions atdts/src/lib/Codegen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,19 @@ let double_esc s =
escape_string_content Double s

let runtime_start atd_filename =
sprintf {|// Generated by atdts from type definitions in '%s'.
//
// Type-safe translations from/to JSON
//
// For each type 'Foo', there is a pair of functions:
// - 'writeFoo': convert a 'Foo' value into a JSON-compatible value.
// - 'readFoo': convert a JSON-compatible value into a TypeScript value
// of type 'Foo'.
|}
sprintf {|/*
Generated by atdts from type definitions in '%s'.

Type-safe translations from/to JSON

For each type 'Foo', there is a pair of functions:
- 'writeFoo': convert a 'Foo' value into a JSON-compatible value.
- 'readFoo': convert a JSON-compatible value into a TypeScript value
of type 'Foo'.
*/

/* tslint:disable */
/* eslint-disable */|}
atd_filename

let runtime_end = {|
Expand Down
20 changes: 12 additions & 8 deletions atdts/test/ts-expected/everything.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// Generated by atdts from type definitions in 'everything.atd'.
//
// Type-safe translations from/to JSON
//
// For each type 'Foo', there is a pair of functions:
// - 'writeFoo': convert a 'Foo' value into a JSON-compatible value.
// - 'readFoo': convert a JSON-compatible value into a TypeScript value
// of type 'Foo'.
/*
Generated by atdts from type definitions in 'everything.atd'.
Type-safe translations from/to JSON
For each type 'Foo', there is a pair of functions:
- 'writeFoo': convert a 'Foo' value into a JSON-compatible value.
- 'readFoo': convert a JSON-compatible value into a TypeScript value
of type 'Foo'.
*/

/* tslint:disable */
/* eslint-disable */

export type Anything = any

Expand Down
22 changes: 22 additions & 0 deletions atdts/test/ts-tests/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"overrides": [
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
}
}
6 changes: 3 additions & 3 deletions atdts/test/ts-tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
/package-lock.json

# Test output
/a_str
/b_str
/b_str2
/aStr
/bStr
/bStr2
8 changes: 7 additions & 1 deletion atdts/test/ts-tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
JS ?= js
JS ?= node

.PHONY: test
test:
# Install TypeScript and JavaScript dependencies locally
npm install
$(MAKE) lint
# Compile TypeScript (handwritten + generated by atdts) to JavaScript
npm run tsc
# Run the resulting code (compiled from TypeScript)
$(JS) manual_sample.js
$(JS) test_atdts.js

# Check for warnings from tslint and eslint, especially in the generated code
.PHONY: lint
lint:
npm run lint

.PHONY: clean
clean:
git clean -dfX
10 changes: 9 additions & 1 deletion atdts/test/ts-tests/manual_sample.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// Handwritten code that serves as a model for generated code.

/*
TODO: don't disable tslint warnings here and change the code
such that it's mostly compliant with the recommended set of warnings.
*/

/* tslint:disable */
/* eslint-disable */

/*
export class Root_ {
kind: 'Root';
Expand Down Expand Up @@ -92,7 +100,7 @@ main({ kind: 'Thing', value: 42 });
// main(new Root_());
main({ kind: 'Root' });
console.log(KindFromJSON(KindToJSON({ kind: 'Thing', value: 42 })))
//_atd_missing_json_field('a', 'b')
// _atd_missing_json_field('a', 'b')

////////////////////// Constant runtime library /////////////////////////////

Expand Down
10 changes: 8 additions & 2 deletions atdts/test/ts-tests/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
{
"scripts": {
"tsc": "tsc"
"tsc": "tsc",
"lint": "tslint --project tsconfig.json && eslint *.ts"
},
"devDependencies": {
"@types/node": "^17.0.25",
"npx": "^10.2.2",
"@typescript-eslint/eslint-plugin": "^5.49.0",
"@typescript-eslint/parser": "^5.49.0",
"eslint": "^8.32.0",
"typescript": "^4.6.3"
},
"dependencies": {
"tslint": "^6.1.3"
}
}
48 changes: 26 additions & 22 deletions atdts/test/ts-tests/test_atdts.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Test JSON reading and writing against expectations.

/* tslint:disable no-console */

import * as API from "./everything"
import * as fs from "fs"

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

function assert(is_true: boolean, errmsg: string) {
if (!is_true) {
function assert(isTrue: boolean, errmsg: string) {
if (!isTrue) {
throw new Error(errmsg)
}
}
Expand All @@ -21,7 +25,7 @@ function save(file: string, data: string) {
}

function test_everything() {
const a_obj : API.Root = {
const aObj : API.Root = {
id: "abc",
this_: 100,
items: [[], [1, 2]],
Expand Down Expand Up @@ -73,16 +77,16 @@ function test_everything() {
any_b: [ [], [[[]]], true, {}, null ]
},
}
const a_str = JSON.stringify(API.writeRoot(a_obj), null, 2)
save('a_str', a_str)
const aStr = JSON.stringify(API.writeRoot(aObj), null, 2)
save('aStr', aStr)

console.log(
`----- a_str (converted from original TS object) -----
${a_str}`
`----- aStr (converted from original TS object) -----
${aStr}`
)

// expected output (copy-pasted from an earlier run)
const b_str =
const bStr =
`{
"ID": "abc",
"this": 100,
Expand Down Expand Up @@ -202,27 +206,27 @@ ${a_str}`
]
}
}`
save('b_str', b_str)
const b_obj = API.readRoot(JSON.parse(a_str))
const b_str2 = JSON.stringify(API.writeRoot(b_obj), null, 2)
save('b_str2', b_str2)
save('bStr', bStr)
const bObj = API.readRoot(JSON.parse(aStr))
const bStr2 = JSON.stringify(API.writeRoot(bObj), null, 2)
save('bStr2', bStr2)

assert(
b_str === b_str2,
bStr === bStr2,
`JSON mismatch:
----- expected (b_str) -----
${b_str}
----- actual (b_str2) -----
${b_str2}
----- expected (bStr) -----
${bStr}
----- actual (bStr2) -----
${bStr2}
---------------------------`
)
assert(
b_str2 === a_str,
bStr2 === aStr,
`JSON mismatch:
----- expected (b_str2) -----
${b_str2}
----- actual (a_str) -----
${a_str}
----- expected (bStr2) -----
${bStr2}
----- actual (aStr) -----
${aStr}
--------------------------`
)
}
Expand Down
3 changes: 3 additions & 0 deletions atdts/test/ts-tests/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "tslint:recommended"
}

0 comments on commit 6a7399a

Please sign in to comment.