Skip to content

Conversation

@Jayachand
Copy link
Contributor

🔒 Scanned for secrets using gitleaks 8.28.0

resolves DAW-2752

🔒 Scanned for secrets using gitleaks 8.28.0
Copy link
Collaborator

@abhimanyubabbar abhimanyubabbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some clarifying questions

func (p *JavaScriptParser) ValidateSyntax(code string) error {
result := api.Transform(code, api.TransformOptions{
Loader: api.LoaderJS,
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there an option regarding validating against a specific ECMAScript ?
I mean what we are supporting, we will only validate against those core JS conditionals and types right ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some v0 transformation follow commonjs format. we need to parse both commonjs and ecmascript

for _, err := range result.Errors {
errorMsgs = append(errorMsgs, err.Text)
}
return fmt.Errorf("javascript syntax error: %s", strings.Join(errorMsgs, "; "))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("javascript syntax error: %s", strings.Join(errorMsgs, "; "))
return fmt.Errorf("javascript syntax error: \n\t%s", strings.Join(errorMsgs, "\n\t"))

This might give better readability, wdyt ?

// Transform the code to extract dependencies
// Using Transform with JSX loader to handle modern JS syntax
result := api.Transform(code, api.TransformOptions{
Loader: api.LoaderJSX,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between the JSX loader vs JS loader used above ?


// extractImportsFromTransformedCode extracts import paths from esbuild-transformed code
// esbuild preserves import/require statements in the output, making them easy to find
func extractImportsFromTransformedCode(code string) map[string]bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just name this function as
extractImports(code string) map[string]bool

Comment on lines 68 to 69
// Scan through the code looking for import/require statements
// esbuild's output is well-formatted, making this reliable
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we support both statements ( import and require ) in js code in transformation ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do not support require. filtering them

}

// extractQuotedString extracts the first quoted string from text
func extractQuotedString(text string) string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like a very complex function, can you explain the values which this function can receive and can we simplify it's complexity ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does miss detecting first and last quotes as same ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used claude to try and simplify the function implementation for a bit more readable and I think it generated the output in accordance the expectations:

package parser

import (
	"regexp"
	"strings"
)

type JavaScriptParser struct{}

// ExtractImports extracts all non-relative library imports from JavaScript code
func (p *JavaScriptParser) ExtractImports(code string) []string {
	// Step 1: Remove comments to avoid false matches
	code = removeComments(code)
	
	// Step 2: Extract all import statements
	imports := extractAllImports(code)
	
	// Step 3: Filter out relative/absolute paths
	imports = filterNonRelativeImports(imports)
	
	// Step 4: Deduplicate
	return deduplicateImports(imports)
}

with each implementation downstream ...

🔒 Scanned for secrets using gitleaks 8.28.0
🔒 Scanned for secrets using gitleaks 8.28.0
…e-parser

🔒 Scanned for secrets using gitleaks 8.28.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants