From 7849c9e20930622ec3b708d76fc921efaf0425d1 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Sun, 19 Apr 2026 15:17:25 +0300 Subject: [PATCH] fromAST: add explicit null guard --- index.js | 6 ++++-- test/test.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 5c5de13..4a8d666 100644 --- a/index.js +++ b/index.js @@ -41,10 +41,12 @@ module.exports = function(file, callback) { /** * Determine the module type from an AST node * - * @param {Object} node - * @return {String | null} + * @param {Object|null|undefined} node + * @return {String|null} */ function fromAST(node) { + if (!node) return null; + if (types.isNamedForm(node)) return 'named'; if (types.isDependencyForm(node)) return 'deps'; if (types.isREMForm(node)) return 'rem'; diff --git a/test/test.js b/test/test.js index 41a89dc..a14583a 100644 --- a/test/test.js +++ b/test/test.js @@ -92,3 +92,15 @@ fromSourceTests('should throw an error if an argument is missing', () => { }); fromSourceTests.run(); + +const fromASTTests = suite('From AST tests'); + +fromASTTests('returns null for a null node', () => { + assert.equal(getType.fromAST(null), null); +}); + +fromASTTests('returns null for an undefined node', () => { + assert.equal(getType.fromAST(undefined), null); +}); + +fromASTTests.run();