Skip to content

Commit

Permalink
feat(jsii): switch to disable reserved words warnings (#1076)
Browse files Browse the repository at this point in the history
* feat(jsii): switch to disable reserved words warnings

By default, jsii issues a warning when it encounters a symbol that uses a word that is reserved in one of the supported languages. Practically this creates a tremendous amount of noise for large existing projects.

#1073 proposes to add support for a warning exclusion file, which is probably a better long term solution, but in the meantime, this switch allows opting-out of reserved words warnings in order to save the planet.

Tested by adding `--disable-reserved-words-warnings` to `jsii-calc` (which uses many reserved words already).

* change boolean switch to --silence-warnings array

* Update jsii.ts

* Update packages/jsii/lib/warnings.ts

Co-Authored-By: Romain Marcadier-Muller <rmuller@amazon.com>

* Update jsii.ts

* Update warnings.ts
  • Loading branch information
Elad Ben-Israel authored and mergify[bot] committed Dec 18, 2019
1 parent 5f8a27f commit 5389def
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/jsii-calc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"build": "jsii && jsii-rosetta --compile",
"build": "jsii --silence-warnings reserved-word && jsii-rosetta --compile",
"watch": "jsii -w",
"test": "node test/test.calc.js && diff-test test/assembly.jsii .jsii",
"test:update": "npm run build && UPDATE_DIFF=1 npm run test"
Expand Down
19 changes: 18 additions & 1 deletion packages/jsii/bin/jsii.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { Compiler, DIAGNOSTICS } from '../lib/compiler';
import { loadProjectInfo } from '../lib/project-info';
import * as utils from '../lib/utils';
import { VERSION } from '../lib/version';
import { enabledWarnings } from '../lib/warnings';

const warningTypes = Object.keys(enabledWarnings);

(async () => {
const argv = yargs
Expand All @@ -27,6 +30,11 @@ import { VERSION } from '../lib/version';
type: 'boolean',
desc: 'Treat warnings as errors'
})
.option('silence-warnings', {
type: 'array',
default: [],
desc: `List of warnings to silence (warnings: ${warningTypes.join(',')})`,
})
.help()
// eslint-disable-next-line @typescript-eslint/no-require-imports
.version(`${VERSION}, typescript ${require('typescript/package.json').version}`)
Expand All @@ -38,11 +46,20 @@ import { VERSION } from '../lib/version';

const projectInfo = await loadProjectInfo(projectRoot, { fixPeerDependencies: argv['fix-peer-dependencies'] });

// disable all silenced warnings
for (const key of argv['silence-warnings']) {
if (!(key in enabledWarnings)) {
throw new Error(`Unknown warning type ${key}. Must be one of: ${warningTypes}`);
}

enabledWarnings[key] = false;
}

const compiler = new Compiler({
projectInfo,
watch: argv.watch,
projectReferences: argv['project-references'],
failOnWarnings: argv['fail-on-warnings']
failOnWarnings: argv['fail-on-warnings'],
});

return { projectRoot, emitResult: await compiler.emit() };
Expand Down
7 changes: 6 additions & 1 deletion packages/jsii/lib/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ProjectInfo } from './project-info';
import { isReservedName } from './reserved-words';
import { Validator } from './validator';
import { SHORT_VERSION, VERSION } from './version';
import { enabledWarnings } from './warnings';

// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
const sortJson = require('sort-json');
Expand Down Expand Up @@ -208,7 +209,7 @@ export class Assembler implements Emitter {
* been executed.
*
* @param fqn FQN of the current type.
* @param deps List of FQNs of types this callback depends on. All deferreds for all
* @param dependedFqns List of FQNs of types this callback depends on. All deferreds for all
* @param cb the function to be called in a deferred way. It will be bound with ``this``, so it can depend on using
* ``this``.
*/
Expand Down Expand Up @@ -1024,6 +1025,10 @@ export class Assembler implements Emitter {
}

private _warnAboutReservedWords(symbol: ts.Symbol) {
if (!enabledWarnings['reserved-word']) {
return;
}

const reservingLanguages = isReservedName(symbol.name);
if (reservingLanguages) {
this._diagnostic(ts.getNameOfDeclaration(symbol.valueDeclaration) || symbol.valueDeclaration,
Expand Down
7 changes: 7 additions & 0 deletions packages/jsii/lib/warnings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Indicates which warnings are currently enabled. By default all warnings are
* enabled, and can be silenced through the --silence-warning option.
*/
export const enabledWarnings: { [name: string]: boolean } = {
'reserved-word': true
};

0 comments on commit 5389def

Please sign in to comment.