Skip to content

Recognize null as a literal in the lexer#141

Open
hniksic wants to merge 1 commit intoTomFrost:masterfrom
hniksic:fix-null-literal
Open

Recognize null as a literal in the lexer#141
hniksic wants to merge 1 commit intoTomFrost:masterfrom
hniksic:fix-null-literal

Conversation

@hniksic
Copy link

@hniksic hniksic commented Jan 11, 2026

Proposed fix for #140.

In lib/Lexer.js, the _createToken method handles true and false as boolean literals:

} else if (element === 'true' || element === 'false') {
  token.value = element === 'true'
}

But there's no corresponding handling for null. null is tokenized as an identifier and looked up in the context, returning undefined.

This explains the strange behavior observed in #140:

  1. [null] parses as [context.null] which evaluates to [undefined]
  2. x in [null] with {x: null} becomes null in [undefined], and evaluates to false
  3. x in [null] with {} or {x: undefined} becomes undefined in [undefined], and evaluates to true`

If we handle null in _createToken:

} else if (element === 'true' || element === 'false') {
  token.value = element === 'true'
} else if (element === 'null') {
  token.value = null
}

...program from #140 works as expected, and JEXL's existing tests pass.

Note: PR #134 works around this in arithmetic operators by treating both null and undefined as "nullable", but that doesn't help operators like in that need to distinguish between the two.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant