From 6a7399adff291c5dc2e5a1d31cd6f303bb4c8cf9 Mon Sep 17 00:00:00 2001 From: Martin Jambon Date: Mon, 23 Jan 2023 22:20:25 -0800 Subject: [PATCH] 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 --- .circleci/setup-system | 11 +++++-- CHANGES.md | 2 ++ atdts/src/lib/Codegen.ml | 22 +++++++------ atdts/test/ts-expected/everything.ts | 20 +++++++----- atdts/test/ts-tests/.eslintrc.json | 22 +++++++++++++ atdts/test/ts-tests/.gitignore | 6 ++-- atdts/test/ts-tests/Makefile | 8 ++++- atdts/test/ts-tests/manual_sample.ts | 10 +++++- atdts/test/ts-tests/package.json | 10 ++++-- atdts/test/ts-tests/test_atdts.ts | 48 +++++++++++++++------------- atdts/test/ts-tests/tslint.json | 3 ++ 11 files changed, 113 insertions(+), 49 deletions(-) create mode 100644 atdts/test/ts-tests/.eslintrc.json create mode 100644 atdts/test/ts-tests/tslint.json diff --git a/.circleci/setup-system b/.circleci/setup-system index ffa5f009..2367ca0d 100755 --- a/.circleci/setup-system +++ b/.circleci/setup-system @@ -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 \ @@ -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 diff --git a/CHANGES.md b/CHANGES.md index a434fc44..01dfb8c6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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) ------------------- diff --git a/atdts/src/lib/Codegen.ml b/atdts/src/lib/Codegen.ml index 453a2081..b41fd2c8 100644 --- a/atdts/src/lib/Codegen.ml +++ b/atdts/src/lib/Codegen.ml @@ -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 = {| diff --git a/atdts/test/ts-expected/everything.ts b/atdts/test/ts-expected/everything.ts index 5dbff4df..25f75a5a 100644 --- a/atdts/test/ts-expected/everything.ts +++ b/atdts/test/ts-expected/everything.ts @@ -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 diff --git a/atdts/test/ts-tests/.eslintrc.json b/atdts/test/ts-tests/.eslintrc.json new file mode 100644 index 00000000..fb4a101d --- /dev/null +++ b/atdts/test/ts-tests/.eslintrc.json @@ -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": { + } +} diff --git a/atdts/test/ts-tests/.gitignore b/atdts/test/ts-tests/.gitignore index 7c4e4597..55cad7d3 100644 --- a/atdts/test/ts-tests/.gitignore +++ b/atdts/test/ts-tests/.gitignore @@ -2,6 +2,6 @@ /package-lock.json # Test output -/a_str -/b_str -/b_str2 +/aStr +/bStr +/bStr2 diff --git a/atdts/test/ts-tests/Makefile b/atdts/test/ts-tests/Makefile index d1ed7e74..91f69e18 100644 --- a/atdts/test/ts-tests/Makefile +++ b/atdts/test/ts-tests/Makefile @@ -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 diff --git a/atdts/test/ts-tests/manual_sample.ts b/atdts/test/ts-tests/manual_sample.ts index 6370d679..93f2aaa9 100644 --- a/atdts/test/ts-tests/manual_sample.ts +++ b/atdts/test/ts-tests/manual_sample.ts @@ -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'; @@ -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 ///////////////////////////// diff --git a/atdts/test/ts-tests/package.json b/atdts/test/ts-tests/package.json index 6a498bbd..db1e520d 100644 --- a/atdts/test/ts-tests/package.json +++ b/atdts/test/ts-tests/package.json @@ -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" } } diff --git a/atdts/test/ts-tests/test_atdts.ts b/atdts/test/ts-tests/test_atdts.ts index bae03560..f8332d5f 100644 --- a/atdts/test/ts-tests/test_atdts.ts +++ b/atdts/test/ts-tests/test_atdts.ts @@ -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 = 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) } } @@ -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]], @@ -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, @@ -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} --------------------------` ) } diff --git a/atdts/test/ts-tests/tslint.json b/atdts/test/ts-tests/tslint.json new file mode 100644 index 00000000..78af2344 --- /dev/null +++ b/atdts/test/ts-tests/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "tslint:recommended" +}