-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style(lint): use optional properties/fields/params
- Loading branch information
1 parent
73964df
commit 40aec93
Showing
23 changed files
with
323 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
apps/precinct-scanner/src/components/ExportResultsModal.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Use optional fields (on interfaces or classes) and parameters rather than a `|undefined` type. (`vx/gts-use-optionals`) | ||
|
||
This rule is from | ||
[Google TypeScript Style Guide section "Optionals vs `|undefined` type"](https://google.github.io/styleguide/tsguide.html#optionals-vs-undefined-type): | ||
|
||
> TypeScript supports a special construct for optional parameters and fields, | ||
> using `?`. Optional parameters implicitly include |undefined in their type. | ||
> However, they are different in that they can be left out when constructing a | ||
> value or calling a method. Use optional fields (on interfaces or classes) and | ||
> parameters rather than a `|undefined` type. | ||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
interface CoffeeOrder { | ||
sugarCubes: number | ||
milk: Whole | LowFat | HalfHalf | undefined | ||
} | ||
|
||
function pourCoffee(volume: Milliliter | undefined) { | ||
/* … */ | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
interface CoffeeOrder { | ||
sugarCubes: number | ||
milk?: Whole | LowFat | HalfHalf | ||
} | ||
|
||
function pourCoffee(volume?: Milliliter) { | ||
/* … */ | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/experimental-utils' | ||
import { createRule, isBindingName } from '../util' | ||
|
||
function isOptionalType(node: TSESTree.TypeNode): boolean { | ||
if (node.type === AST_NODE_TYPES.TSUnionType) { | ||
return node.types.some( | ||
(elementType) => | ||
elementType.type === AST_NODE_TYPES.TSUndefinedKeyword || | ||
isOptionalType(elementType) | ||
) | ||
} | ||
|
||
if (node.type === AST_NODE_TYPES.TSTypeReference) { | ||
return ( | ||
node.typeName.type === AST_NODE_TYPES.Identifier && | ||
node.typeName.name === 'Optional' | ||
) | ||
} | ||
|
||
return false | ||
} | ||
|
||
export default createRule({ | ||
name: 'gts-use-optionals', | ||
meta: { | ||
docs: { | ||
description: | ||
'Use optional fields (on interfaces or classes) and parameters rather than a |undefined type.', | ||
category: 'Best Practices', | ||
recommended: 'error', | ||
suggestion: false, | ||
requiresTypeChecking: false, | ||
}, | ||
messages: { | ||
useOptionalInterfaceProperties: `Use optional properties on interfaces rather than a |undefined type`, | ||
useOptionalClassFields: `Use optional fields on classes rather than a |undefined type`, | ||
useOptionalParams: `Use optional params rather than a |undefined type`, | ||
}, | ||
schema: [], | ||
type: 'problem', | ||
}, | ||
defaultOptions: [], | ||
|
||
create(context) { | ||
function checkFunction(node: { | ||
params: readonly TSESTree.Parameter[] | ||
}): void { | ||
const possibleViolations = node.params.map((param) => { | ||
if ( | ||
isBindingName(param) && | ||
param.typeAnnotation && | ||
isOptionalType(param.typeAnnotation.typeAnnotation) | ||
) { | ||
return param | ||
} | ||
|
||
if ( | ||
param.type === AST_NODE_TYPES.AssignmentPattern && | ||
param.left.typeAnnotation && | ||
isOptionalType(param.left.typeAnnotation.typeAnnotation) | ||
) { | ||
return param | ||
} | ||
|
||
return undefined | ||
}) | ||
|
||
let indexOfLastNonOptionalParam = -1 | ||
for (const [i, param] of node.params.entries()) { | ||
if (isBindingName(param) && !param.optional) { | ||
indexOfLastNonOptionalParam = i | ||
} | ||
} | ||
|
||
for (const [i, param] of possibleViolations.entries()) { | ||
if ( | ||
// do not report params that come before non-optional params | ||
i < indexOfLastNonOptionalParam || | ||
!param | ||
) { | ||
continue | ||
} | ||
|
||
context.report({ | ||
messageId: 'useOptionalParams', | ||
node: param, | ||
}) | ||
} | ||
} | ||
|
||
return { | ||
ClassProperty(node: TSESTree.ClassProperty): void { | ||
if ( | ||
node.typeAnnotation && | ||
isOptionalType(node.typeAnnotation.typeAnnotation) | ||
) { | ||
context.report({ | ||
messageId: 'useOptionalClassFields', | ||
node, | ||
}) | ||
} | ||
}, | ||
|
||
FunctionDeclaration: checkFunction, | ||
FunctionExpression: checkFunction, | ||
ArrowFunctionExpression: checkFunction, | ||
TSFunctionType: checkFunction, | ||
TSMethodSignature: checkFunction, | ||
|
||
TSParameterProperty(node: TSESTree.TSParameterProperty): void { | ||
if ( | ||
node.parameter.typeAnnotation && | ||
isOptionalType(node.parameter.typeAnnotation.typeAnnotation) | ||
) { | ||
context.report({ | ||
messageId: 'useOptionalClassFields', | ||
node, | ||
}) | ||
} | ||
}, | ||
|
||
TSPropertySignature(node: TSESTree.TSPropertySignature): void { | ||
if ( | ||
node.typeAnnotation && | ||
isOptionalType(node.typeAnnotation.typeAnnotation) | ||
) { | ||
context.report({ | ||
messageId: 'useOptionalInterfaceProperties', | ||
node, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.