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/lib/util.ts b/packages/jsii-rosetta/lib/util.ts index b2e326ae01..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 = Symbol('strict'); +export const StrictBrand = 'jsii.strict'; 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/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)); + }); +}); diff --git a/packages/jsii-rosetta/test/util.test.ts b/packages/jsii-rosetta/test/util.test.ts new file mode 100644 index 0000000000..c565782502 --- /dev/null +++ b/packages/jsii-rosetta/test/util.test.ts @@ -0,0 +1,65 @@ +import * as ts from 'typescript'; + +import { + StrictBrand, + annotateStrictDiagnostic, + isErrorDiagnostic, +} from '../lib/util'; + +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, + }; +}