Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions internal/js/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,29 @@ func (ps *parsingState) parseImpl(src, filename string, commonJSWrap bool) (*ast
return prg, code, nil
}

if strings.HasSuffix(filename, ".ts") {
if err := ps.compiler.usage.Uint64(usageParsedTSFilesKey, 1); err != nil {
ps.compiler.logger.WithError(err).Warn("couldn't report usage for " + usageParsedTSFilesKey)
}
code, ps.srcMap, err = StripTypes(src, filename)
if err != nil {
return nil, "", err
}
if ps.loader != nil {
// This hack is required for the source map to work
code += "\n//# sourceMappingURL=" + internalSourceMapURL
}
ps.commonJSWrapped = false
return ps.parseImpl(code, filename, commonJSWrap)
isTsExtensionFile := strings.HasSuffix(filename, ".ts")
isStdin := filename == "file:///-"
if !isTsExtensionFile && !isStdin {
return nil, "", err
}

code, ps.srcMap, err = StripTypes(src, filename)
if err != nil {
return nil, "", err
}

// At this point we have stripped types successfully, no matter whether it was a .ts file
// or a script from stdin, and so we can report that a TS file been parsed successfully.
if err := ps.compiler.usage.Uint64(usageParsedTSFilesKey, 1); err != nil {
ps.compiler.logger.WithError(err).Warn("couldn't report usage for " + usageParsedTSFilesKey)
}

if ps.loader != nil {
// This hack is required for the source map to work
code += "\n//# sourceMappingURL=" + internalSourceMapURL
}
return nil, "", err
ps.commonJSWrapped = false
return ps.parseImpl(code, filename, commonJSWrap)
}

func (ps *parsingState) wrap(code, filename string) string {
Expand Down
13 changes: 13 additions & 0 deletions internal/js/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ func TestCompile(t *testing.T) {
assert.IsType(t, parser.ErrorList{}, err)
assert.Contains(t, err.Error(), `Line 1:4 Unexpected token =>`)
})

t.Run("Stdin TS", func(t *testing.T) {
t.Parallel()
c := New(testutils.NewLogger(t))
src := `1 + (((): number => 2)());`
prg, _, err := c.Parse(src, "file:///-", false, false)
require.NoError(t, err)
pgm, err := sobek.CompileAST(prg, true)
require.NoError(t, err)
v, err := sobek.New().RunProgram(pgm)
require.NoError(t, err)
assert.Equal(t, int64(3), v.Export())
})
}

func TestCorruptSourceMap(t *testing.T) {
Expand Down
Loading