β¬ οΈ Back to Table of Contents
π no-non-null-asserted-optional-chain¶
π Analysis Summary¶
| Metric | Count |
|---|---|
| π§ Functions | 1 |
| π¦ Imports | 3 |
π Table of Contents¶
π οΈ File Location:¶
π packages/eslint-plugin/src/rules/no-non-null-asserted-optional-chain.ts
π€ Default Export¶
| Property | Value |
|---|---|
name |
'no-non-null-asserted-optional-chain' |
meta.type |
'problem' |
meta.docs.description |
'Disallow non-null assertions after an optional chain expression' |
meta.docs.recommended |
'recommended' |
meta.hasSuggestions |
true |
meta.messages.noNonNullOptionalChain |
'Optional chain expressions can return undefined by design - using a non-null assertion is unsafe and wrong.' |
meta.messages.suggestRemovingNonNull |
'You should remove the non-null assertion.' |
meta.schema |
[] |
defaultOptions |
[] |
Entry point: create β documented under Functions.
π¦ Imports¶
| Name | Source |
|---|---|
TSESLint |
@typescript-eslint/utils |
TSESTree |
@typescript-eslint/utils |
createRule |
../util |
Functions¶
create(context: any): { 'TSNonNullExpression > ChainExpression'(node: TSESTree.Ch⦶
Parameters:
contextany
Returns: { 'TSNonNullExpression > ChainExpression'(node: TSESTree.ChainExpression): void; 'ChainExpression > TSNonNullExpression'(node: TSESTree.TSNonNullExpression): void; }
Calls:
context.reportfixer.removeRange
Internal Comments:
// non-nulling a wrapped chain will scrub all nulls introduced by the chain (x2)
// (x?.y)! (x2)
// (x?.())! (x2)
// selector guarantees this assertion (x2)
// use a suggestion instead of a fixer, because this can obviously break type checks (x4)
// non-nulling at the end of a chain will scrub all nulls introduced by the chain (x2)
// x?.y! (x2)
// x?.()! (x2)
Code
create(context) {
return {
// non-nulling a wrapped chain will scrub all nulls introduced by the chain
// (x?.y)!
// (x?.())!
'TSNonNullExpression > ChainExpression'(
node: TSESTree.ChainExpression,
): void {
// selector guarantees this assertion
const parent = node.parent as TSESTree.TSNonNullExpression;
context.report({
node,
messageId: 'noNonNullOptionalChain',
// use a suggestion instead of a fixer, because this can obviously break type checks
suggest: [
{
messageId: 'suggestRemovingNonNull',
fix(fixer): TSESLint.RuleFix {
return fixer.removeRange([
parent.range[1] - 1,
parent.range[1],
]);
},
},
],
});
},
// non-nulling at the end of a chain will scrub all nulls introduced by the chain
// x?.y!
// x?.()!
'ChainExpression > TSNonNullExpression'(
node: TSESTree.TSNonNullExpression,
): void {
context.report({
node,
messageId: 'noNonNullOptionalChain',
// use a suggestion instead of a fixer, because this can obviously break type checks
suggest: [
{
messageId: 'suggestRemovingNonNull',
fix(fixer): TSESLint.RuleFix {
return fixer.removeRange([node.range[1] - 1, node.range[1]]);
},
},
],
});
},
};
}
Generated by Syntax Scribe