Skip to content

Commit

Permalink
Add customizable file extensions (#5)
Browse files Browse the repository at this point in the history
* Always allow empty args in `IF` directive
  • Loading branch information
EthanThatOneKid authored Jun 21, 2023
1 parent 5c73743 commit ac8177b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
8 changes: 7 additions & 1 deletion ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package difflint
import (
"encoding/json"
"log"
"os"
)

var (
Expand Down Expand Up @@ -66,7 +67,12 @@ func NewExtMap(path string) *ExtMap {
if path != "" {
// Unmarshal the JSON file.
var extFile ExtFileJSON
if err := json.Unmarshal([]byte(path), &extFile); err != nil {
bytes, err := os.ReadFile(path)
if err != nil {
log.Fatalf("error reading JSON file %q: %v", path, err)
}

if err := json.Unmarshal(bytes, &extFile); err != nil {
log.Fatalf("error unmarshaling JSON file %q: %v", path, err)
}

Expand Down
15 changes: 7 additions & 8 deletions lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,17 @@ func parseRules(file string, tokens []token, ranges []Range) ([]Rule, error) {
return nil, errors.New("unexpected IF directive at " + file + ":" + string(rune(token.line)))
}

r.Hunk.File = file
r.Hunk.Range = Range{Start: token.line}

targets, err := parseTargets(parseTargetsOptions{
args: token.args,
allowEmpty: r.ID != nil,
args: token.args,
allowEmptyArgs: true,
})
if err != nil {
return nil, err
}

r.Targets = targets
r.Hunk.File = file
r.Hunk.Range = Range{Start: token.line}

case directiveEnd:
if r.Hunk.File == "" {
Expand Down Expand Up @@ -168,13 +167,13 @@ func parseRules(file string, tokens []token, ranges []Range) ([]Rule, error) {

// parseTargets parses the given list of targets and returns the list of targets.
type parseTargetsOptions struct {
args []string
allowEmpty bool
args []string
allowEmptyArgs bool
}

// parseTargets parses the given list of targets and returns the list of targets.
func parseTargets(o parseTargetsOptions) ([]Target, error) {
if !o.allowEmpty && len(o.args) == 0 {
if !o.allowEmptyArgs && len(o.args) == 0 {
return nil, errors.New("missing target")
}

Expand Down
3 changes: 2 additions & 1 deletion rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func RulesMapFromHunks(hunks []Hunk, options LintOptions) (map[string][]Rule, ma
for pathname, ranges := range rangesMap {
rules, err := RulesFromFile(pathname, ranges, visited, &wg, options)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to parse rules for file %s", pathname)
return nil, nil, err
}

for _, rule := range rules {
Expand All @@ -71,6 +71,7 @@ func RulesMapFromHunks(hunks []Hunk, options LintOptions) (map[string][]Rule, ma
return rulesMap, targetsMap, nil
}

// RulesFromFile parses rules from the given file and returns the list of rules.
func RulesFromFile(pathname string, ranges []Range, visited map[string]struct{}, wg *sync.WaitGroup, options LintOptions) ([]Rule, error) {
visited[pathname] = struct{}{}

Expand Down

0 comments on commit ac8177b

Please sign in to comment.