Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rosetta): extract does not respect strict metadata entry #2863

Merged
merged 7 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/jsii-rosetta/lib/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ export function fixturize(snippet: TypeScriptSnippet): TypeScriptSnippet {
}

return {
visibleSource: snippet.visibleSource,
...snippet,
completeSource: source,
where: snippet.where,
parameters,
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/jsii-rosetta/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ export function printDiagnostic(
stream.write(message);
}

const StrictBrand = Symbol('strict');
export const StrictBrand = 'jsii.strict';
interface MaybeStrictDiagnostic {
readonly [StrictBrand]?: boolean;
}

export function annotateStrictDiagnostic(diag: ts.Diagnostic) {
Object.defineProperty(diag, StrictBrand, {
configurable: false,
enumerable: false,
enumerable: true,
value: true,
writable: false,
});
Expand Down
19 changes: 19 additions & 0 deletions packages/jsii-rosetta/test/fixtures.test.ts
Original file line number Diff line number Diff line change
@@ -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));
});
});
65 changes: 65 additions & 0 deletions packages/jsii-rosetta/test/util.test.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}