Skip to content

Commit

Permalink
fix: Package class name regex tweaks on Android (react-native-communi…
Browse files Browse the repository at this point in the history
…ty#563)

* Android - find package's class name regex tweaks + tests

* test tweaks
  • Loading branch information
dratwas authored and thymikee committed Jul 25, 2019
1 parent 4c9512a commit eac98c0
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 6 deletions.
142 changes: 142 additions & 0 deletions packages/platform-android/src/config/__fixtures__/android.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,145 @@ exports.noPackage = {
},
},
};

exports.findPackagesClassNameKotlinValid = [
`
class SomeExampleKotlinPackage() : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): MutableList<NativeModule> {
return Collections.emptyList()
}
override fun createViewManagers(reactContext: ReactApplicationContext): MutableList<SimpleViewManager<View>> {
return Collections.emptyList()
}
}`,
`
class SomeExampleKotlinPackage : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): MutableList<NativeModule> {
return Collections.emptyList()
}
override fun createViewManagers(reactContext: ReactApplicationContext): MutableList<SimpleViewManager<View>> {
return Collections.emptyList()
}
}`,
`
class SomeExampleKotlinPackage:ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): MutableList<NativeModule> {
return Collections.emptyList()
}
override fun createViewManagers(reactContext: ReactApplicationContext): MutableList<SimpleViewManager<View>> {
return Collections.emptyList()
}
}`,
`
class SomeExampleKotlinPackage
:
ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): MutableList<NativeModule> {
return Collections.emptyList()
}
override fun createViewManagers(reactContext: ReactApplicationContext): MutableList<SimpleViewManager<View>> {
return Collections.emptyList()
}
}`,
`
class SomeExampleKotlinPackage() : SomeDelegate, OtherDelegate, ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): MutableList<NativeModule> {
return Collections.emptyList()
}
override fun createViewManagers(reactContext: ReactApplicationContext): MutableList<SimpleViewManager<View>> {
return Collections.emptyList()
}
}`,
`
class SomeExampleKotlinPackage(val name: String) : SomeDelegate, OtherDelegate, ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): MutableList<NativeModule> {
return Collections.emptyList()
}
override fun createViewManagers(reactContext: ReactApplicationContext): MutableList<SimpleViewManager<View>> {
return Collections.emptyList()
}
}`,
`
class SomeExampleKotlinPackage : SomeSuper(), ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): MutableList<NativeModule> {
return Collections.emptyList()
}
override fun createViewManagers(reactContext: ReactApplicationContext): MutableList<SimpleViewManager<View>> {
return Collections.emptyList()
}
}`,
];

exports.findPackagesClassNameKotlinNotValid = [
`
class SomeExampleKotlinPackage() {
override fun createNativeModules(reactContext: ReactApplicationContext): MutableList<NativeModule> {
return Collections.emptyList()
}
override fun createViewManagers(reactContext: ReactApplicationContext): MutableList<SimpleViewManager<View>> {
return Collections.emptyList()
}
}`,
`
class SomeExampleKotlinPackage {
val package: ReactPackage = ReactPackage()
}`,
`
class ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): MutableList<NativeModule> {
return Collections.emptyList()
}
override fun createViewManagers(reactContext: ReactApplicationContext): MutableList<SimpleViewManager<View>> {
return Collections.emptyList()
}
}`,
];

exports.findPackagesClassNameJavaValid = [
`
class SomeExampleKotlinPackage implements ReactPackage {
}
`,
`
class SomeExampleKotlinPackage implements SomePackage, ReactPackage {
}
`,
`
class SomeExampleKotlinPackage extends SomeSuper implements SomePackage, ReactPackage {
}
`,
`
class SomeExampleKotlinPackage
implements
SomePackage,
ReactPackage {
}
`,
];

exports.findPackagesClassNameJavaNotValid = [
`
class SomeExampleKotlinPackage implements SomePackage {
}
`,
`
class ReactPackage {
}
`,
`
class SomeExampleKotlinPackage extends ReactPackage {
}
`,
`
class SomeExampleKotlinPackage {
}
`,
];
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

import mocks from '../__fixtures__/android';
import findPackageClassName from '../findPackageClassName';
import findPackageClassName, {matchClassName} from '../findPackageClassName';

jest.mock('path');
jest.mock('fs');
Expand Down Expand Up @@ -55,3 +55,27 @@ const fs = require('fs');
});
});
});

describe('android:FindPackageClassNameRegex', () => {
[
mocks.findPackagesClassNameKotlinValid,
mocks.findPackagesClassNameJavaValid,
].forEach(files => {
it('returns the name of the kotlin/java class implementing ReactPackage', () => {
files.forEach(file => {
expect(matchClassName(file)[1]).toBe('SomeExampleKotlinPackage');
});
});
});

[
mocks.findPackagesClassNameKotlinNotValid,
mocks.findPackagesClassNameJavaNotValid,
].forEach(files => {
it('returns `null` if there are no matches for kotlin/java classes', () => {
files.forEach(file => {
expect(matchClassName(file)).toBeNull();
});
});
});
});
17 changes: 12 additions & 5 deletions packages/platform-android/src/config/findPackageClassName.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@ export default function getPackageClassName(folder) {

const packages = files
.map(filePath => fs.readFileSync(path.join(folder, filePath), 'utf8'))
.map(file =>
file.match(
/class\s+(.+[^\s])(\s+implements\s+|\s*:)[\s,\w]*[^{]*ReactPackage/,
),
)
.map(matchClassName)
.filter(match => match);

return packages.length ? packages[0][1] : null;
}

/**
* Match function that is looking for package's class name in file
*
* @param {String} file Content of file to match
*/
export function matchClassName(file) {
return file.match(
/class\s+(\w+[^(\s]*)[\s\w():]*(\s+implements\s+|:)[\s\w():,]*[^{]*ReactPackage/,
);
}

0 comments on commit eac98c0

Please sign in to comment.