Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions lib/rules/split-platform-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ module.exports = function (context) {
const androidMessage = 'Android components should be placed in android files';
const iosMessage = 'IOS components should be placed in ios files';
const conflictMessage = 'IOS and Android components can\'t be mixed';
const iosPathRegex = context.options[0] && context.options[0].iosPathRegex
? new RegExp(context.options[0].iosPathRegex)
: /\.ios\.js$/;
const androidPathRegex = context.options[0] && context.options[0].androidPathRegex
? new RegExp(context.options[0].androidPathRegex)
: /\.android\.js$/;

function getName(node) {
if (node.type === 'Property') {
Expand All @@ -37,11 +43,11 @@ module.exports = function (context) {
components.forEach((node) => {
const propName = getName(node);

if (propName.includes('IOS') && !filename.endsWith('.ios.js')) {
if (propName.includes('IOS') && !filename.match(iosPathRegex)) {
context.report(node, containsAndroidAndIOS ? conflictMessage : iosMessage);
}

if (propName.includes('Android') && !filename.endsWith('.android.js')) {
if (propName.includes('Android') && !filename.match(androidPathRegex)) {
context.report(node, containsAndroidAndIOS ? conflictMessage : androidMessage);
}
});
Expand Down Expand Up @@ -71,4 +77,15 @@ module.exports = function (context) {
};
};

module.exports.schema = [];
module.exports.schema = [{
type: 'object',
properties: {
androidPathRegex: {
type: 'string',
},
iosPathRegex: {
type: 'string',
},
},
additionalProperties: false,
}];
42 changes: 42 additions & 0 deletions tests/lib/rules/split-platform-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,48 @@ ruleTester.run('split-platform-components', rule, {
classes: true,
jsx: true,
},
}, {
code: [
'const React = require(\'react-native\');',
'const {',
' ActivityIndicatiorIOS,',
'} = React',
'const Hello = React.createClass({',
' render: function() {',
' return <ActivityIndicatiorIOS />;',
' }',
'});',
].join('\n'),
options: [{
iosPathRegex: '\\.ios(\\.test)?\\.js$',
}],
filename: 'Hello.ios.test.js',
parser: 'babel-eslint',
ecmaFeatures: {
classes: true,
jsx: true,
},
}, {
code: [
'const React = require(\'react-native\');',
'const {',
' ProgressBarAndroid,',
'} = React',
'const Hello = React.createClass({',
' render: function() {',
' return <ProgressBarAndroid />;',
' }',
'});',
].join('\n'),
parser: 'babel-eslint',
options: [{
androidPathRegex: '\\.android(\\.test)?\\.js$',
}],
filename: 'Hello.android.test.js',
ecmaFeatures: {
classes: true,
jsx: true,
},
}],

invalid: [{
Expand Down