From 6ffd604e1cf5874327dc628181c2394429d88d3c Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 4 May 2021 01:12:03 +0800 Subject: [PATCH] Chore: Improve readability of `Program` position fix part (#493) --- lib/espree.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/espree.js b/lib/espree.js index 757b6b10..2e6aec67 100644 --- a/lib/espree.js +++ b/lib/espree.js @@ -159,17 +159,27 @@ export default () => Parser => { * whitespace or leading comments). Acorn also counts trailing whitespace * as part of the program whereas Esprima only counts up to the last token. */ - if (program.range) { - program.range[0] = program.body.length ? program.body[0].range[0] : program.range[0]; - program.range[1] = extra.lastToken ? extra.lastToken.range[1] : program.range[1]; + if (program.body.length) { + const [firstNode] = program.body; + + if (program.range) { + program.range[0] = firstNode.range[0]; + } + if (program.loc) { + program.loc.start = firstNode.loc.start; + } + program.start = firstNode.start; } - if (program.loc) { - program.loc.start = program.body.length ? program.body[0].loc.start : program.loc.start; - program.loc.end = extra.lastToken ? extra.lastToken.loc.end : program.loc.end; + if (extra.lastToken) { + if (program.range) { + program.range[1] = extra.lastToken.range[1]; + } + if (program.loc) { + program.loc.end = extra.lastToken.loc.end; + } + program.end = extra.lastToken.end; } - program.start = program.body.length ? program.body[0].start : program.start; - program.end = extra.lastToken ? extra.lastToken.end : program.end; /* * https://github.com/eslint/espree/issues/349