Skip to content

add transformNonNullExpressions #100

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

Merged
merged 1 commit into from
Nov 1, 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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ There are some options to configure the transformer.
| `ignoreFunctions` *(deprecated, use `functionBehavior` instead)* | Boolean (default: `false`). If `true`, when the transformer encounters a function, it will ignore it and simply return `true`. If `false`, an error is generated at compile time. |
| `functionBehavior` | One of `error`, `ignore`, or `basic` (default: `error`). Determines the behavior of transformer when encountering a function. `error` will cause a compile-time error, `ignore` will cause the validation function to always return `true`, and `basic` will do a simple function-type-check. Overrides `ignoreFunctions`. |
| `disallowSuperfluousObjectProperties` | Boolean (default: `false`). If `true`, objects are checked for having superfluous properties and will cause the validation to fail if they do. If `false`, no check for superfluous properties is made. |
| `transformNonNullExpressions` | Boolean (default: `false`). If `true`, non-null expressions (eg. `foo!.bar`) are checked to not be `null` or `undefined` |

If you are using `ttypescript`, you can include the options in your `tsconfig.json`:

Expand All @@ -150,7 +151,8 @@ If you are using `ttypescript`, you can include the options in your `tsconfig.js
"ignoreClasses": true,
"ignoreMethods": true,
"functionBehavior": "ignore",
"disallowSuperfluousObjectProperties": true
"disallowSuperfluousObjectProperties": true,
"transformNonNullExpressions": true
}
]
}
Expand Down
49 changes: 49 additions & 0 deletions src/transform-inline/transform-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,55 @@ export function transformNode(node: ts.Node, visitorContext: PartialVisitorConte
]
);
}
} else if (visitorContext.options.transformNonNullExpressions && ts.isNonNullExpression(node)) {
const expression = node.expression
return ts.factory.updateNonNullExpression(node, ts.factory.createParenthesizedExpression(ts.factory.createConditionalExpression(
ts.factory.createParenthesizedExpression(ts.factory.createBinaryExpression(
ts.factory.createBinaryExpression(
ts.factory.createTypeOfExpression(expression),
ts.factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken),
ts.factory.createStringLiteral('undefined')
),
ts.factory.createToken(ts.SyntaxKind.BarBarToken),
ts.factory.createBinaryExpression(
expression,
ts.factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken),
ts.factory.createNull()
)
)),
ts.factory.createToken(ts.SyntaxKind.QuestionToken),
ts.factory.createCallExpression(
ts.factory.createParenthesizedExpression(ts.factory.createArrowFunction(
undefined,
undefined,
[],
undefined,
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
ts.factory.createBlock(
[ts.factory.createThrowStatement(ts.factory.createNewExpression(
ts.factory.createIdentifier('Error'),
undefined,
[ts.factory.createTemplateExpression(
ts.factory.createTemplateHead(`${expression.getText()} was non-null asserted but is `),
[ts.factory.createTemplateSpan(
expression,
ts.factory.createTemplateTail(
''
)
)]
)]
))
],
false
)
)),
undefined,
[]
),
ts.factory.createToken(ts.SyntaxKind.ColonToken),
expression
)))
}
return node;
}

3 changes: 2 additions & 1 deletion src/transform-inline/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export default function transformer(program: ts.Program, options?: { [Key: strin
ignoreClasses: !!(options && options.ignoreClasses),
ignoreMethods: !!(options && options.ignoreMethods),
functionBehavior: getFunctionBehavior(options),
disallowSuperfluousObjectProperties: !!(options && options.disallowSuperfluousObjectProperties)
disallowSuperfluousObjectProperties: !!(options && options.disallowSuperfluousObjectProperties),
transformNonNullExpressions: !!(options && options.transformNonNullExpressions)
},
typeMapperStack: [],
previousTypeReference: null
Expand Down
1 change: 1 addition & 0 deletions src/transform-inline/visitor-context.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface Options {
ignoreMethods: boolean;
functionBehavior: 'error' | 'ignore' | 'basic';
disallowSuperfluousObjectProperties: boolean;
transformNonNullExpressions: boolean;
}

export interface VisitorContext extends PartialVisitorContext {
Expand Down
15 changes: 10 additions & 5 deletions test/issue-16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ describe('visitor', () => {
ignoreMethods: false,
functionBehavior: 'error',
shortCircuit: false,
disallowSuperfluousObjectProperties: false
disallowSuperfluousObjectProperties: false,
transformNonNullExpressions: false
},
typeMapperStack: [],
previousTypeReference: null
Expand All @@ -61,7 +62,8 @@ describe('visitor', () => {
ignoreMethods: false,
functionBehavior: 'error',
shortCircuit: false,
disallowSuperfluousObjectProperties: false
disallowSuperfluousObjectProperties: false,
transformNonNullExpressions: false
},
typeMapperStack: [],
previousTypeReference: null
Expand Down Expand Up @@ -90,7 +92,8 @@ describe('visitor', () => {
ignoreMethods: true,
functionBehavior: 'error',
shortCircuit: false,
disallowSuperfluousObjectProperties: false
disallowSuperfluousObjectProperties: false,
transformNonNullExpressions: false
},
typeMapperStack: [],
previousTypeReference: null
Expand Down Expand Up @@ -118,7 +121,8 @@ describe('visitor', () => {
ignoreMethods: false,
functionBehavior: 'error',
shortCircuit: false,
disallowSuperfluousObjectProperties: false
disallowSuperfluousObjectProperties: false,
transformNonNullExpressions: false
},
typeMapperStack: [],
previousTypeReference: null
Expand Down Expand Up @@ -146,7 +150,8 @@ describe('visitor', () => {
ignoreMethods: true,
functionBehavior: 'error',
shortCircuit: false,
disallowSuperfluousObjectProperties: false
disallowSuperfluousObjectProperties: false,
transformNonNullExpressions: false
},
typeMapperStack: [],
previousTypeReference: null
Expand Down
3 changes: 2 additions & 1 deletion test/issue-27.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ describe('visitor', () => {
ignoreMethods: true,
functionBehavior: 'error',
shortCircuit: false,
disallowSuperfluousObjectProperties: false
disallowSuperfluousObjectProperties: false,
transformNonNullExpressions: false
},
typeMapperStack: [],
previousTypeReference: null
Expand Down
3 changes: 2 additions & 1 deletion test/issue-3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ describe('visitor', () => {
ignoreMethods: false,
functionBehavior: 'error',
shortCircuit: false,
disallowSuperfluousObjectProperties: false
disallowSuperfluousObjectProperties: false,
transformNonNullExpressions: false
},
typeMapperStack: [],
previousTypeReference: null
Expand Down
6 changes: 4 additions & 2 deletions test/issue-31.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ describe('visitor', () => {
ignoreMethods: true, // Make sure it does not fail on the methods.
functionBehavior: 'error',
shortCircuit: false,
disallowSuperfluousObjectProperties: false
disallowSuperfluousObjectProperties: false,
transformNonNullExpressions: false
};
const visitorContext = createVisitorContext(program, options);
const visitorContextWithDate = createVisitorContext(programWithDate, options);
Expand Down Expand Up @@ -77,7 +78,8 @@ describe('visitor', () => {
ignoreMethods: false, // It should never get to the methods of the class.
functionBehavior: 'error',
shortCircuit: false,
disallowSuperfluousObjectProperties: false
disallowSuperfluousObjectProperties: false,
transformNonNullExpressions: false
};
const visitorContext = createVisitorContext(program, options);
const visitorContextWithDate = createVisitorContext(programWithDate, options);
Expand Down
6 changes: 4 additions & 2 deletions test/issue-50.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ describe('visitor', () => {
ignoreMethods: true, // Make sure that the function is not seen as a method.
functionBehavior: 'error',
shortCircuit: false,
disallowSuperfluousObjectProperties: false
disallowSuperfluousObjectProperties: false,
transformNonNullExpressions: false
},
typeMapperStack: [],
previousTypeReference: null
Expand Down Expand Up @@ -63,7 +64,8 @@ describe('visitor', () => {
ignoreMethods: false,
functionBehavior: 'ignore',
shortCircuit: false,
disallowSuperfluousObjectProperties: false
disallowSuperfluousObjectProperties: false,
transformNonNullExpressions: false
},
typeMapperStack: [],
previousTypeReference: null
Expand Down
9 changes: 6 additions & 3 deletions test/issue-52.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ describe('visitor', () => {
ignoreMethods: true, // Make sure that the function is not seen as a method.
functionBehavior: 'error',
shortCircuit: false,
disallowSuperfluousObjectProperties: false
disallowSuperfluousObjectProperties: false,
transformNonNullExpressions: false
},
typeMapperStack: [],
previousTypeReference: null
Expand Down Expand Up @@ -63,7 +64,8 @@ describe('visitor', () => {
ignoreMethods: false,
functionBehavior: 'ignore',
shortCircuit: false,
disallowSuperfluousObjectProperties: false
disallowSuperfluousObjectProperties: false,
transformNonNullExpressions: false
},
typeMapperStack: [],
previousTypeReference: null
Expand Down Expand Up @@ -91,7 +93,8 @@ describe('visitor', () => {
ignoreMethods: false,
functionBehavior: 'basic',
shortCircuit: false,
disallowSuperfluousObjectProperties: false
disallowSuperfluousObjectProperties: false,
transformNonNullExpressions: false
},
typeMapperStack: [],
previousTypeReference: null
Expand Down
16 changes: 16 additions & 0 deletions test/issue-98.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as assert from 'assert';

describe('transformNonNullAssertions', () => {
it('should be null', () => {
const foo: { bar: number | null } = {bar: null}
assert.throws(() => foo.bar!, {message: 'foo.bar was non-null asserted but is null'})
})
it('should be undefined', () => {
const foo: { bar?: number } = {}
assert.throws(() => foo.bar!, {message: 'foo.bar was non-null asserted but is undefined'})
})
it('should not throw', () => {
const foo: { bar?: number } = {bar: 1}
assert.equal(foo.bar!, 1)
})
})
5 changes: 3 additions & 2 deletions tsconfig-test.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
"declaration": false,
"plugins": [
{
"transform": "./src/transform-inline/transformer.ts"
"transform": "./src/transform-inline/transformer.ts",
"transformNonNullExpressions": true
}
]
},
"include": [
"test"
]
}
}