Skip to content

Commit

Permalink
fix(android): Add support for unusual import statements (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
markushi authored Sep 12, 2023
1 parent 8eae847 commit 4be7aff
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 21 additions & 7 deletions src/android/code-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
}
49 changes: 49 additions & 0 deletions test/android/code-tools.test.ts
Original file line number Diff line number Diff line change
@@ -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` + `//<insert-location>\n` + `class X {}`;
expect(getLastImportLineLocation(code)).toBe(
code.indexOf('//<insert-location>'),
);
});

it('returns proper line index when static import is used', () => {
const code =
`import static a.b.c;\n` + `//<insert-location>\n` + `class X {}`;
expect(getLastImportLineLocation(code)).toBe(
code.indexOf('//<insert-location>'),
);
});

it('returns proper line index when wildcard import is used', () => {
const code = `import a.b.*\n` + `//<insert-location>\n` + `class X {}`;
expect(getLastImportLineLocation(code)).toBe(
code.indexOf('//<insert-location>'),
);
});

it('returns proper line index when alias import is used', () => {
const code =
`import static a.b.c as d\n` + `//<insert-location>\n` + `class X {}`;
expect(getLastImportLineLocation(code)).toBe(
code.indexOf('//<insert-location>'),
);
});

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` +
`//<insert-location>\n` +
`class X {}`;
expect(getLastImportLineLocation(code)).toBe(
code.indexOf('//<insert-location>'),
);
});
});
});

0 comments on commit 4be7aff

Please sign in to comment.