Skip to content

Commit

Permalink
update pkglint to use regex
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinZZ committed Jan 29, 2024
1 parent 34e9ee8 commit 0129288
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tools/@aws-cdk/pkglint/lib/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ export class MustHaveIntegCommand extends ValidationRule {
public validate(pkg: PackageJson): void {
if (!hasIntegTests(pkg)) { return; }

expectJSON(this.name, pkg, 'scripts.integ', /integ-runner/);
expectJSON(this.name, pkg, 'scripts.integ', /integ-runner/, undefined, false, true);

// We can't ACTUALLY require cdk-build-tools/package.json here,
// because WE don't depend on cdk-build-tools and we don't know if
Expand Down
19 changes: 17 additions & 2 deletions tools/@aws-cdk/pkglint/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,32 @@ import { PackageJson, PKGLINT_IGNORES } from './packagejson';
/**
* Expect a particular JSON key to be a given value
*/
export function expectJSON(ruleName: string, pkg: PackageJson, jsonPath: string, expected: any, ignore?: RegExp, caseInsensitive: boolean = false) {
export function expectJSON(
ruleName: string,
pkg: PackageJson,
jsonPath: string,
expected: any,
ignore?: RegExp,
caseInsensitive: boolean = false,
regexMatch: boolean = false,
) {
const parts = jsonPath.split('.');
const actual = deepGet(pkg.json, parts);
if (applyCaseInsensitive(applyIgnore(actual)) !== applyCaseInsensitive(applyIgnore(expected))) {
if (checkEquality()) {
pkg.report({
ruleName,
message: `${jsonPath} should be ${JSON.stringify(expected)}${ignore ? ` (ignoring ${ignore})` : ''}, is ${JSON.stringify(actual)}`,
fix: () => { deepSet(pkg.json, parts, expected); },
});
}

function checkEquality(): boolean {
if (regexMatch) {
return !expected.test(applyCaseInsensitive(applyIgnore(actual)));
}
return applyCaseInsensitive(applyIgnore(actual)) !== applyCaseInsensitive(applyIgnore(expected));
}

function applyIgnore(val: any): string {
if (!ignore || val == null) { return JSON.stringify(val); }
const str = JSON.stringify(val);
Expand Down

0 comments on commit 0129288

Please sign in to comment.