message: feat(plugin): throw on optional chaining
diff: @@ -2,6 +2,34 @@ import ts from "typescript"
  import type { Config } from "./shared"
  +function checkOptionalChaining(
  + node: ts.PropertyAccessExpression,
  + ctx: ts.TransformationContext
  +) {
  + let found: ts.Node | undefined
  +
  + function visitor(node: ts.Node): ts.Node {
  + if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)) {
  + return ts.visitEachChild(node, visitor, ctx)
  + }
  +
  + if (!ts.isPropertyAccessExpression(node)) {
  + return node
  + }
  +
  + if (ts.isPropertyAccessExpression(node) && node.questionDotToken != null) {
  + found = node.questionDotToken
  + return node
  + }
  +
  + return ts.visitEachChild(node, visitor, ctx)
  + }
  +
  + ts.visitNode(node, visitor)
  +
  + return found
  +}
  +
  export default function rewrite(_program: ts.Program) {
  return {
  before(ctx: ts.TransformationContext) {
  @@ -18,6 +46,19 @@ export default function rewrite(_program: ts.Program) {
  tracingOn
  }: Config
  ) => {
  + function throwIfOptionalChaining(node: ts.PropertyAccessExpression) {
  + const isOptional = checkOptionalChaining(node, ctx)
  +
  + if (isOptional) {
  + const pos = sourceFile.getLineAndCharacterOfPosition(isOptional.getEnd())
  + throw new Error(
  + `compiler plugin doesn't support optional chaining: ${
  + sourceFile.fileName
  + }:${pos.line + 1}:${pos.character}`
  + )
  + }
  + }
  +
  function visitor(node: ts.Node): ts.VisitResult<ts.Node> {
  if (ts.isPropertyAccessExpression(node)) {
  const rewrite = checker
  @@ -32,6 +73,10 @@ export default function rewrite(_program: ts.Program) {
  .map((_) => `${_.name} ${_.text?.map((_) => _.text).join(" ")}`)
  .filter((_) => _.startsWith("ets_rewrite_static"))[0]
  + if (rewrite || ets_rewrite_static) {
  + throwIfOptionalChaining(node)
  + }
  +
  if (rewrite) {
  const [fn, mod, attachTrace] = rewrite
  .match(/ets_rewrite_getter (.*) from "(.*)"(?:(.*))/)!
  @@ -115,6 +160,10 @@ export default function rewrite(_program: ts.Program) {
  _.startsWith("ets_rewrite_static")
  )[0]
  + if (rewrite || rewriteStatic) {
  + throwIfOptionalChaining(node.expression)
  + }
  +
  const processedArguments =
  rewrite || rewriteStatic
  ? node.arguments.map((x) => {
  