Skip to content

Commit

Permalink
refactor: remove typecasts and non-null assertions (#1596)
Browse files Browse the repository at this point in the history
* ref: remove type casts where possible

* ref: remove unused middleware and dependencies

* ref: improve typing of JWT payload

* ref: remove non-null assertions

* ref: rename express.locals.ts

* ref: remove final non-null assertion

* chore: enforce no non-null assertion lint rule

* chore: titlecase Singpass and Corppass

* ref: use switch in extractJwtPayloadFromRequest

* ref: use switch in submissions controllers
  • Loading branch information
mantariksh authored Apr 9, 2021
1 parent 685277b commit 437e16e
Show file tree
Hide file tree
Showing 22 changed files with 293 additions and 578 deletions.
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@
{ "files": ["*.spec.ts"], "extends": ["plugin:jest/recommended"] },
{
"files": ["*.ts", "*.js"],
"excludedFiles": ["**/*.spec.ts", "**/.spec.js"],
"excludedFiles": ["**/*.spec.ts", "**/.spec.js", "**/__tests__/**/*.ts"],
"rules": {
"typesafe/no-await-without-trycatch": "warn"
"typesafe/no-await-without-trycatch": "warn",
"@typescript-eslint/no-non-null-assertion": "error"
}
}
],
Expand Down
4 changes: 0 additions & 4 deletions src/app/modules/examples/examples.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ const execExamplesQuery = (
): ResultAsync<QueryPageResult, DatabaseError> => {
return ResultAsync.fromPromise(
queryBuilder
// TODO(#42): Missing type in native typescript, waiting on upstream fixes.
// Tracking at https://github.com/Automattic/mongoose/issues/9714.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
.append(
selectAndProjectCardInfo(/* limit= */ PAGE_SIZE, /* offset= */ offset),
)
Expand Down

This file was deleted.

54 changes: 0 additions & 54 deletions src/app/modules/form/public-form/public-form.middlewares.ts

This file was deleted.

23 changes: 7 additions & 16 deletions src/app/modules/myinfo/myinfo.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,6 @@ const hasMyInfoAnswer = (
return !!field.isVisible && !!field.myInfo?.attr
}

const filterFieldsWithHashes = (
responses: ProcessedFieldResponse[],
hashes: IHashes,
): VisibleMyInfoResponse[] => {
// Filter twice to get types to cooperate
return responses
.filter(hasMyInfoAnswer)
.filter((response) => !!hashes[response.myInfo.attr])
}

const transformAnswer = (field: VisibleMyInfoResponse): string => {
const answer = field.answer
return field.fieldType === BasicField.Date
Expand All @@ -117,14 +107,15 @@ export const compareHashedValues = (
responses: ProcessedFieldResponse[],
hashes: IHashes,
): MyInfoComparePromises => {
// Filter responses to only those fields with a corresponding hash
const fieldsWithHashes = filterFieldsWithHashes(responses, hashes)
// Map MyInfoAttribute to response
const myInfoResponsesMap: MyInfoComparePromises = new Map()
fieldsWithHashes.forEach((field) => {
const attr = field.myInfo.attr
// Already checked that hashes contains this attr
myInfoResponsesMap.set(field._id, compareSingleHash(hashes[attr]!, field))
responses.forEach((field) => {
if (hasMyInfoAnswer(field)) {
const hash = hashes[field.myInfo.attr]
if (hash) {
myInfoResponsesMap.set(field._id, compareSingleHash(hash, field))
}
}
})
return myInfoResponsesMap
}
Expand Down
18 changes: 12 additions & 6 deletions src/app/modules/spcp/__tests__/spcp.factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ describe('spcp.factory', () => {
)
const fetchLoginPageResult = await SpcpFactory.fetchLoginPage('')
const validateLoginPageResult = SpcpFactory.validateLoginPage('')
const extractJwtPayloadResult = await SpcpFactory.extractJwtPayload(
const extractSingpassJwtPayloadResult = await SpcpFactory.extractSingpassJwtPayload(
'',
)
const extractCorppassJwtPayloadResult = await SpcpFactory.extractCorppassJwtPayload(
'',
AuthType.SP,
)
const parseOOBParamsResult = SpcpFactory.parseOOBParams('', '', AuthType.SP)
const getSpcpAttributesResult = await SpcpFactory.getSpcpAttributes(
Expand All @@ -51,7 +53,8 @@ describe('spcp.factory', () => {
expect(createRedirectUrlResult._unsafeUnwrapErr()).toEqual(error)
expect(fetchLoginPageResult._unsafeUnwrapErr()).toEqual(error)
expect(validateLoginPageResult._unsafeUnwrapErr()).toEqual(error)
expect(extractJwtPayloadResult._unsafeUnwrapErr()).toEqual(error)
expect(extractSingpassJwtPayloadResult._unsafeUnwrapErr()).toEqual(error)
expect(extractCorppassJwtPayloadResult._unsafeUnwrapErr()).toEqual(error)
expect(parseOOBParamsResult._unsafeUnwrapErr()).toEqual(error)
expect(getSpcpAttributesResult._unsafeUnwrapErr()).toEqual(error)
expect(createJWTResult._unsafeUnwrapErr()).toEqual(error)
Expand All @@ -74,9 +77,11 @@ describe('spcp.factory', () => {
)
const fetchLoginPageResult = await SpcpFactory.fetchLoginPage('')
const validateLoginPageResult = SpcpFactory.validateLoginPage('')
const extractJwtPayloadResult = await SpcpFactory.extractJwtPayload(
const extractSingpassJwtPayloadResult = await SpcpFactory.extractSingpassJwtPayload(
'',
)
const extractCorppassJwtPayloadResult = await SpcpFactory.extractCorppassJwtPayload(
'',
AuthType.SP,
)
const parseOOBParamsResult = SpcpFactory.parseOOBParams('', '', AuthType.SP)
const getSpcpAttributesResult = await SpcpFactory.getSpcpAttributes(
Expand All @@ -98,7 +103,8 @@ describe('spcp.factory', () => {
expect(createRedirectUrlResult._unsafeUnwrapErr()).toEqual(error)
expect(fetchLoginPageResult._unsafeUnwrapErr()).toEqual(error)
expect(validateLoginPageResult._unsafeUnwrapErr()).toEqual(error)
expect(extractJwtPayloadResult._unsafeUnwrapErr()).toEqual(error)
expect(extractSingpassJwtPayloadResult._unsafeUnwrapErr()).toEqual(error)
expect(extractCorppassJwtPayloadResult._unsafeUnwrapErr()).toEqual(error)
expect(parseOOBParamsResult._unsafeUnwrapErr()).toEqual(error)
expect(getSpcpAttributesResult._unsafeUnwrapErr()).toEqual(error)
expect(createJWTResult._unsafeUnwrapErr()).toEqual(error)
Expand Down
Loading

0 comments on commit 437e16e

Please sign in to comment.