-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
vm: move some common tests to the top #18816
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
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) { | ||
|
||
| throw new errors.TypeError('ERR_INVALID_ARG_TYPE', propName, | ||
| type, prop); | ||
| } | ||
| } | ||
|
|
||
| function getContextOptions(options) { | ||
|
||
| 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'); | ||
|
||
| return contextOptions; | ||
|
||
| } | ||
|
|
||
|
|
@@ -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'); | ||
|
||
| } | ||
|
|
||
| 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.