Skip to content
Open
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
39 changes: 33 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

'use strict';

const { globals } = require('eslint-plugin-react-native-globals').environments.all;
const pkg = require('./package.json');

const allRules = {
'no-unused-styles': require('./lib/rules/no-unused-styles'),
'no-inline-styles': require('./lib/rules/no-inline-styles'),
Expand All @@ -14,20 +17,27 @@ const allRules = {

function configureAsError(rules) {
const result = {};
for (const key in rules) {
if (!rules.hasOwnProperty(key)) {
continue;
Object.keys(rules).forEach((key) => {
if (!Object.prototype.hasOwnProperty.call(rules, key)) {
return;
}
result['react-native/' + key] = 2;
}
});
return result;
}

const meta = {
name: pkg.name,
version: pkg.version,
};

const plugin = { meta, rules: allRules };

const allRulesConfig = configureAsError(allRules);

module.exports = {
...plugin,
deprecatedRules: {},
rules: allRules,
rulesConfig: {
'no-unused-styles': 0,
'no-inline-styles': 0,
Expand All @@ -38,11 +48,13 @@ module.exports = {
'no-single-element-style-arrays': 0
},
environments: {
// Kept for ESLint 8/9 legacy (.eslintrc) users; ignored by ESLint v10
'react-native': {
globals: require('eslint-plugin-react-native-globals').environments.all.globals,
globals: globals,
},
},
configs: {
// Legacy format (ESLint 8/9 with .eslintrc)
all: {
plugins: [
'react-native',
Expand All @@ -54,5 +66,20 @@ module.exports = {
},
rules: allRulesConfig,
},
// Flat config format (ESLint 9+ / 10+ with eslint.config.js)
'flat/all': {
plugins: {
'react-native': plugin
},
languageOptions: {
globals: globals,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
rules: allRulesConfig,
},
},
};
3 changes: 2 additions & 1 deletion lib/rules/no-single-element-style-arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ module.exports = {
'Single element style arrays are not necessary and cause unnecessary re-renders',
fix(fixer) {
const realStyleNode = JSXExpressionNode.value.expression.elements[0];
const styleSource = context.getSourceCode().getText(realStyleNode);
const sc = context.sourceCode || context.getSourceCode();
const styleSource = sc.getText(realStyleNode);
return fixer.replaceText(JSXExpressionNode.value.expression, styleSource);
},
});
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-unused-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const create = Components.detect((context, components) => {
node.key.name,
].join('');

context.report(node, message);
context.report({ node, message });
});
}
});
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/sort-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function create(context) {
const { ignoreStyleProperties } = options;
const isValidOrder = order === 'asc' ? (a, b) => a <= b : (a, b) => a >= b;

const sourceCode = context.getSourceCode();
const sourceCode = context.sourceCode || context.getSourceCode();

function sort(array) {
return [].concat(array).sort((a, b) => {
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/split-platform-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ function create(context) {
const propName = getName(node);

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

if (propName.includes('Android') && !filename.match(androidPathRegex)) {
context.report(node, containsAndroidAndIOS ? conflictMessage : androidMessage);
context.report({ node, message: containsAndroidAndIOS ? conflictMessage : androidMessage });
}
});
}
Expand All @@ -71,7 +71,7 @@ function create(context) {
}
},
'Program:exit': function () {
const filename = context.getFilename();
const filename = context.filename || context.getFilename();
reportErrors(reactComponents, filename);
},
};
Expand Down
2 changes: 1 addition & 1 deletion lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Components.prototype.length = function () {
};

function componentRule(rule, context, _node) {
const sourceCode = context.getSourceCode();
const sourceCode = context.sourceCode || context.getSourceCode();
const components = new Components();

// Utilities for component detection
Expand Down
3 changes: 1 addition & 2 deletions lib/util/stylesheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ StyleSheets.prototype.getObjectExpressions = function () {
};

let currentContent;
const getSourceCode = (node) => currentContent
.getSourceCode(node)
const getSourceCode = (node) => (currentContent.sourceCode || currentContent.getSourceCode())
.getText(node);

const getStyleSheetObjectNames = (settings) => settings['react-native/style-sheet-object-names'] || ['StyleSheet'];
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"typescript": "^5.2.2"
},
"peerDependencies": {
"eslint": "^3.17.0 || ^4 || ^5 || ^6 || ^7 || ^8 || ^9"
"eslint": "^3.17.0 || ^4 || ^5 || ^6 || ^7 || ^8 || ^9 || ^10"
},
"keywords": [
"eslint",
Expand Down
16 changes: 16 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,20 @@ describe('configurations', () => {
assert(inDeprecatedRules ^ inAllConfig); // eslint-disable-line no-bitwise
});
});

it('should export a \'flat/all\' configuration', () => {
const flatAll = plugin.configs['flat/all'];
assert(flatAll);
assert(flatAll.plugins && flatAll.plugins['react-native']);
assert(flatAll.languageOptions);
Object.keys(flatAll.rules).forEach((configName) => {
assert.equal(configName.indexOf('react-native/'), 0);
assert.equal(flatAll.rules[configName], 2);
});
rules.forEach((ruleName) => {
const inDeprecatedRules = Boolean(plugin.deprecatedRules[ruleName]);
const inFlatAllConfig = Boolean(flatAll.rules['react-native/' + ruleName]);
assert(inDeprecatedRules ^ inFlatAllConfig); // eslint-disable-line no-bitwise
});
});
});
20 changes: 1 addition & 19 deletions tests/lib/rules/sort-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,25 +423,7 @@ const tests = {
// comments 3
})
`,
output: `
const styles = StyleSheet.create({
a: {
d: 4,
// comments 1
c: 3,
a: 1,
b: 2,
},
d: {},
c: {},
// comments 2
b: {
a: 1,
b: 2,
},
// comments 3
})
`,
output: null,
errors: [
{
message:
Expand Down