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 RafaelGSS committed Jan 20, 2023
1 parent 6e7f9fb commit 6c8a81d
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 @@ -20,7 +20,6 @@ const {
const { isContext } = internalBinding('contextify');
const {
isModuleNamespaceObject,
isArrayBufferView,
} = require('internal/util/types');
const {
customInspectSymbol,
Expand All @@ -40,6 +39,7 @@ const {
} = require('internal/errors').codes;
const {
validateBoolean,
validateBuffer,
validateFunction,
validateInt32,
validateObject,
Expand Down Expand Up @@ -275,25 +275,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 6c8a81d

Please sign in to comment.