Skip to content

Commit

Permalink
[ShellScript] Fix test expressions (#3806)
Browse files Browse the repository at this point in the history
Fixes #3804

This commit ...

1. scopes unquoted identifiers in test expressions `string.unquoted` instead
   of `variable.parameter`.
2. removes pattern matching (`=~ ...`) from `test ...` command as the command
   supports basic string comparisons only.
  • Loading branch information
deathaxe authored Jul 13, 2023
1 parent 13c51ae commit 67db4d2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 66 deletions.
16 changes: 5 additions & 11 deletions ShellScript/Bash.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -723,10 +723,10 @@ contexts:
- match: \(
scope: punctuation.section.group.begin.shell
push: cmd-test-args-group-body
- match: ([=!]=|=~)\s*
captures:
1: keyword.operator.comparison.shell
push: cmd-test-args-pattern
- match: '[=!]='
scope: keyword.operator.comparison.shell
- match: =~
scope: invalid.illegal.operator.shell
- include: test-expression-common

cmd-test-args-group-body:
Expand All @@ -737,11 +737,6 @@ contexts:
- include: cmd-test-args-common
- include: eoc-pop

cmd-test-args-pattern:
- meta_content_scope: meta.pattern.regexp.shell
- include: test-pattern-common
- include: eoc-pop

test-expression-body:
- match: \(
scope: punctuation.section.group.begin.shell
Expand Down Expand Up @@ -834,9 +829,8 @@ contexts:
- include: comments
- include: booleans
- include: numbers
- include: strings
- include: expansions-variables
- include: variables
- include: strings-unquoted
- include: line-continuations

###[ UNALIAS BUILTINS ]########################################################
Expand Down
80 changes: 25 additions & 55 deletions ShellScript/test/syntax_test_bash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2061,77 +2061,47 @@ test-=
test+=
#^^^^^ - support.function

test var != 0
test $var != 0
#<- meta.function-call.identifier.shell support.function.test.shell
#^^^ meta.function-call.identifier.shell support.function.test.shell
# ^^^^^^^^ meta.function-call.arguments.shell - meta.pattern
# ^ meta.function-call.arguments.shell meta.pattern.regexp.shell
# ^ - meta.function-call
# ^^ keyword.operator.comparison.shell
# ^ - constant.numeric
# ^^^^^^^^^ meta.function-call.arguments.shell - meta.pattern
# ^ meta.function-call.arguments.shell
# ^ - meta.function-call
# ^^ keyword.operator.comparison.shell
# ^ constant.numeric.value.shell

test var == true
test $var == true
#<- meta.function-call.identifier.shell support.function.test.shell
#^^^ meta.function-call.identifier.shell support.function.test.shell
# ^^^^^^^^^^^^ meta.function-call.arguments.shell - meta.pattern
# ^^ keyword.operator.comparison.shell
# ^^^^ constant.language.boolean.shell
# ^^^^^^^^^^^^^ meta.function-call.arguments.shell - meta.pattern
# ^^ keyword.operator.comparison.shell
# ^^^^ constant.language.boolean.shell

test str == "str"
#<- meta.function-call.identifier.shell support.function.test.shell
#^^^ meta.function-call.identifier.shell support.function.test.shell
# ^^^^^^^^^^^^^ meta.function-call.arguments.shell
# ^^^ meta.string.shell string.unquoted.shell
# ^^ keyword.operator.comparison.shell
# ^^^^^ string.quoted.double.shell

test var[0] != var[^0-9]*$
#<- meta.function-call.identifier.shell support.function.test.shell
#^^^ meta.function-call.identifier.shell support.function.test.shell
# ^^^^^^^^^^^^^^^^^^^^^^ meta.function-call.arguments.shell
# ^^^ meta.item-access.shell
# ^^^^^^ meta.string.shell string.unquoted.shell
# ^^ keyword.operator.comparison.shell
# ^^^^^^^^^^^ meta.pattern.regexp.shell
# ^^^^^^^^^^^ meta.string.shell string.unquoted.shell - meta.pattern

test var == [
# <- meta.function-call.identifier.shell support.function.test.shell
#^^^ meta.function-call.identifier.shell support.function.test.shell
# ^^^^^^^^^ meta.function-call.arguments.shell
# ^^^ meta.variable.shell variable.other.readwrite.shell
# ^^ keyword.operator.comparison.shell
# ^ meta.pattern.regexp.shell - meta.set

test var == ]
# <- meta.function-call.identifier.shell support.function.test.shell
#^^^ meta.function-call.identifier.shell support.function.test.shell
# ^^^^^^^^^ meta.function-call.arguments.shell
# ^^^ meta.variable.shell variable.other.readwrite.shell
# ^^ keyword.operator.comparison.shell
# ^ meta.pattern.regexp.shell - meta.set

test var == [[:alpha:
# <- meta.function-call.identifier.shell support.function.test.shell
#^^^ meta.function-call.identifier.shell support.function.test.shell
# ^^^^^^^^^^^^^^^^^ meta.function-call.arguments.shell
# ^^^ meta.variable.shell variable.other.readwrite.shell
# ^^ keyword.operator.comparison.shell
# ^^^^^^^^^ meta.pattern.regexp.shell - meta.set

test var == [[:alpha:]
# <- meta.function-call.identifier.shell support.function.test.shell
#^^^ meta.function-call.identifier.shell support.function.test.shell
# ^^^^^^^^^^^^^^^^^^ meta.function-call.arguments.shell
# ^^^ meta.variable.shell variable.other.readwrite.shell
# ^^ keyword.operator.comparison.shell
# ^ meta.pattern.regexp.shell - meta.set
# ^^^^^^^^^ meta.pattern.regexp.shell meta.set.regexp.shell

test var == [[:alpha:]]
# <- meta.function-call.identifier.shell support.function.test.shell
test ${var[0]} != var[^0-9]*$
#<- meta.function-call.identifier.shell support.function.test.shell
#^^^ meta.function-call.identifier.shell support.function.test.shell
# ^^^^^^^^^^^^^^^^^^^ meta.function-call.arguments.shell
# ^^^ meta.variable.shell variable.other.readwrite.shell
# ^^ keyword.operator.comparison.shell
# ^^^^^^^^^^^ meta.pattern.regexp.shell meta.set.regexp.shell
# ^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function-call.arguments.shell
# ^^^^^^^^^ meta.interpolation.parameter.shell
# ^^^ variable.other.readwrite.shell
# ^^^ meta.item-access.shell
# ^^ keyword.operator.comparison.shell
# ^^^^^^^^^^^ meta.string.shell string.unquoted.shell - meta.pattern

test expr -a expr -o expr -- | cmd |& cmd
# <- meta.function-call.identifier.shell support.function.test.shell
Expand All @@ -2155,8 +2125,8 @@ test ! ($line =~ ^[0-9]+$)
# ^ keyword.operator.logical.shell
# ^ punctuation.section.group.begin.shell
# ^^^^^ variable.other.readwrite.shell
# ^^ keyword.operator.comparison.shell
# ^^^^^^^^ meta.pattern.regexp.shell
# ^^ invalid.illegal.operator.shell
# ^^^^^^^^ meta.string.shell string.unquoted.shell

test ! ($line =~ ^[0-9]+$) >> /file
# <- meta.function-call.identifier.shell support.function.test.shell
Expand All @@ -2169,8 +2139,8 @@ test ! ($line =~ ^[0-9]+$) >> /file
# ^ keyword.operator.logical.shell
# ^ punctuation.section.group.begin.shell
# ^^^^^ variable.other.readwrite.shell
# ^^ keyword.operator.comparison.shell
# ^^^^^^^^ meta.pattern.regexp.shell
# ^^ invalid.illegal.operator.shell
# ^^^^^^^^ meta.string.shell string.unquoted.shell
# ^ punctuation.section.group.end.shell
# ^^ keyword.operator.assignment.redirection.shell

Expand Down

0 comments on commit 67db4d2

Please sign in to comment.