-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to lint based on word boundaries
This commit introduces a new syntax for matching words with the ReservedWords linter and is intended to be used with the upcoming sensitive words linter defined in #1364. In addition to supporting wildcard searches ("*" prefix, suffix, and contains), we now support matching based on word boundaries. This commit introduces the "terms" keyword for word boundary searches and adds dedicated abstractions for word boundary and wildcard matching. For example, "access key id" will match "AccessKeyId", "access_key_id", "accessKeyID", "access_key_id100", "AccesKeyIDValue". It will also match when all the words are concatenated together: "accesskeyid". However, it will not match "accesskey_id" because it only has two word boundaries ("accesskey" and "id").
- Loading branch information
Showing
6 changed files
with
538 additions
and
50 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
86 changes: 86 additions & 0 deletions
86
smithy-linters/src/main/java/software/amazon/smithy/linters/WildcardMatcher.java
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,86 @@ | ||
/* | ||
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.linters; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.function.Predicate; | ||
import software.amazon.smithy.utils.StringUtils; | ||
|
||
final class WildcardMatcher implements Predicate<String> { | ||
|
||
private final List<Predicate<String>> predicates = new ArrayList<>(); | ||
|
||
@Override | ||
public boolean test(String text) { | ||
if (StringUtils.isEmpty(text)) { | ||
return false; | ||
} | ||
|
||
text = text.toLowerCase(Locale.ENGLISH); | ||
for (Predicate<String> predicate : predicates) { | ||
if (predicate.test(text)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
void addSearch(String pattern) { | ||
if (StringUtils.isEmpty(pattern)) { | ||
throw new IllegalArgumentException("Invalid empty pattern"); | ||
} else if (pattern.equals("*")) { | ||
throw new IllegalArgumentException("Invalid wildcard pattern: *"); | ||
} else { | ||
predicates.add(parseWildcardPattern(pattern)); | ||
} | ||
} | ||
|
||
private static Predicate<String> parseWildcardPattern(String pattern) { | ||
boolean suffix = false; | ||
boolean prefix = false; | ||
|
||
// Find any leading or ending star, ensure that no inner stars are used. | ||
StringBuilder result = new StringBuilder(); | ||
for (int i = 0; i < pattern.length(); i++) { | ||
char c = pattern.charAt(i); | ||
if (c == '*') { | ||
if (i == 0) { | ||
suffix = true; | ||
} else if (i == pattern.length() - 1) { | ||
prefix = true; | ||
} else { | ||
throw new IllegalArgumentException("Invalid inner '*' in wildcard pattern: " + pattern); | ||
} | ||
} else { | ||
result.append(Character.toLowerCase(c)); | ||
} | ||
} | ||
|
||
String needle = result.toString(); | ||
if (suffix && prefix) { | ||
return text -> text.contains(needle); | ||
} else if (suffix) { | ||
return text -> text.endsWith(needle); | ||
} else if (prefix) { | ||
return text -> text.startsWith(needle); | ||
} else { | ||
return text -> text.equals(needle); | ||
} | ||
} | ||
} |
Oops, something went wrong.