Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable esparse's TS option to avoid removal of unused imports #87

Merged
merged 5 commits into from
Oct 14, 2022
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
21 changes: 11 additions & 10 deletions internal/backends/nodejs/grab.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package nodejs

import (
"io/ioutil"
"log"
"path/filepath"
"strings"

"github.com/amasad/esparse/ast"
"github.com/amasad/esparse/logging"
"github.com/amasad/esparse/parser"
"github.com/replit/upm/internal/api"
"github.com/replit/upm/internal/util"
"io/ioutil"
"log"
"path/filepath"
"strings"
)

var internalModules = []string{
Expand Down Expand Up @@ -70,17 +71,17 @@ type parseResult struct {

func parseFile(source logging.Source, results chan parseResult) {
parseOptions := parser.ParseOptions{
IsBundling: true,
IsBundling: true,
MangleSyntax: false,
}

// Always parse jsx
parseOptions.JSX.Parse = true
// TS parsing strips unused imports, that becomes
// inconsistent with the regex-based import searching used to
// generate the guess hash, so we disable it
parseOptions.TS.Parse = false

ext := getExt(source.AbsolutePath)

if ext == ".ts" || ext == ".tsx" {
parseOptions.TS.Parse = true
}
logo, _ := logging.NewDeferLog()

ast, ok := parser.Parse(logo, source, parseOptions)
Expand Down
147 changes: 115 additions & 32 deletions internal/backends/nodejs/nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"github.com/replit/upm/internal/api"
)

type TestCase struct {
scenario string
backend api.LanguageBackend
fileContent string
expected map[api.PkgName]bool
}

func TestNodejsYarnBackend_Guess(t *testing.T) {
tcs := []struct {
scenario string
backend api.LanguageBackend
fileContent string
expected map[api.PkgName]bool
}{
tcs := []TestCase{
{
scenario: "Returns the imports when given a JavaScript file with normal imports",
backend: NodejsNPMBackend,
Expand All @@ -27,6 +29,16 @@ func TestNodejsYarnBackend_Guess(t *testing.T) {
"react": true,
},
},
{
scenario: "Returns normal requires",
backend: NodejsNPMBackend,
fileContent: `
const request = require('request');
`,
expected: map[api.PkgName]bool{
"request": true,
},
},
{
scenario: "Ignore internal imports",
backend: NodejsNPMBackend,
Expand All @@ -35,6 +47,18 @@ func TestNodejsYarnBackend_Guess(t *testing.T) {
`,
expected: map[api.PkgName]bool{},
},
{
scenario: "Returns both requires and imports in mixed file",
backend: NodejsNPMBackend,
fileContent: `
const request = require('request');
import yargs from 'yargs';
`,
expected: map[api.PkgName]bool{
"request": true,
"yargs": true,
},
},
{
scenario: "Ignore local file imports",
backend: NodejsNPMBackend,
Expand Down Expand Up @@ -73,41 +97,100 @@ func TestNodejsYarnBackend_Guess(t *testing.T) {
"@material-ui/core": true,
},
},
}

for _, tc := range tcs {
tc := tc
t.Run(tc.scenario, func(t *testing.T) {
dir, err := ioutil.TempDir(".", "temp")
if err != nil {
t.Error(err)
{
scenario: "Conditional require",
backend: NodejsNPMBackend,
fileContent: `
if (process.env.NODE_ENV === "production") {
require("node-fetch");
}
defer os.RemoveAll(dir)
`,
expected: map[api.PkgName]bool{
"node-fetch": true,
},
},
{
scenario: "types then conditional require",
backend: NodejsNPMBackend,
fileContent: `
type Field<T> = { field: string; };

file, err := ioutil.TempFile(dir, "*.js")
if err != nil {
t.Error(err)
if (process.env.NODE_ENV === "production") {
require("node-fetch");
}
`,
expected: map[api.PkgName]bool{},
},
{
scenario: "dynamic import",
backend: NodejsNPMBackend,
fileContent: `

_, err = file.WriteString(tc.fileContent)
if err != nil {
t.Error(err)
if (process.env.NODE_ENV === "production") {
import("node-fetch");
}
`,
expected: map[api.PkgName]bool{
"node-fetch": true,
},
},
{
scenario: "typings then dynamic import",
backend: NodejsNPMBackend,
fileContent: `
type Field<T> = { field: string; };

result, ok := tc.backend.Guess()
if !ok {
t.Errorf("Guess return a non true value")
if (process.env.NODE_ENV === "production") {
import("node-fetch");
}
`,
expected: map[api.PkgName]bool{},
},
}

if len(tc.expected) != len(result) {
t.Errorf("Expected length of result to match length of expected")
}
for _, tc := range tcs {
tc := tc
t.Run(tc.scenario+"in js", func(t *testing.T) {
verify(t, tc, "js")

for key := range tc.expected {
if _, ok := result[key]; !ok {
t.Errorf("Key %s not found in result map", key)
}
}
})

t.Run(tc.scenario+" in ts", func(t *testing.T) {
verify(t, tc, "ts")

})
}
}

func verify(t *testing.T, tc TestCase, extension string) {
dir, err := ioutil.TempDir(".", "temp")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(dir)

file, err := ioutil.TempFile(dir, "*."+extension)
if err != nil {
t.Error(err)
}

_, err = file.WriteString(tc.fileContent)
if err != nil {
t.Error(err)
}

result, ok := tc.backend.Guess()
if !ok {
t.Errorf("Guess return a non true value")
}

if len(tc.expected) != len(result) {
t.Errorf("Expected length of result to match length of expected")
}

for key := range tc.expected {
if _, ok := result[key]; !ok {
t.Errorf("Key %s not found in result map", key)
}
}
}