typescript - In the TypeScript compiler source, is `parsingContext` a blob or an instance of an enum?
问题描述
In the TypeScript compiler source, is parsingContext
meant to be always equal to one of the values in the ParsingContext
enum? Or is ParsingContext
a set of flags and parsingContext
is a blob to be manipulated through bitwise operators?
Support for parsingContext
being equal to one of the values of the enum (0 -> 22):
- An intended-to-be-exhaustive check for all values of
parsingContext
: https://github.com/Microsoft/TypeScript/blob/0bb897273fe1e4844f16e341700911e4e730fd82/src/compiler/parser.ts#L1457 - That the type is called "ParsingContext," not "ParsingContextFlags."
Support for parsingContext
being a blob intended to be manipulated with bitwise operators:
- Reading and Writing through bitwise operators:
I'm asking here instead of on GitHub because the TypeScript Issues page says to ask questions here instead of on GH and because it's possible I'm just not understanding a common thing people do with bitwise operators.
解决方案
In the TypeScript compiler source, is parsingContext meant to be always equal to one of the values in the ParsingContext enum? Or is ParsingContext a set of flags and parsingContext is a blob to be manipulated through bitwise operators?
There are different entities called parsingContext
.
let parsingContext: ParsingContext;
holds arbitrary combination of bit flags (is a 'blob' as you say), is always manipulated via bitwise operators and is never compared directly with values of ParsingContext
enum, although for some unclear reason it's declared to have ParsingContext
type.
The other parsingContext
s are declared as parameters to several functions, and are really meant to be just ParsingContext
enum values. In fact, values for these parsingContext
s always come from some function above in the call stack where they are called kind
, not parsingContext
. Here is one instance of kind
in parseList
becoming parseContext
in parseListElement
:
// Parses a list of elements
function parseList<T extends Node>(kind: ParsingContext, parseElement: () => T): NodeArray<T> {
const saveParsingContext = parsingContext;
parsingContext |= 1 << kind;
const list = [];
const listPos = getNodePos();
while (!isListTerminator(kind)) {
if (isListElement(kind, /*inErrorRecovery*/ false)) {
const element = parseListElement(kind, parseElement);
^^^^
function parseListElement<T extends Node>(parsingContext: ParsingContext, parseElement: () => T): T {
const node = currentNode(parsingContext);
All checks for parsingContext
being equal to some value of ParsingContext
enum are done in these functions where parsingContext
does not refer to the parsingContext
bit flags variable, but is coming as a parameter that was initially named kind
and was given as a single value of ParsingContext
enum.
Yes it's confusing, hope that my answer does not add to the confusion :)
推荐阅读
- javascript - 如何简单地将添加事件按钮添加到 FullCalendar?
- ldap - ldapsearch - 如何使用数字字段的范围进行过滤
- javascript - React - 重新渲染道具更改/动态更改 css 而无需重新渲染
- jquery - 在数据表中使用 if else 条件?
- javascript - 修复用于替换所有 \n 和 \r 引号内的正则表达式
- flutter - 在颤动中迭代一个 Json 解析的 http 响应
- vb.net - 将字符串转换为图片框名称
- windows - 可执行文件 merge.exe(从 MSYS2 中提取)不在 Windows 上运行
- javascript - 如何实现会话超时 - Angular - Django 应用与 Azure AD 集成
- vue.js - “错误:[vuex] 不要在突变处理程序之外改变 vuex 存储状态。”