Skip to content

Commit

Permalink
vm: refactor to use validate function
Browse files Browse the repository at this point in the history
Throwing error after checking type is repeated.
So replace it with validate function.

PR-URL: #46176
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
deokjinkim authored and juanarbol committed Jan 31, 2023
1 parent 3d1dd96 commit 277d9da
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions lib/internal/vm/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const {
const { isContext } = internalBinding('contextify');
const {
isModuleNamespaceObject,
isArrayBufferView,
} = require('internal/util/types');
const {
customInspectSymbol,
Expand All @@ -41,6 +40,7 @@ const {
} = require('internal/errors').codes;
const {
validateBoolean,
validateBuffer,
validateFunction,
validateInt32,
validateObject,
Expand Down Expand Up @@ -276,25 +276,16 @@ class SourceTextModule extends Module {
validateInt32(lineOffset, 'options.lineOffset');
validateInt32(columnOffset, 'options.columnOffset');

if (initializeImportMeta !== undefined &&
typeof initializeImportMeta !== 'function') {
throw new ERR_INVALID_ARG_TYPE(
'options.initializeImportMeta', 'function', initializeImportMeta);
if (initializeImportMeta !== undefined) {
validateFunction(initializeImportMeta, 'options.initializeImportMeta');
}

if (importModuleDynamically !== undefined &&
typeof importModuleDynamically !== 'function') {
throw new ERR_INVALID_ARG_TYPE(
'options.importModuleDynamically', 'function',
importModuleDynamically);
if (importModuleDynamically !== undefined) {
validateFunction(importModuleDynamically, 'options.importModuleDynamically');
}

if (cachedData !== undefined && !isArrayBufferView(cachedData)) {
throw new ERR_INVALID_ARG_TYPE(
'options.cachedData',
['Buffer', 'TypedArray', 'DataView'],
cachedData
);
if (cachedData !== undefined) {
validateBuffer(cachedData, 'options.cachedData');
}

super({
Expand Down

0 comments on commit 277d9da

Please sign in to comment.