[Babel 8]: Ensure parseExpression with tokens: true returns the exported tokens#17666
[Babel 8]: Ensure parseExpression with tokens: true returns the exported tokens#17666nicolo-ribaudo merged 2 commits intobabel:mainbabel/babel:mainfrom
parseExpression with tokens: true returns the exported tokens#17666Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/60466 |
| expr.errors = this.state.errors; | ||
| if (this.optionFlags & OptionFlags.Tokens) { | ||
| expr.tokens = this.tokens; | ||
| expr.tokens = createExportedTokens(this.tokens); |
There was a problem hiding this comment.
This change stems from the improved ExportedTokens typing, which clearly indicates that previously we passed the internal tokens to the parser return result.
| // TODO: Remove this in Babel 8 | ||
| bracketBarR: createToken("|]"), | ||
| braceL: createToken("{", { beforeExpr, startsExpr }), | ||
| // TODO: Remove this in Babel 8 |
There was a problem hiding this comment.
Although there was a todo item here. It turns out the braceBarL token is also used in Flow object type annotation. Therefore the item is invalid and thus removed.
| | VoidPattern | ||
| )[]; | ||
| returnType: TsTypeAnnotation | undefined | null; | ||
| // TODO(Babel 8): Remove |
There was a problem hiding this comment.
Although there was a todo item that parameters should be removed, it turns out the TSIndexSignature still uses parameters. Therefore we create a new TsIndexSignatureBase type hosting the parameters property.
There was a problem hiding this comment.
There is both params and parameters on TSIndexSignature?
There was a problem hiding this comment.
No. Here is the current situation:
Babel 7:
TSIndexSignature { parameters }
TSConstructSignatureDeclaration { parameters }
TSMethodSignature { parameters }
TSFunctionType { parameters }
TSConstructorType { parameters }
Babel 8:
TSIndexSignature { parameters }
TSConstructSignatureDeclaration { params }
TSMethodSignature { params }
TSFunctionType { params }
TSConstructorType { params }
The TSIndexSignature still holds parameters property, while other nodes hold params property. The other nodes inherit TsSignatureDeclarationBase type, therefore I moved params to TsSignatureDeclarationBase and created TsIndexSignatureBase for parameters.
The Babel 8 AST aligns to the current ts-eslint AST. This PR does not change the TSIndexSignature AST, but rather just fixes typing errors.
|
commit: |
parseExpression with tokens: true return the exported tokensparseExpression with tokens: true returns the exported tokens
The only breaking change introduced in this PR is that the
parser.parseExpression(?, { tokens: true })API now returns exported token rather than internal tokens. For exampleThe Babel 8 behaviour aligns with the behaviour of the more popular API
parse().Since the number representing the internal token is never meant to be part of the return result -- The number might change in different Babel minor releases as we keep adding new tokens. I am open to backporting this change to Babel 7.