Skip to content

Commit

Permalink
feat: javascript import support (#1027)
Browse files Browse the repository at this point in the history
* fix: log original error when rule is invalid

* fix: always match variables at pattern leaf nodes

* feat: support imports in javascript/typescript
  • Loading branch information
didroe authored Jun 1, 2023
1 parent 7f7a438 commit 5d5073b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
46 changes: 43 additions & 3 deletions new/language/implementation/javascript/javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
}

anonymousPatternNodeParentTypes = []string{}
patternMatchNodeContainerTypes = []string{"required_parameter"}
patternMatchNodeContainerTypes = []string{"import_clause", "required_parameter"}

// $<name:type> or $<name:type1|type2> or $<name>
patternQueryVariableRegex = regexp.MustCompile(`\$<(?P<name>[^>:!\.]+)(?::(?P<types>[^>]+))?>`)
Expand Down Expand Up @@ -110,17 +110,24 @@ func (*javascriptImplementation) AnalyzeFlow(rootNode *tree.Node) error {
if scopedNode := scope.Lookup(node.Content()); scopedNode != nil {
node.UnifyWith(scopedNode)
}

break
}

// typescript: different type of identifier
if parent.Type() == "required_parameter" {
scope.Assign(node.Content(), node)

break
}

if parent.Type() == "arguments" {
callNode := parent.Parent()
callNode.UnifyWith(node)
break
}

if isImportedIdentifier(node) {
scope.Assign(node.Content(), node)
}
case "property_identifier":
parent := node.Parent()
Expand Down Expand Up @@ -221,7 +228,7 @@ func (implementation *javascriptImplementation) PatternIsAnchored(node *tree.Nod
// arrow functions statement_block
// function statement_block
// method statement_block
unAnchored := []string{"statement_block", "class_body", "object_pattern"}
unAnchored := []string{"statement_block", "class_body", "object_pattern", "named_imports"}

isUnanchored := !slices.Contains(unAnchored, parent.Type())
return isUnanchored, isUnanchored
Expand Down Expand Up @@ -294,3 +301,36 @@ func (*javascriptImplementation) ContributesToResult(node *tree.Node) bool {

return true
}

func isImportedIdentifier(node *tree.Node) bool {
parent := node.Parent()
if parent == nil {
return false
}

// import x from "library"
if parent.Type() == "import_clause" {
return true
}

// import * as x from "library"
if parent.Type() == "namespace_import" {
return true
}

if parent.Type() != "import_specifier" {
return false
}

// import { x } from "library"
if parent.ChildByFieldName("alias") == nil {
return true
}

// import { a as x } from "library"
if node.Equal(parent.ChildByFieldName("alias")) {
return true
}

return false
}
2 changes: 1 addition & 1 deletion new/language/patternquery/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (builder *builder) getVariableFor(node *tree.Node) *types.Variable {
continue
}

if node.Content() == variable.DummyValue {
if node.ChildCount() == 0 && node.Content() == variable.DummyValue {
return &variable
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/commands/process/worker/pool/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ func (process *Process) start(config settings.Config) error {
var result = strings.Split(err.Error(), "failed to create detector customDetector:")
if len(result) > 1 {
// custom detector issue ; assume custom rule parse issue
var ruleName = strings.Split(result[1], ":")[0]
var ruleName = strings.TrimSpace(strings.Split(result[1], ":")[0])
log.Debug().Msgf(err.Error())
log.Fatal().Msgf("could not parse rule %s. Is this a custom rule? See documentation on rule patterns and format https://docs.bearer.com/guides/custom-rule/", ruleName)
} else {
log.Fatal().Msgf("failed to start bearer, error with your configuration %s", err)
Expand Down

0 comments on commit 5d5073b

Please sign in to comment.