Skip to content

Commit

Permalink
fix: Syntax Error when parse trigger files
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikoTan committed Jul 18, 2021
1 parent 4edda28 commit 05f61c7
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/timeline/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EventEmitter, languages, TextDocumentContentProvider, Uri, window, work
import { parseAsync, traverse } from "@babel/core";
import { isArrayExpression, isIdentifier, isObjectExpression, isObjectProperty, isStringLiteral } from "@babel/types";
import { promisify } from "bluebird";
import * as ts from "typescript";

import {
CommonReplacement,
Expand All @@ -18,7 +19,18 @@ export const extractReplacements = async (
): Promise<TimelineReplace[]> => {

const ret: TimelineReplace[] = [];
traverse(await parseAsync(String(await promisify(readFile)(triggerPath))), {

let fileContent = String(await promisify(readFile)(triggerPath));

// transpile typescript first, then feed to babel.
if (triggerPath.endsWith(".ts")) {
fileContent = ts.transpile(fileContent, {
target: ts.ScriptTarget.ES2020,
esModuleInterop: true,
});
}

traverse(await parseAsync(fileContent), {
// eslint-disable-next-line @typescript-eslint/naming-convention
ObjectProperty(path) {
const node = path.node;
Expand Down Expand Up @@ -242,8 +254,8 @@ export const translateTimeline = async (): Promise<void> => {

// TODO: very hacky way
// maybe it should be the `timelineFile` or `timeline` key in the trigger file
if (filename.endsWith(".js")) {
filename = filename.replace(/\.js$/, ".txt");
if (filename.endsWith(".js") || filename.endsWith(".ts")) {
filename = filename.replace(/\.(js|ts)$/, ".txt");
}

// try to get locale settings in settings.json
Expand Down Expand Up @@ -303,7 +315,7 @@ export const translateTimeline = async (): Promise<void> => {
// TODO: not only monitor the current document,
// but also the related files.
workspace.onDidChangeTextDocument((e) => {
if (e.document.fileName.replace(/\.(js|txt)$/, "") === document.fileName.replace(/\.(js|txt)$/, "")) {
if (e.document.fileName.replace(/\.(js|ts|txt)$/, "") === document.fileName.replace(/\.(js|ts|txt)$/, "")) {
translatedTimelineProvider.onDidChangeEmitter.fire(uri);
}
});
Expand Down

0 comments on commit 05f61c7

Please sign in to comment.