Skip to content

Commit

Permalink
chore: Enabling more ESLint checks and fixing errors (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiranya911 committed Mar 2, 2020
1 parent c0e6ae1 commit 0f6c02e
Show file tree
Hide file tree
Showing 49 changed files with 175 additions and 150 deletions.
24 changes: 14 additions & 10 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ module.exports = {
rules: {
// Following checks are temporarily disabled. We shall incrementally enable them in the
// future, fixing any violations as we go.
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-use-before-define": 0,
"@typescript-eslint/explicit-function-return-type": 0,
"@typescript-eslint/camelcase": 0,
"@typescript-eslint/no-non-null-assertion": 0,
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/ban-types": 0,
"no-useless-escape": 0,
"no-prototype-builtins": 0,
'@typescript-eslint/no-non-null-assertion': 0,

// Disabled checks
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/no-use-before-define': 0,

// Required checks
"indent": ["error", 2]
'indent': ['error', 2],
'@typescript-eslint/explicit-function-return-type': [
'error',
{
'allowExpressions': true,
'allowTypedFunctionExpressions': true,
'allowHigherOrderFunctions': true
}
],
}
};
2 changes: 1 addition & 1 deletion src/auth/action-code-settings-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class ActionCodeSettingsBuilder {
};
// Remove all null and undefined fields from request.
for (const key in request) {
if (request.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(request, key)) {
if (typeof request[key] === 'undefined' || request[key] === null) {
delete request[key];
}
Expand Down
6 changes: 3 additions & 3 deletions src/auth/auth-api-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class TenantAwareAuthResourceUrlBuilder extends AuthResourceUrlBuilder {
*
* @param {any} request The providerUserInfo request object.
*/
function validateProviderUserInfo(request: any) {
function validateProviderUserInfo(request: any): void {
const validKeys = {
rawId: true,
providerId: true,
Expand Down Expand Up @@ -247,7 +247,7 @@ function validateProviderUserInfo(request: any) {
* @param {any} request The create/edit request object.
* @param {boolean=} uploadAccountRequest Whether to validate as an uploadAccount request.
*/
function validateCreateEditRequest(request: any, uploadAccountRequest = false) {
function validateCreateEditRequest(request: any, uploadAccountRequest = false): void {
// Hash set of whitelisted parameters.
const validKeys = {
displayName: true,
Expand Down Expand Up @@ -370,7 +370,7 @@ function validateCreateEditRequest(request: any, uploadAccountRequest = false) {
const invalidClaims: string[] = [];
// Check for any invalid claims.
RESERVED_CLAIMS.forEach((blacklistedClaim) => {
if (developerClaims.hasOwnProperty(blacklistedClaim)) {
if (Object.prototype.hasOwnProperty.call(developerClaims, blacklistedClaim)) {
invalidClaims.push(blacklistedClaim);
}
});
Expand Down
10 changes: 5 additions & 5 deletions src/auth/auth-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ export class EmailSignInConfig implements EmailSignInProviderConfig {
public static buildServerRequest(options: EmailSignInProviderConfig): EmailSignInConfigServerRequest {
const request: EmailSignInConfigServerRequest = {};
EmailSignInConfig.validate(options);
if (options.hasOwnProperty('enabled')) {
if (Object.prototype.hasOwnProperty.call(options, 'enabled')) {
request.allowPasswordSignup = options.enabled;
}
if (options.hasOwnProperty('passwordRequired')) {
if (Object.prototype.hasOwnProperty.call(options, 'passwordRequired')) {
request.enableEmailLinkSignin = !options.passwordRequired;
}
return request;
Expand All @@ -193,7 +193,7 @@ export class EmailSignInConfig implements EmailSignInProviderConfig {
*
* @param {any} options The options object to validate.
*/
private static validate(options: EmailSignInProviderConfig) {
private static validate(options: EmailSignInProviderConfig): void {
// TODO: Validate the request.
const validKeys = {
enabled: true,
Expand Down Expand Up @@ -349,7 +349,7 @@ export class SAMLConfig implements SAMLAuthProviderConfig {
* @param {SAMLAuthProviderRequest} options The options object to validate.
* @param {boolean=} ignoreMissingFields Whether to ignore missing fields.
*/
public static validate(options: SAMLAuthProviderRequest, ignoreMissingFields = false) {
public static validate(options: SAMLAuthProviderRequest, ignoreMissingFields = false): void {
const validKeys = {
enabled: true,
displayName: true,
Expand Down Expand Up @@ -590,7 +590,7 @@ export class OIDCConfig implements OIDCAuthProviderConfig {
* @param {OIDCAuthProviderRequest} options The options object to validate.
* @param {boolean=} ignoreMissingFields Whether to ignore missing fields.
*/
public static validate(options: OIDCAuthProviderRequest, ignoreMissingFields = false) {
public static validate(options: OIDCAuthProviderRequest, ignoreMissingFields = false): void {
const validKeys = {
enabled: true,
displayName: true,
Expand Down
2 changes: 1 addition & 1 deletion src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ export class BaseAuth<T extends AbstractAuthRequestHandler> {
providerConfigs,
};
// Delete result.pageToken if undefined.
if (response.hasOwnProperty('nextPageToken')) {
if (Object.prototype.hasOwnProperty.call(response, 'nextPageToken')) {
result.pageToken = response.nextPageToken;
}
return result;
Expand Down
4 changes: 3 additions & 1 deletion src/auth/credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export class ServiceAccountCredential implements Credential {
].join(' '),
};

// eslint-disable-next-line @typescript-eslint/no-var-requires
const jwt = require('jsonwebtoken');
// This method is actually synchronous so we can capture and return the buffer.
return jwt.sign(claims, this.privateKey, {
Expand Down Expand Up @@ -189,6 +190,7 @@ class ServiceAccount {
throw new FirebaseAppError(AppErrorCodes.INVALID_CREDENTIAL, errorMessage);
}

// eslint-disable-next-line @typescript-eslint/no-var-requires
const forge = require('node-forge');
try {
forge.pki.privateKeyFromPem(this.privateKey);
Expand Down Expand Up @@ -386,7 +388,7 @@ export function isApplicationDefault(credential?: Credential): boolean {
* @param key Name of the property to copy.
* @param alt Alternative name of the property to copy.
*/
function copyAttr(to: {[key: string]: any}, from: {[key: string]: any}, key: string, alt: string) {
function copyAttr(to: {[key: string]: any}, from: {[key: string]: any}, key: string, alt: string): void {
const tmp = from[key] || from[alt];
if (typeof tmp !== 'undefined') {
to[key] = tmp;
Expand Down
2 changes: 1 addition & 1 deletion src/auth/tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class Tenant {
* @param {any} request The tenant options object to validate.
* @param {boolean} createRequest Whether this is a create request.
*/
private static validate(request: any, createRequest: boolean) {
private static validate(request: any, createRequest: boolean): void {
const validKeys = {
displayName: true,
emailSignInConfig: true,
Expand Down
9 changes: 5 additions & 4 deletions src/auth/token-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class ServiceAccountSigner implements CryptoSigner {
* @inheritDoc
*/
public sign(buffer: Buffer): Promise<Buffer> {
const crypto = require('crypto');
const crypto = require('crypto'); // eslint-disable-line @typescript-eslint/no-var-requires
const sign = crypto.createSign('RSA-SHA256');
sign.update(buffer);
return Promise.resolve(sign.sign(this.credential.privateKey));
Expand Down Expand Up @@ -285,7 +285,7 @@ export class FirebaseTokenGenerator {
if (typeof developerClaims !== 'undefined') {
for (const key in developerClaims) {
/* istanbul ignore else */
if (developerClaims.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(developerClaims, key)) {
if (BLACKLISTED_CLAIMS.indexOf(key) !== -1) {
throw new FirebaseAuthError(
AuthClientErrorCode.INVALID_ARGUMENT,
Expand All @@ -311,6 +311,7 @@ export class FirebaseTokenGenerator {
uid,
};
if (this.tenantId) {
// eslint-disable-next-line @typescript-eslint/camelcase
body.tenant_id = this.tenantId;
}
if (Object.keys(claims).length > 0) {
Expand All @@ -324,9 +325,9 @@ export class FirebaseTokenGenerator {
});
}

private encodeSegment(segment: object | Buffer) {
private encodeSegment(segment: object | Buffer): string {
const buffer: Buffer = (segment instanceof Buffer) ? segment : Buffer.from(JSON.stringify(segment));
return toWebSafeBase64(buffer).replace(/\=+$/, '');
return toWebSafeBase64(buffer).replace(/=+$/, '');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/auth/token-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class FirebaseTokenVerifier {
}

return this.fetchPublicKeys().then((publicKeys) => {
if (!publicKeys.hasOwnProperty(header.kid)) {
if (!Object.prototype.hasOwnProperty.call(publicKeys, header.kid)) {
return Promise.reject(
new FirebaseAuthError(
AuthClientErrorCode.INVALID_ARGUMENT,
Expand Down Expand Up @@ -299,7 +299,7 @@ export class FirebaseTokenVerifier {
// error responses.
throw new HttpError(resp);
}
if (resp.headers.hasOwnProperty('cache-control')) {
if (Object.prototype.hasOwnProperty.call(resp.headers, 'cache-control')) {
const cacheControlHeader: string = resp.headers['cache-control'];
const parts = cacheControlHeader.split(',');
parts.forEach((part) => {
Expand Down
4 changes: 2 additions & 2 deletions src/database/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export class DatabaseService implements FirebaseServiceInterface {

let db: Database = this.INTERNAL.databases[dbUrl];
if (typeof db === 'undefined') {
const rtdb = require('@firebase/database');
const { version } = require('../../package.json');
const rtdb = require('@firebase/database'); // eslint-disable-line @typescript-eslint/no-var-requires
const { version } = require('../../package.json'); // eslint-disable-line @typescript-eslint/no-var-requires
db = rtdb.initStandalone(this.appInternal, dbUrl, version).instance;

const rulesClient = new DatabaseRulesClient(this.app, dbUrl);
Expand Down
4 changes: 2 additions & 2 deletions src/firebase-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class FirebaseAppInternals {
*
* @param {function(string)} listener The listener that will be called with each new token.
*/
public addAuthTokenListener(listener: (token: string) => void) {
public addAuthTokenListener(listener: (token: string) => void): void {
this.tokenListeners_.push(listener);
if (this.cachedToken_) {
listener(this.cachedToken_.accessToken);
Expand All @@ -201,7 +201,7 @@ export class FirebaseAppInternals {
*
* @param {function(string)} listener The listener to remove.
*/
public removeAuthTokenListener(listener: (token: string) => void) {
public removeAuthTokenListener(listener: (token: string) => void): void {
this.tokenListeners_ = this.tokenListeners_.filter((other) => other !== listener);
}

Expand Down
6 changes: 5 additions & 1 deletion src/firebase-namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export class FirebaseNamespaceInternals {
* @param {FirebaseApp} app The FirebaseApp instance whose app hooks to call.
* @param {string} eventName The event name representing which app hooks to call.
*/
private callAppHooks_(app: FirebaseApp, eventName: string) {
private callAppHooks_(app: FirebaseApp, eventName: string): void {
Object.keys(this.serviceFactories).forEach((serviceName) => {
if (this.appHooks_[serviceName]) {
this.appHooks_[serviceName](eventName, app);
Expand Down Expand Up @@ -340,6 +340,8 @@ export class FirebaseNamespace {
const fn: FirebaseServiceNamespace<Database> = (app?: FirebaseApp) => {
return this.ensureApp(app).database();
};

// eslint-disable-next-line @typescript-eslint/no-var-requires
return Object.assign(fn, require('@firebase/database'));
}

Expand Down Expand Up @@ -375,6 +377,8 @@ export class FirebaseNamespace {
let fn: FirebaseServiceNamespace<Firestore> = (app?: FirebaseApp) => {
return this.ensureApp(app).firestore();
};

// eslint-disable-next-line @typescript-eslint/no-var-requires
const firestore = require('@google-cloud/firestore');

fn = Object.assign(fn, firestore.Firestore);
Expand Down
5 changes: 3 additions & 2 deletions src/firestore/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ export function getFirestoreOptions(app: FirebaseApp): Settings {

const projectId: string | null = utils.getExplicitProjectId(app);
const credential = app.options.credential;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { version: firebaseVersion } = require('../../package.json');
if (credential instanceof ServiceAccountCredential) {
return {
credentials: {
private_key: credential.privateKey,
client_email: credential.clientEmail,
private_key: credential.privateKey, // eslint-disable-line @typescript-eslint/camelcase
client_email: credential.clientEmail, // eslint-disable-line @typescript-eslint/camelcase
},
// When the SDK is initialized with ServiceAccountCredentials an explicit projectId is
// guaranteed to be available.
Expand Down
2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { Bucket } from '@google-cloud/storage';
import * as _firestore from '@google-cloud/firestore';
import { Agent } from 'http';

/* eslint-disable @typescript-eslint/ban-types */

/**
* `admin` is a global namespace from which all Firebase Admin
* services are accessed.
Expand Down

0 comments on commit 0f6c02e

Please sign in to comment.