Skip to content

Commit 7b4b8a2

Browse files
Inbal-Tishethanshar
authored andcommitted
Eslint rules (#69)
* create initial eslint rule * Adding lint rule no-hard-coded-font (size); fixing lint warnings of hard-coded font size * adding test to no-hard-coded-font rule * adding .eslintignore file; fixing hard-coded colors to use Colors * preparing for npm upload
1 parent f16d577 commit 7b4b8a2

File tree

18 files changed

+226
-18
lines changed

18 files changed

+226
-18
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/src/style/typography.js

.eslintrc

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
{
22
"parser": "babel-eslint",
3-
"plugins": ["react-native"],
3+
"plugins": ["react-native", "uilib"],
44
"extends": ["airbnb"],
55
"rules": {
6-
"object-curly-spacing": "off",
7-
"react/prefer-stateless-function": "off",
8-
"react/jsx-filename-extension": "off",
9-
"react/require-default-props": "off",
6+
"arrow-body-style": "off",
7+
"class-methods-use-this": "off",
8+
"consistent-return": "off",
9+
"global-require": "off",
10+
"import/prefer-default-export": "off",
11+
"no-plusplus": "off",
12+
"no-return-assign": "off",
1013
"no-use-before-define": "off",
14+
"max-len": [2, 120, 4, {"ignoreUrls": true}],
15+
"object-curly-spacing": "off",
1116
"react/forbid-prop-types": "off",
17+
"react/jsx-filename-extension": "off",
1218
"react/jsx-space-before-closing": "off",
1319
"react/jsx-tag-spacing": "off",
14-
"max-len": [2, 120, 4, {"ignoreUrls": true}],
15-
"class-methods-use-this": "off",
16-
"arrow-body-style": "off",
17-
"no-plusplus": "off",
18-
"import/prefer-default-export": "off",
19-
"global-require": "off",
20-
"consistent-return": "off",
21-
"no-return-assign": "off"
20+
"react/prefer-stateless-function": "off",
21+
"react/require-default-props": "off",
22+
"uilib/no-hard-coded-font": "warn"
2223
},
2324
"env": {
2425
"browser": true,

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ android/
232232
typings/
233233
demo/
234234
uilib-docs/
235+
eslint-rules/
235236
scripts/
236237
demo-app.component.js
237238
index.android.js

eslint-rules/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# eslint-plugin-uilib
2+
3+
uilib set of eslint rules
4+
5+
## Installation
6+
7+
You'll first need to install [ESLint](http://eslint.org):
8+
9+
```
10+
$ npm i eslint --save-dev
11+
```
12+
13+
Next, install `eslint-plugin-uilib`:
14+
15+
```
16+
$ npm install eslint-plugin-uilib --save-dev
17+
```
18+
19+
**Note:** If you installed ESLint globally (using the `-g` flag) then you must also install `eslint-plugin-uilib` globally.
20+
21+
## Usage
22+
23+
Add `uilib` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
24+
25+
```json
26+
{
27+
"plugins": [
28+
"uilib"
29+
]
30+
}
31+
```
32+
33+
34+
Then configure the rules you want to use under the rules section.
35+
36+
```json
37+
{
38+
"rules": {
39+
"uilib/rule-name": 2
40+
}
41+
}
42+
```
43+
44+
## Supported Rules
45+
46+
* Fill in provided rules here
47+
48+
49+
50+
51+

eslint-rules/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
'no-hard-coded-font': require('./lib/rules/no-hard-coded-font'),
4+
},
5+
};

eslint-rules/lib/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @fileoverview uilib set of eslint rules
3+
* @author Ethan Sharabi
4+
*/
5+
6+
'use strict';
7+
8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
12+
const requireIndex = require('requireindex');
13+
14+
//------------------------------------------------------------------------------
15+
// Plugin Definition
16+
//------------------------------------------------------------------------------
17+
18+
// import all rules in lib/rules
19+
module.exports.rules = requireIndex(__dirname + "/rules");

eslint-rules/lib/rules/no-hard-coded-color.js

Whitespace-only changes.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @fileoverview Rule to disallow hard coded font style
3+
* @author Inbal Tish
4+
*/
5+
6+
'use strict';
7+
8+
//------------------------------------------------------------------------------
9+
// Rule Definition
10+
//------------------------------------------------------------------------------
11+
12+
module.exports = {
13+
meta: {
14+
docs: {
15+
description: 'disallow hard coded font style',
16+
category: 'Possible Errors',
17+
recommended: true,
18+
},
19+
messages: {
20+
reportMessage: 'Please do not use hard coded fontSize prop in style objects, instead use Typography presets',
21+
},
22+
fixable: 'code',
23+
schema: [], // no options
24+
},
25+
create(context) {
26+
return {
27+
ObjectExpression: function (node) {
28+
node.properties.forEach((property) => {
29+
if (property.key) {
30+
const propName = property.key.name;
31+
if (propName === 'fontSize') {
32+
if (property.value.type === 'CallExpression') {
33+
return;
34+
}
35+
if (property.value.type === 'MemberExpression') {
36+
const objectName = property.value.object.object.name;
37+
if (objectName.toLowerCase() === 'typography') {
38+
return;
39+
}
40+
}
41+
// console.log(`${property.value.value} should be fixed!`);
42+
context.report({
43+
node,
44+
messageId: 'reportMessage',
45+
});
46+
}
47+
}
48+
});
49+
},
50+
};
51+
},
52+
};

eslint-rules/package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "eslint-plugin-uilib",
3+
"version": "1.0.0",
4+
"description": "uilib set of eslint rules",
5+
"keywords": [
6+
"eslint",
7+
"eslintplugin",
8+
"eslint-plugin"
9+
],
10+
"author": "Tnbal Tish <[email protected]>",
11+
"main": "lib/index.js",
12+
"scripts": {
13+
"test": "mocha tests --recursive"
14+
},
15+
"dependencies": {
16+
"requireindex": "~1.1.0"
17+
},
18+
"devDependencies": {
19+
"eslint": "~3.9.1",
20+
"mocha": "^3.1.2"
21+
},
22+
"engines": {
23+
"node": ">=0.10.0"
24+
},
25+
"license": "ISC"
26+
}

eslint-rules/tests/lib/rules/no-hard-coded-color.js

Whitespace-only changes.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const rule = require('../../../lib/rules/no-hard-coded-font');
2+
const RuleTester = require('eslint').RuleTester;
3+
4+
RuleTester.setDefaultConfig({
5+
parserOptions: {
6+
ecmaVersion: 6,
7+
ecmaFeatures: {
8+
jsx: true,
9+
experimentalObjectRestSpread: true,
10+
},
11+
},
12+
});
13+
14+
const ruleTester = new RuleTester();
15+
16+
ruleTester.run('no-hard-coded-font', rule, {
17+
valid: [
18+
{ code: 'const goodUsage = <Text style={{color: \'red\'}}/>;' },
19+
{ code: 'const goodUsage = <Text style={{fontSize: Typography.text10.fontSize}}/>;' },
20+
],
21+
invalid: [
22+
{
23+
code: 'const badUsage = <Text style={{fontSize: 15}}/>;',
24+
errors: [
25+
{
26+
messageId: 'foo',
27+
},
28+
],
29+
},
30+
{
31+
code: 'const badUsage = <Text style={{fontSize: Constants.isAndroid ? 16 : 17}}/>;',
32+
errors: [
33+
{
34+
messageId: 'foo',
35+
},
36+
],
37+
},
38+
{
39+
code: 'const badUsage = <Text style={{fontSize: size}}/>;',
40+
errors: [
41+
{
42+
messageId: 'foo',
43+
},
44+
],
45+
},
46+
],
47+
});

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"test": "jest",
1919
"test:watch": "jest --watch",
2020
"lint": "eslint src -c .eslintrc",
21+
"lint:test": "mocha eslint-rules/tests/lib/rules",
2122
"xcode": "open ./ios/uilib.xcodeproj",
2223
"build": "mkdir -p dist && cp -r src/assets dist/ && babel src --out-dir dist",
2324
"log": "react-native log-ios | grep 'ethan -'",
@@ -48,9 +49,11 @@
4849
"eslint-plugin-jsx-a11y": "4.0.0",
4950
"eslint-plugin-react": "^6.9.0",
5051
"eslint-plugin-react-native": "^2.0.0",
52+
"eslint-plugin-uilib": "file:./eslint-rules",
5153
"gatsby": "^1.9.128",
5254
"gh-pages": "^1.1.0",
5355
"jest": "^20.0.4",
56+
"mocha": "^5.0.0",
5457
"react": "16.0.0",
5558
"react-addons-test-utils": "^15.4.2",
5659
"react-autobind": "^1.0.6",

src/commons/__tests__/BaseComponent.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('BaseComponent', () => {
5252
});
5353

5454
it('should return value of the custom made typography', () => {
55-
const customTypography = {fontSize: 34, fontWeight: '400'};
55+
const customTypography = {fontSize: Typography.text30.fontSize, fontWeight: '400'};
5656
Typography.loadTypographies({customTypography});
5757
expect(new BaseComponent({customTypography: true}).extractTypographyValue()).toEqual(customTypography);
5858
expect(

src/components/avatar/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,13 @@ function createStyles({size, labelColor, imageSource}) {
186186
bottom: 1,
187187
left: 1,
188188
},
189+
/*eslint-disable*/
189190
initials: {
190191
fontSize: size * fontSizeToImageSizeRatio,
191192
color: labelColor,
192193
backgroundColor: 'rgba(0,0,0,0)',
193194
},
195+
/*eslint-enable*/
194196
defaultImage: {
195197
width: size,
196198
height: size,

src/components/button/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ function createStyles() {
414414
backgroundColor: undefined,
415415
},
416416
shadowStyle: {
417-
shadowColor: '#3082C8',
417+
shadowColor: Colors.blue10,
418418
shadowOffset: {height: 5, width: 0},
419419
shadowOpacity: 0.35,
420420
shadowRadius: 9.5,

src/components/carousel/Carousel2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export default class Carousel2 extends BaseComponent {
131131
// const titleStyle = {
132132
// color: this.deltaX.interpolate({
133133
// inputRange,
134-
// outputRange: this.generateOutputRange(pageIndex, ['#C2C7CB', '#20303C']),
134+
// outputRange: this.generateOutputRange(pageIndex, [Colors.dark60, Colors.dark10]),
135135
// }),
136136
// transform: [{
137137
// scale: this.deltaX.interpolate({

src/components/connectionStatusBar/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function createStyles() {
153153
alignSelf: 'center',
154154
},
155155
x: {
156-
fontSize: 15,
156+
fontSize: Typography.text80.fontSize,
157157
color: 'black',
158158
},
159159
});

src/components/stepper/style.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default function createStyles() {
4242
borderColor: separatorColor,
4343
},
4444
buttonText: {
45-
fontSize: 30,
45+
...Typography.text40,
4646
fontWeight: '200',
4747
color: ThemeManager.primaryColor,
4848
backgroundColor: 'transparent',

0 commit comments

Comments
 (0)