Tools: 馃搸 Add `as const` TypeScript syntax support

Created on 11 Jul 2020  路  5Comments  路  Source: rome/tools

We will need to add a TSConstKeyword node or something. Right now we parse it as a ReferenceIdentifier and we throw an undeclaredVariables error.

parser task typescript

All 5 comments

@sebmck can I take a look at this?

@VictorHom Sure. packages/@romefrontend/js-parser/parser/typescript.ts is the relevant file. Let me know if you need other pointers!

@sebmck I'm trying to go thru this section of rome. I see that when I parse
let v1 = 'abc' as const

it becomes:

JSRoot {
  comments: Array []
  corrupt: false
  diagnostics: Array []
  directives: Array []
  filename: "project-rome_development_vh/foo.ts"
  hasHoistedVars: false
  mtime: 1_595_085_960_986.6138
  sourceType: "module"
  syntax: Array ["ts"]
  body: Array [
    JSVariableDeclarationStatement {
      declaration: JSVariableDeclaration {
        kind: "let"
        declarations: Array [
          JSVariableDeclarator {
            id: JSBindingIdentifier {name: "v1"}
            init: TSAsExpression {
              expression: JSStringLiteral {value: "abc"}
              typeAnnotation: TSTypeReference {typeName: JSReferenceIdentifier {name: "const"}}
            }
          }
        ]
      }
    }
  ]
}

is the TSAsExpression enough to indicate as const?

@VictorHom Yeah it already parses, we need to look for that node structure and create a new dedicated node. Otherwise we'll have to go around special-casing const everywhere since it looks like a variable.

We should create a new TSConstKeyword node and replace it when we see TSTypeReference/JSReferenceIdentifier with the name const in the typeAnnotation field.

You can find the relevant function where we parse TSAsExpression in the parseExpressionOp method located in packages/@romefrontend/js-parser/parser/expression.ts.

Implemented via #838

Was this page helpful?
0 / 5 - 0 ratings