From ef1bb3141377c11f7f50eb45db695ce8919ae338 Mon Sep 17 00:00:00 2001 From: Ben Chaimberg Date: Fri, 28 May 2021 12:42:16 -0700 Subject: [PATCH 1/4] fix(rosetta): extract does not respect strict metadata entry --- packages/jsii-rosetta/lib/fixtures.ts | 3 +-- packages/jsii-rosetta/test/fixtures.test.ts | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 packages/jsii-rosetta/test/fixtures.test.ts diff --git a/packages/jsii-rosetta/lib/fixtures.ts b/packages/jsii-rosetta/lib/fixtures.ts index b12750361d..81f3d09979 100644 --- a/packages/jsii-rosetta/lib/fixtures.ts +++ b/packages/jsii-rosetta/lib/fixtures.ts @@ -39,9 +39,8 @@ export function fixturize(snippet: TypeScriptSnippet): TypeScriptSnippet { } return { - visibleSource: snippet.visibleSource, + ...snippet, completeSource: source, - where: snippet.where, parameters, }; } diff --git a/packages/jsii-rosetta/test/fixtures.test.ts b/packages/jsii-rosetta/test/fixtures.test.ts new file mode 100644 index 0000000000..92e9f9ff7c --- /dev/null +++ b/packages/jsii-rosetta/test/fixtures.test.ts @@ -0,0 +1,19 @@ +import { fixturize } from '../lib/fixtures'; +import { SnippetParameters } from '../lib/snippet'; + +describe('fixturize', () => { + test('snippet retains properties', () => { + const snippet = { + visibleSource: 'visibleSource', + where: 'where', + parameters: { + [SnippetParameters.$PROJECT_DIRECTORY]: 'directory', + [SnippetParameters.NO_FIXTURE]: '', + key: 'value', + }, + strict: true, + }; + + expect(fixturize(snippet)).toEqual(expect.objectContaining(snippet)); + }); +}); From 5c8339f401167e99cf1149edfba5cf775f452348 Mon Sep 17 00:00:00 2001 From: Ben Chaimberg Date: Fri, 28 May 2021 14:14:39 -0700 Subject: [PATCH 2/4] fix strict brand serialization --- packages/jsii-rosetta/lib/util.ts | 4 +- packages/jsii-rosetta/test/util.test.ts | 54 +++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 packages/jsii-rosetta/test/util.test.ts diff --git a/packages/jsii-rosetta/lib/util.ts b/packages/jsii-rosetta/lib/util.ts index b2e326ae01..41bc1463cd 100644 --- a/packages/jsii-rosetta/lib/util.ts +++ b/packages/jsii-rosetta/lib/util.ts @@ -38,7 +38,7 @@ export function printDiagnostic( stream.write(message); } -const StrictBrand = Symbol('strict'); +const StrictBrand = 'jsiiStrict'; interface MaybeStrictDiagnostic { readonly [StrictBrand]?: boolean; } @@ -46,7 +46,7 @@ interface MaybeStrictDiagnostic { export function annotateStrictDiagnostic(diag: ts.Diagnostic) { Object.defineProperty(diag, StrictBrand, { configurable: false, - enumerable: false, + enumerable: true, value: true, writable: false, }); diff --git a/packages/jsii-rosetta/test/util.test.ts b/packages/jsii-rosetta/test/util.test.ts new file mode 100644 index 0000000000..f5542738a3 --- /dev/null +++ b/packages/jsii-rosetta/test/util.test.ts @@ -0,0 +1,54 @@ +import * as ts from 'typescript'; +import { annotateStrictDiagnostic, isErrorDiagnostic } from '../lib/util'; + +const strictBrand = 'jsiiStrict'; + +describe(annotateStrictDiagnostic, () => { + const diagnostic = { + category: ts.DiagnosticCategory.Error, + code: 999, + messageText: 'messageText', + file: undefined, + start: undefined, + length: undefined, + }; + + test('adds strict property', () => { + annotateStrictDiagnostic(diagnostic); + + expect(diagnostic).toHaveProperty(strictBrand); + }); +}); + +describe(isErrorDiagnostic, () => { + const warningDiagnostic = makeDiagnostic(ts.DiagnosticCategory.Warning); + const errorDiagnostic = makeDiagnostic(ts.DiagnosticCategory.Error); + const strictErrorDiagnostic = { + ...makeDiagnostic(ts.DiagnosticCategory.Error), + [strictBrand]: true, + }; + const diagnostics = [warningDiagnostic, errorDiagnostic, strictErrorDiagnostic]; + + test('returns all error diagnostics if onlyStrict is false', () => { + const onlyStrict = false; + + expect(diagnostics.filter((diag) => isErrorDiagnostic(diag, { onlyStrict }))).toStrictEqual([errorDiagnostic, strictErrorDiagnostic]); + }); + + test('returns only strict error diagnostics if onlyStrict is true', () => { + const onlyStrict = true; + + expect(diagnostics.filter((diag) => isErrorDiagnostic(diag, { onlyStrict }))).toStrictEqual([strictErrorDiagnostic]); + }); +}); + +function makeDiagnostic(category: ts.DiagnosticCategory): ts.Diagnostic { + return { + category: category, + code: 999, + messageText: 'messageText', + file: undefined, + start: undefined, + length: undefined, + }; +} From d59c10db602975dac85fbe7bce3eabdbd46d70eb Mon Sep 17 00:00:00 2001 From: Ben Chaimberg Date: Fri, 28 May 2021 14:19:00 -0700 Subject: [PATCH 3/4] linting --- packages/jsii-rosetta/test/util.test.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/jsii-rosetta/test/util.test.ts b/packages/jsii-rosetta/test/util.test.ts index f5542738a3..1c9258467e 100644 --- a/packages/jsii-rosetta/test/util.test.ts +++ b/packages/jsii-rosetta/test/util.test.ts @@ -1,4 +1,5 @@ import * as ts from 'typescript'; + import { annotateStrictDiagnostic, isErrorDiagnostic } from '../lib/util'; const strictBrand = 'jsiiStrict'; @@ -27,18 +28,26 @@ describe(isErrorDiagnostic, () => { ...makeDiagnostic(ts.DiagnosticCategory.Error), [strictBrand]: true, }; - const diagnostics = [warningDiagnostic, errorDiagnostic, strictErrorDiagnostic]; + const diagnostics = [ + warningDiagnostic, + errorDiagnostic, + strictErrorDiagnostic, + ]; test('returns all error diagnostics if onlyStrict is false', () => { const onlyStrict = false; - expect(diagnostics.filter((diag) => isErrorDiagnostic(diag, { onlyStrict }))).toStrictEqual([errorDiagnostic, strictErrorDiagnostic]); + expect( + diagnostics.filter((diag) => isErrorDiagnostic(diag, { onlyStrict })), + ).toStrictEqual([errorDiagnostic, strictErrorDiagnostic]); }); test('returns only strict error diagnostics if onlyStrict is true', () => { const onlyStrict = true; - expect(diagnostics.filter((diag) => isErrorDiagnostic(diag, { onlyStrict }))).toStrictEqual([strictErrorDiagnostic]); + expect( + diagnostics.filter((diag) => isErrorDiagnostic(diag, { onlyStrict })), + ).toStrictEqual([strictErrorDiagnostic]); }); }); From 2c495861f26ca8a13c15f4794b02b34b52bce9dd Mon Sep 17 00:00:00 2001 From: Ben Chaimberg Date: Sun, 6 Jun 2021 06:47:17 -0700 Subject: [PATCH 4/4] rename strict brand --- packages/jsii-rosetta/lib/util.ts | 2 +- packages/jsii-rosetta/test/util.test.ts | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/jsii-rosetta/lib/util.ts b/packages/jsii-rosetta/lib/util.ts index 41bc1463cd..888cc672bb 100644 --- a/packages/jsii-rosetta/lib/util.ts +++ b/packages/jsii-rosetta/lib/util.ts @@ -38,7 +38,7 @@ export function printDiagnostic( stream.write(message); } -const StrictBrand = 'jsiiStrict'; +export const StrictBrand = 'jsii.strict'; interface MaybeStrictDiagnostic { readonly [StrictBrand]?: boolean; } diff --git a/packages/jsii-rosetta/test/util.test.ts b/packages/jsii-rosetta/test/util.test.ts index 1c9258467e..c565782502 100644 --- a/packages/jsii-rosetta/test/util.test.ts +++ b/packages/jsii-rosetta/test/util.test.ts @@ -1,8 +1,10 @@ import * as ts from 'typescript'; -import { annotateStrictDiagnostic, isErrorDiagnostic } from '../lib/util'; - -const strictBrand = 'jsiiStrict'; +import { + StrictBrand, + annotateStrictDiagnostic, + isErrorDiagnostic, +} from '../lib/util'; describe(annotateStrictDiagnostic, () => { const diagnostic = { @@ -17,7 +19,7 @@ describe(annotateStrictDiagnostic, () => { test('adds strict property', () => { annotateStrictDiagnostic(diagnostic); - expect(diagnostic).toHaveProperty(strictBrand); + expect(diagnostic).toHaveProperty([StrictBrand]); }); }); @@ -26,7 +28,7 @@ describe(isErrorDiagnostic, () => { const errorDiagnostic = makeDiagnostic(ts.DiagnosticCategory.Error); const strictErrorDiagnostic = { ...makeDiagnostic(ts.DiagnosticCategory.Error), - [strictBrand]: true, + [StrictBrand]: true, }; const diagnostics = [ warningDiagnostic,