From 4be7aff505ed23e21c4235bcb8d21813104f6687 Mon Sep 17 00:00:00 2001 From: Markus Hintersteiner Date: Tue, 12 Sep 2023 11:50:13 +0200 Subject: [PATCH] fix(android): Add support for unusual import statements (#440) --- CHANGELOG.md | 1 + src/android/code-tools.ts | 28 ++++++++++++++----- test/android/code-tools.test.ts | 49 +++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 test/android/code-tools.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 371c9fcb..684562f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - feat(sourcemaps): Automatically insert Sentry Webpack plugin (#432) +- fix(android): Add support for unusual import statements (#440) ## 3.11.0 diff --git a/src/android/code-tools.ts b/src/android/code-tools.ts index faad5b7f..601e68e5 100644 --- a/src/android/code-tools.ts +++ b/src/android/code-tools.ts @@ -103,13 +103,8 @@ export function patchMainActivity(activityFile: string | undefined): boolean { return true; } - const importRegex = /import\s+[\w.]+;?/gim; - let importsMatch = importRegex.exec(activityContent); - let importIndex = 0; - while (importsMatch) { - importIndex = importsMatch.index + importsMatch[0].length + 1; - importsMatch = importRegex.exec(activityContent); - } + const importIndex = getLastImportLineLocation(activityContent); + let newActivityContent; if (activityFile.endsWith('.kt')) { newActivityContent = @@ -154,3 +149,22 @@ export function patchMainActivity(activityFile: string | undefined): boolean { return true; } + +/** + * Returns the string index of the last import statement in the given code file. + * Works for both Java and Kotlin import statements. + * + * @param sourceCode + * @returns the insert index, or 0 if none found. + */ +export function getLastImportLineLocation(sourceCode: string): number { + const importRegex = /import(?:\sstatic)?\s+[\w.*]+(?: as [\w.]+)?;?/gim; + + let importsMatch = importRegex.exec(sourceCode); + let importIndex = 0; + while (importsMatch) { + importIndex = importsMatch.index + importsMatch[0].length + 1; + importsMatch = importRegex.exec(sourceCode); + } + return importIndex; +} diff --git a/test/android/code-tools.test.ts b/test/android/code-tools.test.ts new file mode 100644 index 00000000..5a018467 --- /dev/null +++ b/test/android/code-tools.test.ts @@ -0,0 +1,49 @@ +//@ts-ignore +import { getLastImportLineLocation } from '../../src/android/code-tools'; + +describe('code-tools', () => { + describe('getLastImportLineLocation', () => { + it('returns proper line index', () => { + const code = `import a.b.c;\n` + `//\n` + `class X {}`; + expect(getLastImportLineLocation(code)).toBe( + code.indexOf('//'), + ); + }); + + it('returns proper line index when static import is used', () => { + const code = + `import static a.b.c;\n` + `//\n` + `class X {}`; + expect(getLastImportLineLocation(code)).toBe( + code.indexOf('//'), + ); + }); + + it('returns proper line index when wildcard import is used', () => { + const code = `import a.b.*\n` + `//\n` + `class X {}`; + expect(getLastImportLineLocation(code)).toBe( + code.indexOf('//'), + ); + }); + + it('returns proper line index when alias import is used', () => { + const code = + `import static a.b.c as d\n` + `//\n` + `class X {}`; + expect(getLastImportLineLocation(code)).toBe( + code.indexOf('//'), + ); + }); + + it('returns proper line index when multiple imports are present', () => { + const code = + `import static a.b.c as d\n` + + `import a.b.*\n` + + `import static a.b.c;\n` + + `import a.b.c;\n` + + `//\n` + + `class X {}`; + expect(getLastImportLineLocation(code)).toBe( + code.indexOf('//'), + ); + }); + }); +});