fix(prettier): Properly handle lint-stage files (#6970)

Now handles top level files and recursive files in folders.

Set max line length to be 100
This commit is contained in:
Diamond Lewis
2020-10-25 15:06:58 -05:00
committed by GitHub
parent c2f2281e6d
commit e6ac3b6932
178 changed files with 5585 additions and 10688 deletions

View File

@@ -24,17 +24,7 @@ import logger from './logger';
// RestWrite will handle objectId, createdAt, and updatedAt for
// everything. It also knows to use triggers and special modifications
// for the _User class.
function RestWrite(
config,
auth,
className,
query,
data,
originalData,
clientSDK,
context,
action
) {
function RestWrite(config, auth, className, query, data, originalData, clientSDK, context, action) {
if (auth.isReadOnly) {
throw new Parse.Error(
Parse.Error.OPERATION_FORBIDDEN,
@@ -55,10 +45,7 @@ function RestWrite(
if (!query) {
if (this.config.allowCustomObjectId) {
if (
Object.prototype.hasOwnProperty.call(data, 'objectId') &&
!data.objectId
) {
if (Object.prototype.hasOwnProperty.call(data, 'objectId') && !data.objectId) {
throw new Parse.Error(
Parse.Error.MISSING_OBJECT_ID,
'objectId must not be empty, null or undefined'
@@ -66,16 +53,10 @@ function RestWrite(
}
} else {
if (data.objectId) {
throw new Parse.Error(
Parse.Error.INVALID_KEY_NAME,
'objectId is an invalid field name.'
);
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, 'objectId is an invalid field name.');
}
if (data.id) {
throw new Parse.Error(
Parse.Error.INVALID_KEY_NAME,
'id is an invalid field name.'
);
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, 'id is an invalid field name.');
}
}
}
@@ -175,9 +156,7 @@ RestWrite.prototype.getUserAndRoleACL = function () {
if (this.auth.user) {
return this.auth.getUserRoles().then(roles => {
this.runOptions.acl = this.runOptions.acl.concat(roles, [
this.auth.user.id,
]);
this.runOptions.acl = this.runOptions.acl.concat(roles, [this.auth.user.id]);
return;
});
} else {
@@ -199,9 +178,7 @@ RestWrite.prototype.validateClientClassCreation = function () {
if (hasClass !== true) {
throw new Parse.Error(
Parse.Error.OPERATION_FORBIDDEN,
'This user is not allowed to access ' +
'non-existent class: ' +
this.className
'This user is not allowed to access ' + 'non-existent class: ' + this.className
);
}
});
@@ -229,11 +206,7 @@ RestWrite.prototype.runBeforeSaveTrigger = function () {
// Avoid doing any setup for triggers if there is no 'beforeSave' trigger for this class.
if (
!triggers.triggerExists(
this.className,
triggers.Types.beforeSave,
this.config.applicationId
)
!triggers.triggerExists(this.className, triggers.Types.beforeSave, this.config.applicationId)
) {
return Promise.resolve();
}
@@ -277,10 +250,7 @@ RestWrite.prototype.runBeforeSaveTrigger = function () {
// In the case that there is no permission for the operation, it throws an error
return databasePromise.then(result => {
if (!result || result.length <= 0) {
throw new Parse.Error(
Parse.Error.OBJECT_NOT_FOUND,
'Object not found.'
);
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Object not found.');
}
});
})
@@ -318,11 +288,7 @@ RestWrite.prototype.runBeforeSaveTrigger = function () {
RestWrite.prototype.runBeforeLoginTrigger = async function (userData) {
// Avoid doing any setup for triggers if there is no 'beforeLogin' trigger
if (
!triggers.triggerExists(
this.className,
triggers.Types.beforeLogin,
this.config.applicationId
)
!triggers.triggerExists(this.className, triggers.Types.beforeLogin, this.config.applicationId)
) {
return;
}
@@ -349,16 +315,13 @@ RestWrite.prototype.runBeforeLoginTrigger = async function (userData) {
RestWrite.prototype.setRequiredFieldsIfNeeded = function () {
if (this.data) {
return this.validSchemaController.getAllClasses().then(allClasses => {
const schema = allClasses.find(
oneClass => oneClass.className === this.className
);
const schema = allClasses.find(oneClass => oneClass.className === this.className);
const setRequiredFieldIfNeeded = (fieldName, setDefault) => {
if (
this.data[fieldName] === undefined ||
this.data[fieldName] === null ||
this.data[fieldName] === '' ||
(typeof this.data[fieldName] === 'object' &&
this.data[fieldName].__op === 'Delete')
(typeof this.data[fieldName] === 'object' && this.data[fieldName].__op === 'Delete')
) {
if (
setDefault &&
@@ -366,23 +329,15 @@ RestWrite.prototype.setRequiredFieldsIfNeeded = function () {
schema.fields[fieldName].defaultValue !== null &&
schema.fields[fieldName].defaultValue !== undefined &&
(this.data[fieldName] === undefined ||
(typeof this.data[fieldName] === 'object' &&
this.data[fieldName].__op === 'Delete'))
(typeof this.data[fieldName] === 'object' && this.data[fieldName].__op === 'Delete'))
) {
this.data[fieldName] = schema.fields[fieldName].defaultValue;
this.storage.fieldsChangedByTrigger =
this.storage.fieldsChangedByTrigger || [];
this.storage.fieldsChangedByTrigger = this.storage.fieldsChangedByTrigger || [];
if (this.storage.fieldsChangedByTrigger.indexOf(fieldName) < 0) {
this.storage.fieldsChangedByTrigger.push(fieldName);
}
} else if (
schema.fields[fieldName] &&
schema.fields[fieldName].required === true
) {
throw new Parse.Error(
Parse.Error.VALIDATION_ERROR,
`${fieldName} is required`
);
} else if (schema.fields[fieldName] && schema.fields[fieldName].required === true) {
throw new Parse.Error(Parse.Error.VALIDATION_ERROR, `${fieldName} is required`);
}
}
};
@@ -394,9 +349,7 @@ RestWrite.prototype.setRequiredFieldsIfNeeded = function () {
// Only assign new objectId if we are creating new object
if (!this.data.objectId) {
this.data.objectId = cryptoUtils.newObjectId(
this.config.objectIdSize
);
this.data.objectId = cryptoUtils.newObjectId(this.config.objectIdSize);
}
if (schema) {
Object.keys(schema.fields).forEach(fieldName => {
@@ -422,23 +375,11 @@ RestWrite.prototype.validateAuthData = function () {
}
if (!this.query && !this.data.authData) {
if (
typeof this.data.username !== 'string' ||
_.isEmpty(this.data.username)
) {
throw new Parse.Error(
Parse.Error.USERNAME_MISSING,
'bad or missing username'
);
if (typeof this.data.username !== 'string' || _.isEmpty(this.data.username)) {
throw new Parse.Error(Parse.Error.USERNAME_MISSING, 'bad or missing username');
}
if (
typeof this.data.password !== 'string' ||
_.isEmpty(this.data.password)
) {
throw new Parse.Error(
Parse.Error.PASSWORD_MISSING,
'password is required'
);
if (typeof this.data.password !== 'string' || _.isEmpty(this.data.password)) {
throw new Parse.Error(Parse.Error.PASSWORD_MISSING, 'password is required');
}
}
@@ -448,10 +389,7 @@ RestWrite.prototype.validateAuthData = function () {
) {
// Handle saving authData to {} or if authData doesn't exist
return;
} else if (
Object.prototype.hasOwnProperty.call(this.data, 'authData') &&
!this.data.authData
) {
} else if (Object.prototype.hasOwnProperty.call(this.data, 'authData') && !this.data.authData) {
// Handle saving authData to null
throw new Parse.Error(
Parse.Error.UNSUPPORTED_SERVICE,
@@ -482,9 +420,7 @@ RestWrite.prototype.handleAuthDataValidation = function (authData) {
if (authData[provider] === null) {
return Promise.resolve();
}
const validateAuthData = this.config.authDataManager.getValidatorForProvider(
provider
);
const validateAuthData = this.config.authDataManager.getValidatorForProvider(provider);
if (!validateAuthData) {
throw new Parse.Error(
Parse.Error.UNSUPPORTED_SERVICE,
@@ -595,8 +531,7 @@ RestWrite.prototype.handleAuthData = function (authData) {
if (this.response) {
// Assign the new authData in the response
Object.keys(mutatedAuthData).forEach(provider => {
this.response.response.authData[provider] =
mutatedAuthData[provider];
this.response.response.authData[provider] = mutatedAuthData[provider];
});
// Run the DB update directly, as 'master'
@@ -614,10 +549,7 @@ RestWrite.prototype.handleAuthData = function (authData) {
// Trying to update auth data but users
// are different
if (userResult.objectId !== userId) {
throw new Parse.Error(
Parse.Error.ACCOUNT_ALREADY_LINKED,
'this auth is already used'
);
throw new Parse.Error(Parse.Error.ACCOUNT_ALREADY_LINKED, 'this auth is already used');
}
// No auth data was mutated, just keep going
if (!hasMutatedAuthData) {
@@ -628,10 +560,7 @@ RestWrite.prototype.handleAuthData = function (authData) {
return this.handleAuthDataValidation(authData).then(() => {
if (results.length > 1) {
// More than 1 user with the passed id's
throw new Parse.Error(
Parse.Error.ACCOUNT_ALREADY_LINKED,
'this auth is already used'
);
throw new Parse.Error(Parse.Error.ACCOUNT_ALREADY_LINKED, 'this auth is already used');
}
});
});
@@ -757,10 +686,7 @@ RestWrite.prototype._validateEmail = function () {
// Validate basic email address format
if (!this.data.email.match(/^.+@.+$/)) {
return Promise.reject(
new Parse.Error(
Parse.Error.INVALID_EMAIL_ADDRESS,
'Email address format is invalid.'
)
new Parse.Error(Parse.Error.INVALID_EMAIL_ADDRESS, 'Email address format is invalid.')
);
}
// Case insensitive match, see note above function.
@@ -823,9 +749,7 @@ RestWrite.prototype._validatePasswordRequirements = function () {
(this.config.passwordPolicy.validatorCallback &&
!this.config.passwordPolicy.validatorCallback(this.data.password))
) {
return Promise.reject(
new Parse.Error(Parse.Error.VALIDATION_ERROR, policyError)
);
return Promise.reject(new Parse.Error(Parse.Error.VALIDATION_ERROR, policyError));
}
// check whether password contain username
@@ -833,26 +757,19 @@ RestWrite.prototype._validatePasswordRequirements = function () {
if (this.data.username) {
// username is not passed during password reset
if (this.data.password.indexOf(this.data.username) >= 0)
return Promise.reject(
new Parse.Error(Parse.Error.VALIDATION_ERROR, containsUsernameError)
);
return Promise.reject(new Parse.Error(Parse.Error.VALIDATION_ERROR, containsUsernameError));
} else {
// retrieve the User object using objectId during password reset
return this.config.database
.find('_User', { objectId: this.objectId() })
.then(results => {
if (results.length != 1) {
throw undefined;
}
if (this.data.password.indexOf(results[0].username) >= 0)
return Promise.reject(
new Parse.Error(
Parse.Error.VALIDATION_ERROR,
containsUsernameError
)
);
return Promise.resolve();
});
return this.config.database.find('_User', { objectId: this.objectId() }).then(results => {
if (results.length != 1) {
throw undefined;
}
if (this.data.password.indexOf(results[0].username) >= 0)
return Promise.reject(
new Parse.Error(Parse.Error.VALIDATION_ERROR, containsUsernameError)
);
return Promise.resolve();
});
}
}
return Promise.resolve();
@@ -999,11 +916,7 @@ RestWrite.prototype.destroyDuplicatedSessions = function () {
// Handles any followup logic
RestWrite.prototype.handleFollowup = function () {
if (
this.storage &&
this.storage['clearSessions'] &&
this.config.revokeSessionOnPasswordReset
) {
if (this.storage && this.storage['clearSessions'] && this.config.revokeSessionOnPasswordReset) {
var sessionQuery = {
user: {
__type: 'Pointer',
@@ -1038,26 +951,16 @@ RestWrite.prototype.handleSession = function () {
}
if (!this.auth.user && !this.auth.isMaster) {
throw new Parse.Error(
Parse.Error.INVALID_SESSION_TOKEN,
'Session token required.'
);
throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN, 'Session token required.');
}
// TODO: Verify proper error to throw
if (this.data.ACL) {
throw new Parse.Error(
Parse.Error.INVALID_KEY_NAME,
'Cannot set ' + 'ACL on a Session.'
);
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, 'Cannot set ' + 'ACL on a Session.');
}
if (this.query) {
if (
this.data.user &&
!this.auth.isMaster &&
this.data.user.objectId != this.auth.user.id
) {
if (this.data.user && !this.auth.isMaster && this.data.user.objectId != this.auth.user.id) {
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME);
} else if (this.data.installationId) {
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME);
@@ -1085,10 +988,7 @@ RestWrite.prototype.handleSession = function () {
return createSession().then(results => {
if (!results.response) {
throw new Parse.Error(
Parse.Error.INTERNAL_SERVER_ERROR,
'Error creating session.'
);
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, 'Error creating session.');
}
sessionData['objectId'] = results.response['objectId'];
this.response = {
@@ -1118,8 +1018,7 @@ RestWrite.prototype.handleInstallation = function () {
) {
throw new Parse.Error(
135,
'at least one ID field (deviceToken, installationId) ' +
'must be specified in this operation'
'at least one ID field (deviceToken, installationId) ' + 'must be specified in this operation'
);
}
@@ -1146,12 +1045,7 @@ RestWrite.prototype.handleInstallation = function () {
}
// Updating _Installation but not updating anything critical
if (
this.query &&
!this.data.deviceToken &&
!installationId &&
!this.data.deviceType
) {
if (this.query && !this.data.deviceToken && !installationId && !this.data.deviceType) {
return;
}
@@ -1194,11 +1088,7 @@ RestWrite.prototype.handleInstallation = function () {
})
.then(results => {
results.forEach(result => {
if (
this.query &&
this.query.objectId &&
result.objectId == this.query.objectId
) {
if (this.query && this.query.objectId && result.objectId == this.query.objectId) {
objectIdMatch = result;
}
if (result.installationId == installationId) {
@@ -1212,20 +1102,14 @@ RestWrite.prototype.handleInstallation = function () {
// Sanity checks when running a query
if (this.query && this.query.objectId) {
if (!objectIdMatch) {
throw new Parse.Error(
Parse.Error.OBJECT_NOT_FOUND,
'Object not found for update.'
);
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Object not found for update.');
}
if (
this.data.installationId &&
objectIdMatch.installationId &&
this.data.installationId !== objectIdMatch.installationId
) {
throw new Parse.Error(
136,
'installationId may not be changed in this ' + 'operation'
);
throw new Parse.Error(136, 'installationId may not be changed in this ' + 'operation');
}
if (
this.data.deviceToken &&
@@ -1234,20 +1118,14 @@ RestWrite.prototype.handleInstallation = function () {
!this.data.installationId &&
!objectIdMatch.installationId
) {
throw new Parse.Error(
136,
'deviceToken may not be changed in this ' + 'operation'
);
throw new Parse.Error(136, 'deviceToken may not be changed in this ' + 'operation');
}
if (
this.data.deviceType &&
this.data.deviceType &&
this.data.deviceType !== objectIdMatch.deviceType
) {
throw new Parse.Error(
136,
'deviceType may not be changed in this ' + 'operation'
);
throw new Parse.Error(136, 'deviceType may not be changed in this ' + 'operation');
}
}
@@ -1260,10 +1138,7 @@ RestWrite.prototype.handleInstallation = function () {
}
// need to specify deviceType only if it's new
if (!this.query && !this.data.deviceType && !idMatch) {
throw new Parse.Error(
135,
'deviceType must be specified in this operation'
);
throw new Parse.Error(135, 'deviceType must be specified in this operation');
}
})
.then(() => {
@@ -1310,10 +1185,7 @@ RestWrite.prototype.handleInstallation = function () {
return;
}
} else {
if (
deviceTokenMatches.length == 1 &&
!deviceTokenMatches[0]['installationId']
) {
if (deviceTokenMatches.length == 1 && !deviceTokenMatches[0]['installationId']) {
// Exactly one device token match and it doesn't have an installation
// ID. This is the one case where we want to merge with the existing
// object.
@@ -1332,10 +1204,7 @@ RestWrite.prototype.handleInstallation = function () {
throw err;
});
} else {
if (
this.data.deviceToken &&
idMatch.deviceToken != this.data.deviceToken
) {
if (this.data.deviceToken && idMatch.deviceToken != this.data.deviceToken) {
// We're setting the device token on an existing installation, so
// we should try cleaning out old installations that match this
// device token.
@@ -1364,16 +1233,14 @@ RestWrite.prototype.handleInstallation = function () {
if (this.data.appIdentifier) {
delQuery['appIdentifier'] = this.data.appIdentifier;
}
this.config.database
.destroy('_Installation', delQuery)
.catch(err => {
if (err.code == Parse.Error.OBJECT_NOT_FOUND) {
// no deletions were made. Can be ignored.
return;
}
// rethrow the error
throw err;
});
this.config.database.destroy('_Installation', delQuery).catch(err => {
if (err.code == Parse.Error.OBJECT_NOT_FOUND) {
// no deletions were made. Can be ignored.
return;
}
// rethrow the error
throw err;
});
}
// In non-merge scenarios, just return the installation match id
return idMatch.objectId;
@@ -1397,10 +1264,7 @@ RestWrite.prototype.handleInstallation = function () {
RestWrite.prototype.expandFilesForExistingObjects = function () {
// Check whether we have a short-circuited response - only then run expansion.
if (this.response && this.response.response) {
this.config.filesController.expandFilesInObject(
this.config,
this.response.response
);
this.config.filesController.expandFilesInObject(this.config, this.response.response);
}
};
@@ -1413,11 +1277,7 @@ RestWrite.prototype.runDatabaseOperation = function () {
this.config.cacheController.role.clear();
}
if (
this.className === '_User' &&
this.query &&
this.auth.isUnauthenticated()
) {
if (this.className === '_User' && this.query && this.auth.isUnauthenticated()) {
throw new Parse.Error(
Parse.Error.SESSION_MISSING,
`Cannot modify user ${this.query.objectId}.`
@@ -1437,11 +1297,7 @@ RestWrite.prototype.runDatabaseOperation = function () {
if (this.query) {
// Force the user to not lockout
// Matched with parse.com
if (
this.className === '_User' &&
this.data.ACL &&
this.auth.isMaster !== true
) {
if (this.className === '_User' && this.data.ACL && this.auth.isMaster !== true) {
this.data.ACL[this.query.objectId] = { read: true, write: true };
}
// update password timestamp if user password is being changed
@@ -1484,8 +1340,7 @@ RestWrite.prototype.runDatabaseOperation = function () {
}
//n-1 passwords go into history including last password
while (
oldPasswords.length >
Math.max(0, this.config.passwordPolicy.maxPasswordHistory - 2)
oldPasswords.length > Math.max(0, this.config.passwordPolicy.maxPasswordHistory - 2)
) {
oldPasswords.shift();
}
@@ -1525,48 +1380,28 @@ RestWrite.prototype.runDatabaseOperation = function () {
ACL[this.data.objectId] = { read: true, write: true };
this.data.ACL = ACL;
// password timestamp to be used when password expiry policy is enforced
if (
this.config.passwordPolicy &&
this.config.passwordPolicy.maxPasswordAge
) {
if (this.config.passwordPolicy && this.config.passwordPolicy.maxPasswordAge) {
this.data._password_changed_at = Parse._encode(new Date());
}
}
// Run a create
return this.config.database
.create(
this.className,
this.data,
this.runOptions,
false,
this.validSchemaController
)
.create(this.className, this.data, this.runOptions, false, this.validSchemaController)
.catch(error => {
if (
this.className !== '_User' ||
error.code !== Parse.Error.DUPLICATE_VALUE
) {
if (this.className !== '_User' || error.code !== Parse.Error.DUPLICATE_VALUE) {
throw error;
}
// Quick check, if we were able to infer the duplicated field name
if (
error &&
error.userInfo &&
error.userInfo.duplicated_field === 'username'
) {
if (error && error.userInfo && error.userInfo.duplicated_field === 'username') {
throw new Parse.Error(
Parse.Error.USERNAME_TAKEN,
'Account already exists for this username.'
);
}
if (
error &&
error.userInfo &&
error.userInfo.duplicated_field === 'email'
) {
if (error && error.userInfo && error.userInfo.duplicated_field === 'email') {
throw new Parse.Error(
Parse.Error.EMAIL_TAKEN,
'Account already exists for this email address.'
@@ -1641,9 +1476,7 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
triggers.Types.afterSave,
this.config.applicationId
);
const hasLiveQuery = this.config.liveQueryController.hasLiveQuery(
this.className
);
const hasLiveQuery = this.config.liveQueryController.hasLiveQuery(this.className);
if (!hasAfterSaveHook && !hasLiveQuery) {
return Promise.resolve();
}
@@ -1662,16 +1495,11 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
// Build the inflated object, different from beforeSave, originalData is not empty
// since developers can change data in the beforeSave.
const updatedObject = this.buildUpdatedObject(extraData);
updatedObject._handleSaveResponse(
this.response.response,
this.response.status || 200
);
updatedObject._handleSaveResponse(this.response.response, this.response.status || 200);
this.config.database.loadSchema().then(schemaController => {
// Notifiy LiveQueryServer if possible
const perms = schemaController.getClassLevelPermissions(
updatedObject.className
);
const perms = schemaController.getClassLevelPermissions(updatedObject.className);
this.config.liveQueryController.onAfterSave(
updatedObject.className,
updatedObject,
@@ -1702,8 +1530,7 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
// A helper to figure out what location this operation happens at.
RestWrite.prototype.location = function () {
var middle =
this.className === '_User' ? '/users/' : '/classes/' + this.className + '/';
var middle = this.className === '_User' ? '/users/' : '/classes/' + this.className + '/';
const mount = this.config.mount || this.config.serverURL;
return mount + middle + this.data.objectId;
};