Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(versioning/gradle): ensure strict null check #13554

Merged
merged 46 commits into from Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
49d5b64
enable strictNullCHeck in lib/ versioning/gradle
RahulGautamSingh Jan 6, 2022
2cb7a82
refactor: add null type
RahulGautamSingh Jan 6, 2022
7c35b7d
refactor: ensure strict null
RahulGautamSingh Jan 14, 2022
508494b
Merge branch 'main' into refactor(gradle)/ensure-strict-null-check
RahulGautamSingh Jan 14, 2022
01decf1
Update compare.ts
Jan 14, 2022
4d4684e
refactor code
RahulGautamSingh Jan 14, 2022
853878f
refactor: clean code
RahulGautamSingh Jan 14, 2022
1743702
refactor code
RahulGautamSingh Jan 14, 2022
ee63e72
Merge branch 'main' into refactor(gradle)/ensure-strict-null-check
RahulGautamSingh Jan 14, 2022
d00e57d
refactor code
RahulGautamSingh Jan 14, 2022
83defce
update test case
RahulGautamSingh Jan 14, 2022
64ed7b7
pulled changes
RahulGautamSingh Jan 14, 2022
411d309
Merge branch 'main' into refactor(gradle)/ensure-strict-null-check
RahulGautamSingh Jan 14, 2022
c4455ec
Merge branch 'main' into refactor(gradle)/ensure-strict-null-check
rarkins Jan 14, 2022
defbc67
Merge branch 'main' into refactor(gradle)/ensure-strict-null-check
RahulGautamSingh Jan 14, 2022
66bd6b7
Update lib/versioning/gradle/index.ts
Jan 14, 2022
5d5a359
Update lib/versioning/gradle/index.ts
Jan 14, 2022
facd26b
Merge branch 'main' into refactor(gradle)/ensure-strict-null-check
rarkins Jan 14, 2022
5356065
add new fn valid
RahulGautamSingh Jan 17, 2022
84326e0
add tests for valid
RahulGautamSingh Jan 17, 2022
62839d8
replace isVersion with valid
RahulGautamSingh Jan 17, 2022
425ec95
Merge branch 'main' into refactor(gradle)/ensure-strict-null-check
RahulGautamSingh Jan 17, 2022
1e3540c
resolve coverage issue
RahulGautamSingh Jan 17, 2022
587978b
Merge branch 'main' into refactor(gradle)/ensure-strict-null-check
RahulGautamSingh Jan 17, 2022
3ef74cc
Update lib/versioning/gradle/compare.ts
Jan 17, 2022
1e12545
remove test
RahulGautamSingh Jan 17, 2022
622bc7b
coverage issue: add test-case
RahulGautamSingh Jan 17, 2022
6993f94
Merge branch 'main' into refactor(gradle)/ensure-strict-null-check
RahulGautamSingh Jan 17, 2022
94ca7eb
add test-case
RahulGautamSingh Jan 18, 2022
787240f
pulled changes
RahulGautamSingh Jan 18, 2022
69403c2
refactor code
RahulGautamSingh Jan 18, 2022
a9e64dc
Merge branch 'main' into refactor(gradle)/ensure-strict-null-check
RahulGautamSingh Jan 18, 2022
a8fd1b2
Merge branch 'main' into refactor(gradle)/ensure-strict-null-check
rarkins Jan 18, 2022
4013b15
remove duplicate code
RahulGautamSingh Jan 18, 2022
3ac888b
pulled changes
RahulGautamSingh Jan 18, 2022
aed47f6
move code-logic from isVersion to valid
RahulGautamSingh Jan 18, 2022
1ac5ec6
rename valid to parse
RahulGautamSingh Jan 18, 2022
a31286f
Merge branch 'main' into refactor(gradle)/ensure-strict-null-check
RahulGautamSingh Jan 19, 2022
200139f
revert changes to package.json
RahulGautamSingh Jan 19, 2022
28673d3
pulled changes
RahulGautamSingh Jan 19, 2022
9c637ea
Merge branch 'main' into refactor(gradle)/ensure-strict-null-check
Jan 20, 2022
9503c17
update lockfile
RahulGautamSingh Jan 20, 2022
79d9605
Merge branch 'main' into refactor(gradle)/ensure-strict-null-check
RahulGautamSingh Jan 20, 2022
616c483
Update yarn.lock
viceice Jan 20, 2022
10edc7e
Merge branch 'main' into refactor(gradle)/ensure-strict-null-check
viceice Jan 20, 2022
57b184d
Merge branch 'main' into refactor(gradle)/ensure-strict-null-check
rarkins Jan 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/versioning/gradle/compare.ts
Expand Up @@ -192,6 +192,27 @@ export function compare(left: string, right: string): number {
return 0;
}

export function valid(input: string): Token[] | null {
if (!input) {
return null;
}

if (!regEx(/^[-._+a-zA-Z0-9]+$/i).test(input)) {
return null;
}

if (regEx(/^latest\.?/i).test(input)) {
return null;
}

const tokens = tokenize(input);
// istanbul ignore if: should not happen
if (!tokens?.length) {
return null;
}
return tokens;
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
}

export function isVersion(input: string): boolean {
viceice marked this conversation as resolved.
Show resolved Hide resolved
if (!input) {
return false;
Expand Down
5 changes: 4 additions & 1 deletion lib/versioning/gradle/index.spec.ts
Expand Up @@ -155,11 +155,14 @@ describe('versioning/gradle/index', () => {
test.each`
input | expected
${''} | ${false}
${'latest'} | ${false}
${'foobar'} | ${true}
${'final'} | ${true}
${'1'} | ${true}
${'1..2'} | ${false}
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
${'1.2'} | ${true}
${'1.2.3'} | ${true}
${'1.2.3.4 s'} | ${false}
${'1.2.3.4'} | ${true}
${'v1.2.3.4'} | ${true}
${'1-alpha-1'} | ${false}
Expand Down Expand Up @@ -205,7 +208,7 @@ describe('versioning/gradle/index', () => {

test.each`
version | range | expected
${'1'} | ${'[[]]'} | ${null}
${'1'} | ${'[[]]'} | ${false}
${'0'} | ${'[0,1]'} | ${true}
${'1'} | ${'[0,1]'} | ${true}
${'0'} | ${'(0,1)'} | ${false}
Expand Down
46 changes: 27 additions & 19 deletions lib/versioning/gradle/index.ts
Expand Up @@ -8,7 +8,7 @@ import {
isVersion,
parseMavenBasedRange,
parsePrefixRange,
tokenize,
valid,
} from './compare';

export const id = 'gradle';
Expand All @@ -22,19 +22,19 @@ export const supportedRangeStrategies = ['pin'];
const equals = (a: string, b: string): boolean => compare(a, b) === 0;

const getMajor = (version: string): number | null => {
if (isVersion(version)) {
const tokens = tokenize(version.replace(regEx(/^v/i), ''));
const tokens = valid(version?.replace(regEx(/^v/i), ''));
if (tokens) {
const majorToken = tokens?.[0];
if (majorToken && majorToken.type === TokenType.Number) {
return +majorToken.val;
return majorToken.val as number;
}
}
return null;
};

const getMinor = (version: string): number | null => {
if (isVersion(version)) {
const tokens = tokenize(version.replace(regEx(/^v/i), ''));
const tokens = valid(version?.replace(regEx(/^v/i), ''));
if (tokens) {
const majorToken = tokens[0];
const minorToken = tokens[1];
if (
Expand All @@ -43,16 +43,16 @@ const getMinor = (version: string): number | null => {
minorToken &&
minorToken.type === TokenType.Number
) {
return +minorToken.val;
return minorToken.val as number;
}
return 0;
}
return null;
};

const getPatch = (version: string): number | null => {
if (isVersion(version)) {
const tokens = tokenize(version.replace(regEx(/^v/i), ''));
const tokens = valid(version?.replace(regEx(/^v/i), ''));
if (tokens) {
const majorToken = tokens[0];
const minorToken = tokens[1];
const patchToken = tokens[2];
Expand All @@ -64,7 +64,7 @@ const getPatch = (version: string): number | null => {
patchToken &&
patchToken.type === TokenType.Number
) {
return +patchToken.val;
return patchToken.val as number;
}
return 0;
}
Expand All @@ -88,8 +88,8 @@ const unstable = new Set([
]);

const isStable = (version: string): boolean => {
if (isVersion(version)) {
const tokens = tokenize(version);
const tokens = valid(version);
if (tokens) {
for (const token of tokens) {
if (token.type === TokenType.String) {
const val = token.val.toString().toLowerCase();
Expand All @@ -104,7 +104,8 @@ const isStable = (version: string): boolean => {
};

const matches = (a: string, b: string): boolean => {
if (!a || !isVersion(a) || !b) {
const versionTokens = valid(a);
if (!a || !versionTokens || !b) {
return false;
}
if (isVersion(b)) {
Expand All @@ -117,7 +118,6 @@ const matches = (a: string, b: string): boolean => {
if (tokens.length === 0) {
return true;
}
const versionTokens = tokenize(a);
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
const x = versionTokens
.slice(0, tokens.length)
.map(({ val }) => val)
Expand All @@ -128,7 +128,7 @@ const matches = (a: string, b: string): boolean => {

const mavenBasedRange = parseMavenBasedRange(b);
if (!mavenBasedRange) {
return null;
return false;
}

const { leftBound, leftVal, rightBound, rightVal } = mavenBasedRange;
Expand All @@ -152,8 +152,11 @@ const matches = (a: string, b: string): boolean => {
return leftResult && rightResult;
};

const getSatisfyingVersion = (versions: string[], range: string): string =>
versions.reduce((result, version) => {
function getSatisfyingVersion(
versions: string[],
range: string
): string | null {
return versions.reduce((result: string | null, version) => {
if (matches(version, range)) {
if (!result) {
return version;
Expand All @@ -164,9 +167,13 @@ const getSatisfyingVersion = (versions: string[], range: string): string =>
}
return result;
}, null);
}

const minSatisfyingVersion = (versions: string[], range: string): string =>
versions.reduce((result, version) => {
function minSatisfyingVersion(
versions: string[],
range: string
): string | null {
return versions.reduce((result: string | null, version) => {
if (matches(version, range)) {
if (!result) {
return version;
Expand All @@ -177,6 +184,7 @@ const minSatisfyingVersion = (versions: string[], range: string): string =>
}
return result;
}, null);
}

function getNewValue({
currentValue,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.strict.json
Expand Up @@ -43,7 +43,7 @@
"lib/proxy.ts",
"lib/types/**/*.ts",
"lib/util/**/*.ts",
"lib/versioning/gradle/compare.ts",
"lib/versioning/gradle/**/*.ts",
"lib/versioning/hex/**/*.ts",
"lib/versioning/loose/**/*.ts",
"lib/versioning/maven/**/*.ts",
Expand Down