Skip to content

Commit

Permalink
[#IP-86] tslint to eslint migration (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldisaro authored Apr 14, 2021
1 parent 4288a76 commit c3bc174
Show file tree
Hide file tree
Showing 78 changed files with 2,205 additions and 1,049 deletions.
23 changes: 23 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
env: {
browser: true,
es6: true,
node: true
},
ignorePatterns: [
"node_modules",
"generated",
"**/__tests__/*",
"**/__mocks__/*",
"**/__integrations__/*",
"Dangerfile.*",
"*.d.ts"
],
parser: "@typescript-eslint/parser",
parserOptions: {
project: "tsconfig.json",
sourceType: "module"
},
extends: ["@pagopa/eslint-config/strong"],
rules: {}
};
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ coverage
.vscode
.npmrc
.env

# eslint section
!.eslintrc.js
.eslintcache
1 change: 0 additions & 1 deletion Dangerfile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// import custom DangerJS rules
// see http://danger.systems/js
// see https://github.com/teamdigitale/danger-plugin-digitalcitizenship/
// tslint:disable-next-line:prettier
import checkDangers from 'danger-plugin-digitalcitizenship';

checkDangers();
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const toMjml = (content, options) => {
return [
`// DO NOT EDIT THIS FILE`,
`// this file was auto generated from '${name}' by gulp generate:templates`,
`// tslint:disable-next-line:parameters-max-number`,
`// eslint-disable-next-line max-params`,
`export default function(`,
` title: string,`,
` headlineText: string,`,
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"test": "jest -i",
"test:integration": "yarn build && cd ./__integrations__ && yarn install --frozen-lockfile && yarn start",
"test:all": "yarn test && yarn test:integration",
"lint": "tslint --project .",
"lint": "eslint . -c .eslintrc.js --ext .ts,.tsx",
"generate:all": "npm-run-all generate:*",
"generate:definitions": "gen-api-models --api-spec ./openapi/index.yaml --out-dir ./generated/definitions",
"generate:templates": "gulp generate:templates",
Expand All @@ -25,6 +25,7 @@
"devDependencies": {
"@azure/functions": "^1.0.3",
"@babel/runtime": "^7.9.0",
"@pagopa/eslint-config": "^1.3.1",
"@types/express": "^4.17.0",
"@types/helmet": "^0.0.40",
"@types/jest": "^24.0.13",
Expand All @@ -37,19 +38,18 @@
"auto-changelog": "^2.2.1",
"danger": "^7.0.0",
"danger-plugin-digitalcitizenship": "*",
"eslint-plugin-prettier": "^3.3.1",
"gulp": "^3.9.1",
"gulp-prettier": "^3.0.0",
"gulp-rename": "^2.0.0",
"gulp-text-simple": "^0.5.4",
"italia-tslint-rules": "*",
"italia-utils": "^6.2.0",
"jest": "^24.8.0",
"mjml": "^4.1.0",
"npm-run-all": "^4.1.1",
"prettier": "^1.14.3",
"rimraf": "^2.6.2",
"ts-jest": "^24.0.2",
"tslint": "^5.11.0",
"typescript": "^3.8.0"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/mailer/__tests__/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// tslint:disable: no-empty
// eslint-disable no-empty, no-empty-function

import { Either } from "fp-ts/lib/Either";
import {
Expand Down Expand Up @@ -202,7 +202,7 @@ describe("MailerConfig", () => {
NODE_ENV: "production"
};

// tslint:disable-next-line: no-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const examples: ReadonlyArray<any> = [
// the following configuration is not ambiguos as sendgrid would override mailup anyway
// see here for the rationale: https://github.com/pagopa/io-functions-admin/pull/89#commitcomment-42917672
Expand Down
4 changes: 2 additions & 2 deletions src/mailer/__tests__/mailup.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// tslint:disable:no-any
// eslint-disable @typescript-eslint/no-explicit-any

jest.mock("winston");

// tslint:disable-next-line:no-submodule-imports
// eslint-disable-next-line import/no-internal-modules
import Mail = require("nodemailer/lib/mailer");

import * as nodemailer from "nodemailer";
Expand Down
2 changes: 1 addition & 1 deletion src/mailer/__tests__/multi_transport_connection.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// tslint:disable: no-hardcoded-credentials
// eslint-disable sonar/no-hardcoded-credentials
import { right } from "fp-ts/lib/Either";

import { MailMultiTransportConnectionsFromString } from "../multi_transport_connection";
Expand Down
2 changes: 1 addition & 1 deletion src/mailer/__tests__/transports.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// tslint:disable: no-duplicate-string
// eslint-disable sonarjs/no-duplicate-string

import { fromNullable } from "fp-ts/lib/Option";
import {
Expand Down
4 changes: 3 additions & 1 deletion src/mailer/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { MailMultiTransportConnectionsFromString } from "./multi_transport_conne

// exclude a specific value from a type
// as strict equality is performed, allowed input types are constrained to be values not references (object, arrays, etc)
// tslint:disable-next-line max-union-size
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const AnyBut = <A extends string | number | boolean | symbol, O = A>(
but: A,
base: t.Type<A, O> = t.any
) =>
t.brand(
base,

(s): s is t.Branded<t.TypeOf<typeof base>, { readonly AnyBut: symbol }> =>
s !== but,
"AnyBut"
Expand Down Expand Up @@ -68,6 +69,7 @@ export const MailhogMailerConfig = t.interface({

// configuration to send email
export type MailerConfig = t.TypeOf<typeof MailerConfig>;

export const MailerConfig = t.intersection([
// common required fields
t.interface({
Expand Down
12 changes: 6 additions & 6 deletions src/mailer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ const defaultFetchAgent = toFetch(
* @returns a mail transporter object
* @throws an error creating the transporter
*/
export function getMailerTransporter(
export const getMailerTransporter = (
config: MailerConfig,
fetchAgent: typeof fetch = defaultFetchAgent
): MailerTransporter {
): MailerTransporter => {
const maybeTransportOpts: Option<
| Transport
| {
host: NonEmptyString;
port: number;
secure: boolean;
readonly host: NonEmptyString;
readonly port: number;
readonly secure: boolean;
}
> = SendgridMailerConfig.is(config)
? some(
Expand Down Expand Up @@ -100,7 +100,7 @@ export function getMailerTransporter(
"Failed to choose a mail transport based on provided configuration"
);
});
}
};

// expose inner stuff as public module interface
export { MailerConfig } from "./config";
Expand Down
70 changes: 41 additions & 29 deletions src/mailer/mailup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import nodeFetch from "node-fetch";

import * as nodemailer from "nodemailer";

// tslint:disable-next-line:no-submodule-imports
// eslint-disable-next-line import/no-internal-modules
import { Address as NodemailerAddress } from "nodemailer/lib/addressparser";

import * as winston from "winston";
Expand All @@ -36,35 +36,43 @@ const TRANSPORT_VERSION = "0.1";
* You need to create a SMTP+ user in MailUp administration panel
* see also http://help.mailup.com/display/MUG/SMTP+Settings
*/
/* eslint-disable @typescript-eslint/naming-convention */
export const SmtpAuthInfo = t.interface({
Secret: NonEmptyString,
Username: NonEmptyString
});
/* eslint-enable @typescript-eslint/naming-convention */

export type SmtpAuthInfo = t.TypeOf<typeof SmtpAuthInfo>;

/**
* MailUp API calls common response fields
*/
/* eslint-disable @typescript-eslint/naming-convention */
const ApiResponse = t.interface({
Code: t.string,
Message: t.string,
Status: t.string
});
/* eslint-enable @typescript-eslint/naming-convention */

type ApiResponse = t.TypeOf<typeof ApiResponse>;

/* eslint-disable @typescript-eslint/naming-convention */
const Address = t.interface({
Email: EmailString,
Name: t.string
});
/* eslint-enable @typescript-eslint/naming-convention */

type Address = t.TypeOf<typeof Address>;

/* eslint-disable @typescript-eslint/naming-convention */
const NameValue = t.interface({
N: NonEmptyString,
V: t.string
});
/* eslint-enable @typescript-eslint/naming-convention */

type NameValue = t.TypeOf<typeof NameValue>;

Expand All @@ -74,6 +82,7 @@ const Html = t.interface({

type Html = t.TypeOf<typeof NameValue>;

/* eslint-disable @typescript-eslint/naming-convention */
const EmailPayload = t.intersection([
t.interface({
ExtendedHeaders: t.array(NameValue),
Expand All @@ -89,6 +98,7 @@ const EmailPayload = t.intersection([
ReplyTo: t.string
})
]);
/* eslint-enable @typescript-eslint/naming-convention */

type EmailPayload = t.TypeOf<typeof EmailPayload>;

Expand All @@ -97,6 +107,7 @@ export interface IMailUpTransportOptions {
readonly fetchAgent?: typeof fetch;
}

/* eslint-disable @typescript-eslint/naming-convention */
interface IAddresses {
readonly bcc?: ReadonlyArray<NodemailerAddress>;
readonly cc?: ReadonlyArray<NodemailerAddress>;
Expand All @@ -105,13 +116,15 @@ interface IAddresses {
readonly "reply-to"?: ReadonlyArray<NodemailerAddress>;
readonly to?: ReadonlyArray<NodemailerAddress>;
}
/* eslint-enable @typescript-eslint/naming-convention */

function sendTransactionalMail(
/* eslint-disable @typescript-eslint/naming-convention */
const sendTransactionalMail = (
creds: SmtpAuthInfo,
payload: EmailPayload,
fetchAgent: typeof fetch
): TaskEither<Error, ApiResponse> {
return tryCatch(
): TaskEither<Error, ApiResponse> =>
tryCatch(
() =>
fetchAgent(SEND_TRANSACTIONAL_MAIL_ENDPOINT, {
body: JSON.stringify({ ...payload, User: creds }),
Expand Down Expand Up @@ -155,40 +168,37 @@ function sendTransactionalMail(
)
)
);
}
/* eslint-enable @typescript-eslint/naming-convention */

/**
* Translates nodemailer parsed addresses ({ name: <name>, address: <address> })
* to the format expected by the MailUp API ({ Name: <name>, Email: <address> })
*/
function toMailupAddresses(
const toMailupAddresses = (
addresses: ReadonlyArray<NodemailerAddress>
): ReadonlyArray<Address> {
return addresses.map((address: NodemailerAddress) => {
return {
Email: EmailString.decode(address.address).getOrElseL(() => {
// this never happens as nodemailer has already parsed
// the email address (so it's a valid one)
throw new Error(
`Error while parsing email address (toMailupAddresses): invalid format '${address.address}'.`
);
}),
Name: address.name || address.address
};
});
}
): ReadonlyArray<Address> =>
addresses.map((address: NodemailerAddress) => ({
Email: EmailString.decode(address.address).getOrElseL(() => {
// this never happens as nodemailer has already parsed
// the email address (so it's a valid one)
throw new Error(
`Error while parsing email address (toMailupAddresses): invalid format '${address.address}'.`
);
}),
Name: address.name || address.address
}));

/**
* Translates nodemailer parsed addresses ({ name: <name>, address: <address> })
* to the format expected by the MailUp API ({ Name: <name>, Email: <address> })
* then get the first one from the input array.
*/
function toMailupAddress(
const toMailupAddress = (
addresses: ReadonlyArray<NodemailerAddress>
): Option<Address> {
): Option<Address> => {
const addrs = toMailupAddresses(addresses);
return fromNullable(addrs[0]);
}
};

/**
* Nodemailer transport for MailUp transactional APIs
Expand Down Expand Up @@ -220,9 +230,10 @@ function toMailupAddress(
* .then(res => console.log(JSON.stringify(res)))
* .catch(err => console.error(JSON.stringify(err)));
*/
export function MailUpTransport(
/* eslint-disable @typescript-eslint/naming-convention */
export const MailUpTransport = (
options: IMailUpTransportOptions
): nodemailer.Transport {
): nodemailer.Transport => {
const fetchAgent =
options.fetchAgent !== undefined
? options.fetchAgent
Expand All @@ -232,7 +243,8 @@ export function MailUpTransport(

version: TRANSPORT_VERSION,

send: (mail, callback) => {
// eslint-disable-next-line sort-keys
send: (mail, callback): void => {
// We don't extract email addresses from mail.data.from / mail.data.to
// as they are just strings that can contain invalid addresses.
// Instead, mail.message.getAddresses() gets the email addresses
Expand All @@ -251,7 +263,7 @@ export function MailUpTransport(
}
).map(header => ({
N: header,
// tslint:disable-next-line:no-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
V: (mail.data.headers as any)[header]
}));

Expand Down Expand Up @@ -297,7 +309,6 @@ export function MailUpTransport(
.run()
.then(errorOrResponse => {
if (isRight(errorOrResponse)) {
// tslint:disable-next-line:no-null-keyword
return callback(null, {
...errorOrResponse.value,
messageId: mail.data.messageId
Expand All @@ -309,4 +320,5 @@ export function MailUpTransport(
.catch(e => callback(e, undefined));
}
};
}
};
/* eslint-enable @typescript-eslint/naming-convention */
Loading

0 comments on commit c3bc174

Please sign in to comment.