-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
48 changed files
with
574 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./utils" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./math" | ||
export * from "./sort" | ||
export { default as UnSorter } from "./unsort" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export async function abs(a) { | ||
return Math.abs(a) | ||
} | ||
|
||
export default abs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const equals = (a, b) => { | ||
return a === b | ||
} | ||
|
||
export { equals } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./sum" | ||
export { equals } from "./equals" | ||
export * as abs from "./abs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function sum (a, b) { | ||
return a + b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class Sorter { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default class UnSorter { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import './a' |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import './b' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import './1' | ||
import './a' | ||
import './b' |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import './2' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { a, b } from './2/2' | ||
import * as something from './2/index' | ||
import './1/a/a' | ||
import('./1/a') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
console.log("hello world!") | ||
export const a = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
console.log("hello world!") | ||
export function foo () { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
console.log("hello world!") | ||
export const b = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
import './index' | ||
|
||
export const b = 2 |
2 changes: 2 additions & 0 deletions
2
internal/js/.parser_test/with-imports-index-imported/other.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
import './' | ||
|
||
export class A {} |
2 changes: 1 addition & 1 deletion
2
internal/js/.parser_test/with-imports-nested/generated/generated.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
console.log("hello world!") | ||
export default () => console.log("hello world!") |
File renamed without changes.
Empty file.
Empty file.
Empty file.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package js | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path" | ||
|
||
"dep-tree/internal/js/grammar" | ||
"dep-tree/internal/utils" | ||
) | ||
|
||
type ExportsCacheKey string | ||
|
||
func (p *Parser) parseExports( | ||
ctx context.Context, | ||
filePath string, | ||
) (context.Context, map[string]string, error) { | ||
cacheKey := ExportsCacheKey(filePath) | ||
if cached, ok := ctx.Value(cacheKey).(map[string]string); ok { | ||
return ctx, cached, nil | ||
} else { | ||
ctx, result, err := p.uncachedParseExports(ctx, filePath) | ||
if err != nil { | ||
return ctx, nil, err | ||
} | ||
ctx = context.WithValue(ctx, cacheKey, result) | ||
return ctx, result, err | ||
} | ||
} | ||
|
||
func (p *Parser) uncachedParseExports( | ||
ctx context.Context, | ||
filePath string, | ||
) (context.Context, map[string]string, error) { | ||
ctx, jsFile, err := grammar.Parse(ctx, filePath) | ||
if err != nil { | ||
return ctx, nil, err | ||
} | ||
exported := make(map[string]string) | ||
for _, stmt := range jsFile.Statements { | ||
switch { | ||
case stmt == nil: | ||
continue | ||
case stmt.DeclarationExport != nil: | ||
exported[stmt.DeclarationExport.Name] = filePath | ||
case stmt.ListExport != nil: | ||
if stmt.ListExport.ExportDeconstruction != nil { | ||
for _, name := range stmt.ListExport.ExportDeconstruction.Names { | ||
exportedName := name.Alias | ||
if exportedName == "" { | ||
exportedName = name.Original | ||
} | ||
exported[exportedName] = filePath | ||
} | ||
} | ||
case stmt.DefaultExport != nil: | ||
if stmt.DefaultExport.Default { | ||
exported["default"] = filePath | ||
} | ||
case stmt.ProxyExport != nil: | ||
var exportFrom string | ||
ctx, exportFrom, err = p.ResolvePath(ctx, stmt.ProxyExport.From, path.Dir(filePath)) | ||
if err != nil { | ||
return ctx, nil, err | ||
} | ||
var proxyExports map[string]string | ||
ctx, proxyExports, err = p.parseExports(ctx, exportFrom) | ||
if err != nil { | ||
return ctx, nil, err | ||
} | ||
if stmt.ProxyExport.ExportAll { | ||
if stmt.ProxyExport.ExportAllAlias != "" { | ||
exported[stmt.ProxyExport.ExportAllAlias] = filePath | ||
} else { | ||
exported = utils.Merge(exported, proxyExports) | ||
} | ||
} else if stmt.ProxyExport.ExportDeconstruction != nil { | ||
for _, name := range stmt.ProxyExport.ExportDeconstruction.Names { | ||
alias := name.Alias | ||
original := name.Original | ||
if alias == "" { | ||
alias = original | ||
} | ||
if proxyPath, ok := proxyExports[original]; ok { | ||
exported[alias] = proxyPath | ||
} else { | ||
return ctx, nil, fmt.Errorf("cannot import \"%s\" from %s", original, exportFrom) | ||
} | ||
} | ||
} | ||
default: | ||
continue | ||
} | ||
} | ||
return ctx, exported, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package js | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const exportsTestFolder = ".exports_test" | ||
|
||
func TestParser_parseExports(t *testing.T) { | ||
cwd, _ := os.Getwd() | ||
|
||
tests := []struct { | ||
Name string | ||
File string | ||
Expected map[string]string | ||
}{ | ||
{ | ||
Name: "test", | ||
File: path.Join(exportsTestFolder, "src", "index.js"), | ||
Expected: map[string]string{ | ||
"Sorter": path.Join(cwd, exportsTestFolder, "src", "utils", "sort.js"), | ||
"UnSorter": path.Join(cwd, exportsTestFolder, "src", "utils", "unsort.js"), | ||
"equals": path.Join(cwd, exportsTestFolder, "src", "utils", "math", "equals.js"), | ||
"abs": path.Join(cwd, exportsTestFolder, "src", "utils", "math", "index.js"), | ||
"sum": path.Join(cwd, exportsTestFolder, "src", "utils", "math", "sum.js"), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.Name, func(t *testing.T) { | ||
a := require.New(t) | ||
parser, err := MakeJsParser(tt.File) | ||
a.NoError(err) | ||
_, exports, err := parser.parseExports(context.Background(), tt.File) | ||
a.NoError(err) | ||
a.Equal(tt.Expected, exports) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.