-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vm: move some common tests to the top #18816
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,21 +79,21 @@ Script.prototype.runInNewContext = function(sandbox, options) { | |
return this.runInContext(context, options); | ||
}; | ||
|
||
function validateType(prop, propName, type = 'string') { | ||
if (prop !== undefined && | ||
typeof prop !== type) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this should fit on the line above. |
||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', propName, | ||
type, prop); | ||
} | ||
} | ||
|
||
function getContextOptions(options) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a new line in between functions. |
||
const contextOptions = options ? { | ||
name: options.contextName, | ||
origin: options.contextOrigin | ||
} : {}; | ||
if (contextOptions.name !== undefined && | ||
typeof contextOptions.name !== 'string') { | ||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.contextName', | ||
'string', contextOptions.name); | ||
} | ||
if (contextOptions.origin !== undefined && | ||
typeof contextOptions.origin !== 'string') { | ||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.contextOrigin', | ||
'string', contextOptions.origin); | ||
} | ||
validateType(contextOptions.name, 'options.contextName'); | ||
validateType(contextOptions.origin, 'options.contextOrigin'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not change the error message itself here. So please stick to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @BridgeAR, the error messages are as it was. |
||
return contextOptions; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you are here: would you be so kind and rewrite this to: if (options) {
const contextOptions = {
name: ...
...
};
validateType(contextOptions.name, 'contextOptions.name');
validate...
return contextOptions;
}
return {}; |
||
} | ||
|
||
|
@@ -116,14 +116,11 @@ function createContext(sandbox, options) { | |
}; | ||
if (options.name === undefined) { | ||
options.name = `VM Context ${defaultContextNameIndex++}`; | ||
} else if (typeof options.name !== 'string') { | ||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.name', | ||
'string', options.name); | ||
} | ||
if (options.origin !== undefined && typeof options.origin !== 'string') { | ||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.origin', | ||
'string', options.origin); | ||
} else { | ||
validateType(options.name, 'options.name'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please keep this as it was. Otherwise there is an extra check from now on. |
||
} | ||
|
||
validateType(options.origin, 'options.origin'); | ||
} else { | ||
options = { | ||
name: `VM Context ${defaultContextNameIndex++}` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type is always a string and setting a default value is a minor overhead. So instead, please just directly use 'string' in this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case, please change the function name to
validateString
.