瀏覽代碼

Add prettier to the project

Mathieu Acthernoene 8 年之前
父節點
當前提交
2895fa2ade
共有 9 個文件被更改,包括 223 次插入190 次删除
  1. 3 1
      .prettierrc
  2. 47 40
      Example/Example.js
  3. 6 8
      Example/__tests__/index.android.js
  4. 6 8
      Example/__tests__/index.ios.js
  5. 3 5
      Example/index.android.js
  6. 3 5
      Example/index.ios.js
  7. 82 73
      index.android.js
  8. 63 50
      index.ios.js
  9. 10 0
      package.json

+ 3 - 1
.prettierrc

@@ -1,3 +1,5 @@
 {
-  "singleQuote": true
+  "singleQuote": true,
+  "trailingComma": "all",
+  "semi": false
 }

+ 47 - 40
Example/Example.js

@@ -4,7 +4,7 @@
  * @flow
  */
 
-import React, { Component } from 'react';
+import React, { Component } from 'react'
 import {
   StyleSheet,
   TouchableHighlight,
@@ -13,7 +13,7 @@ import {
   Alert,
   AppState,
   Platform,
-} from 'react-native';
+} from 'react-native'
 
 import Permissions from 'react-native-permissions'
 
@@ -29,11 +29,14 @@ export default class Example extends Component {
 
     this.setState({ types, canOpenSettings })
     this._updatePermissions(types)
-    AppState.addEventListener('change', this._handleAppStateChange.bind(this));
+    AppState.addEventListener('change', this._handleAppStateChange.bind(this))
   }
 
   componentWillUnmount() {
-    AppState.removeEventListener('change', this._handleAppStateChange.bind(this));
+    AppState.removeEventListener(
+      'change',
+      this._handleAppStateChange.bind(this),
+    )
   }
 
   //update permissions when app comes back from settings
@@ -44,16 +47,17 @@ export default class Example extends Component {
   }
 
   _openSettings() {
-    return Permissions.openSettings()
-      .then(() => alert('back to app!!'))
+    return Permissions.openSettings().then(() => alert('back to app!!'))
   }
 
   _updatePermissions(types) {
     Permissions.checkMultiple(types)
       .then(status => {
         if (this.state.isAlways) {
-          return Permissions.check('location', 'always')
-            .then(location => ({...status, location}))
+          return Permissions.check('location', 'always').then(location => ({
+            ...status,
+            location,
+          }))
         }
         return status
       })
@@ -70,19 +74,24 @@ export default class Example extends Component {
     Permissions.request(permission, options)
       .then(res => {
         this.setState({
-          status: {...this.state.status, [permission]: res}
+          status: { ...this.state.status, [permission]: res },
         })
         if (res != 'authorized') {
           var buttons = [{ text: 'Cancel', style: 'cancel' }]
-          if (this.state.canOpenSettings) buttons.push({ text: 'Open Settings', onPress: this._openSettings.bind(this) })
-          
+          if (this.state.canOpenSettings)
+            buttons.push({
+              text: 'Open Settings',
+              onPress: this._openSettings.bind(this),
+            })
+
           Alert.alert(
             'Whoops!',
-            "There was a problem getting your permission. Please enable it from settings.",
-            buttons
+            'There was a problem getting your permission. Please enable it from settings.',
+            buttons,
           )
         }
-      }).catch(e => console.warn(e))
+      })
+      .catch(e => console.warn(e))
   }
 
   _onLocationSwitchChange() {
@@ -93,46 +102,44 @@ export default class Example extends Component {
   render() {
     return (
       <View style={styles.container}>
-
         {this.state.types.map(p => (
-          <TouchableHighlight 
+          <TouchableHighlight
             style={[styles.button, styles[this.state.status[p]]]}
             key={p}
-            onPress={this._requestPermission.bind(this, p)}>
+            onPress={this._requestPermission.bind(this, p)}
+          >
             <View>
               <Text style={styles.text}>
-                {Platform.OS == 'ios' && p == 'location' ? `location ${this.state.isAlways ? 'always' : 'whenInUse'}` : p}
-              </Text>
-              <Text style={styles.subtext}>
-                {this.state.status[p]}
+                {Platform.OS == 'ios' && p == 'location'
+                  ? `location ${this.state.isAlways ? 'always' : 'whenInUse'}`
+                  : p}
               </Text>
+              <Text style={styles.subtext}>{this.state.status[p]}</Text>
             </View>
           </TouchableHighlight>
-          )
-        )}
+        ))}
         <View style={styles.footer}>
-          <TouchableHighlight 
-            style={styles['footer_'+Platform.OS]}
-            onPress={this._onLocationSwitchChange.bind(this)}>
+          <TouchableHighlight
+            style={styles['footer_' + Platform.OS]}
+            onPress={this._onLocationSwitchChange.bind(this)}
+          >
             <Text style={styles.text}>Toggle location type</Text>
           </TouchableHighlight>
-   
-          {
-            this.state.canOpenSettings &&
-            <TouchableHighlight 
-              onPress={this._openSettings.bind(this)}>
+
+          {this.state.canOpenSettings && (
+            <TouchableHighlight onPress={this._openSettings.bind(this)}>
               <Text style={styles.text}>Open settings</Text>
             </TouchableHighlight>
-          }
-
+          )}
         </View>
 
-
-        <Text style={styles['footer_'+Platform.OS]}>
-          Note: microphone permissions may not work on iOS simulator. Also, toggling permissions from the settings menu may cause the app to crash. This is normal on iOS. Google "ios crash permission change"
+        <Text style={styles['footer_' + Platform.OS]}>
+          Note: microphone permissions may not work on iOS simulator. Also,
+          toggling permissions from the settings menu may cause the app to
+          crash. This is normal on iOS. Google "ios crash permission change"
         </Text>
       </View>
-    );
+    )
   }
 }
 
@@ -169,7 +176,7 @@ const styles = StyleSheet.create({
     backgroundColor: '#ef9a9a',
   },
   restricted: {
-    backgroundColor: '#ef9a9a'
+    backgroundColor: '#ef9a9a',
   },
   footer: {
     padding: 10,
@@ -179,5 +186,5 @@ const styles = StyleSheet.create({
   footer_android: {
     height: 0,
     width: 0,
-  }
-})
+  },
+})

+ 6 - 8
Example/__tests__/index.android.js

@@ -1,12 +1,10 @@
-import 'react-native';
-import React from 'react';
-import Index from '../index.android.js';
+import 'react-native'
+import React from 'react'
+import Index from '../index.android.js'
 
 // Note: test renderer must be required after react-native.
-import renderer from 'react-test-renderer';
+import renderer from 'react-test-renderer'
 
 it('renders correctly', () => {
-  const tree = renderer.create(
-    <Index />
-  );
-});
+  const tree = renderer.create(<Index />)
+})

+ 6 - 8
Example/__tests__/index.ios.js

@@ -1,12 +1,10 @@
-import 'react-native';
-import React from 'react';
-import Index from '../index.ios.js';
+import 'react-native'
+import React from 'react'
+import Index from '../index.ios.js'
 
 // Note: test renderer must be required after react-native.
-import renderer from 'react-test-renderer';
+import renderer from 'react-test-renderer'
 
 it('renders correctly', () => {
-  const tree = renderer.create(
-    <Index />
-  );
-});
+  const tree = renderer.create(<Index />)
+})

+ 3 - 5
Example/index.android.js

@@ -4,11 +4,9 @@
  * @flow
  */
 
-import React, { Component } from 'react';
-import {
-  AppRegistry,
-} from 'react-native';
+import React, { Component } from 'react'
+import { AppRegistry } from 'react-native'
 
 import Example from './Example'
 
-AppRegistry.registerComponent('Example', () => Example);
+AppRegistry.registerComponent('Example', () => Example)

+ 3 - 5
Example/index.ios.js

@@ -4,11 +4,9 @@
  * @flow
  */
 
-import React, { Component } from 'react';
-import {
-  AppRegistry,
-} from 'react-native';
+import React, { Component } from 'react'
+import { AppRegistry } from 'react-native'
 
 import Example from './Example'
 
-AppRegistry.registerComponent('Example', () => Example);
+AppRegistry.registerComponent('Example', () => Example)

+ 82 - 73
index.android.js

@@ -1,91 +1,100 @@
-'use strict';
+'use strict'
 
 const ReactNative = require('react-native')
-const RNPermissions = ReactNative.PermissionsAndroid;
+const RNPermissions = ReactNative.PermissionsAndroid
 const AsyncStorage = ReactNative.AsyncStorage
 
 const RNPTypes = {
-	location: RNPermissions.PERMISSIONS.ACCESS_FINE_LOCATION,
-	camera: RNPermissions.PERMISSIONS.CAMERA,
-	microphone: RNPermissions.PERMISSIONS.RECORD_AUDIO,
-	contacts: RNPermissions.PERMISSIONS.READ_CONTACTS,
-	event: RNPermissions.PERMISSIONS.READ_CALENDAR,
-	storage: RNPermissions.PERMISSIONS.READ_EXTERNAL_STORAGE,
-	photo: RNPermissions.PERMISSIONS.READ_EXTERNAL_STORAGE,
-	callPhone: RNPermissions.PERMISSIONS.CALL_PHONE,
-	readSms: RNPermissions.PERMISSIONS.READ_SMS,
-	receiveSms: RNPermissions.PERMISSIONS.RECEIVE_SMS,
+  location: RNPermissions.PERMISSIONS.ACCESS_FINE_LOCATION,
+  camera: RNPermissions.PERMISSIONS.CAMERA,
+  microphone: RNPermissions.PERMISSIONS.RECORD_AUDIO,
+  contacts: RNPermissions.PERMISSIONS.READ_CONTACTS,
+  event: RNPermissions.PERMISSIONS.READ_CALENDAR,
+  storage: RNPermissions.PERMISSIONS.READ_EXTERNAL_STORAGE,
+  photo: RNPermissions.PERMISSIONS.READ_EXTERNAL_STORAGE,
+  callPhone: RNPermissions.PERMISSIONS.CALL_PHONE,
+  readSms: RNPermissions.PERMISSIONS.READ_SMS,
+  receiveSms: RNPermissions.PERMISSIONS.RECEIVE_SMS,
 }
 
 const RESULTS = {
-	[ RNPermissions.RESULTS.GRANTED ]: 'authorized',
-	[ RNPermissions.RESULTS.DENIED ]: 'denied',
-	[ RNPermissions.RESULTS.NEVER_ASK_AGAIN ]: 'restricted',
+  [RNPermissions.RESULTS.GRANTED]: 'authorized',
+  [RNPermissions.RESULTS.DENIED]: 'denied',
+  [RNPermissions.RESULTS.NEVER_ASK_AGAIN]: 'restricted',
 }
 
 const STORAGE_KEY = '@RNPermissions:didAskPermission:'
 
 const setDidAskOnce = p => AsyncStorage.setItem(STORAGE_KEY + p, 'true')
-const getDidAskOnce = p =>  AsyncStorage.getItem(STORAGE_KEY + p).then(res => !!res)
+const getDidAskOnce = p =>
+  AsyncStorage.getItem(STORAGE_KEY + p).then(res => !!res)
 
 class ReactNativePermissions {
-	canOpenSettings() {
-		return false
-	}
-
-	openSettings() {
-		return Promise.reject('\'openSettings\' is Depricated on android')
-	}
-
-	getTypes() {
-		return Object.keys(RNPTypes);
-	}
-
-	check(permission) {
-		const androidPermission = RNPTypes[permission]
-  	if (!androidPermission) return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on Android`);
-		
-		const shouldShowRationale = ReactNative.NativeModules.PermissionsAndroid.shouldShowRequestPermissionRationale;
-
-		return RNPermissions.check(androidPermission)
-			.then(isAuthorized => {
-				if (isAuthorized) return 'authorized'
-
-				return getDidAskOnce(permission)
-					.then(didAsk => {
-						if (didAsk) {
-							return shouldShowRationale(androidPermission)
-								.then(shouldShow => shouldShow ? 'denied' : 'restricted')
-						}
-						return 'undetermined'
-					})
-			})
-	}
-
-
-	request(permission) {
-		const androidPermission = RNPTypes[permission]
-  	if (!androidPermission) return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on Android`);
-
-
-		return RNPermissions.request(androidPermission)
-			.then(res => {
-				// RNPermissions.request() to native module resolves to boolean
-				// rather than string if running on OS version prior to Android M 
-				if (typeof res === 'boolean') return res ? 'authorized' : 'denied';
-				return setDidAskOnce(permission)
-					.then(() => RESULTS[res])
-			});
-	}
-
-	checkMultiple(permissions) {
-		return Promise.all(permissions.map(this.check.bind(this)))
-			.then(res => res.reduce((pre, cur, i) => {
-				var name = permissions[i]
-				pre[name] = cur
-				return pre
-			}, {}))
-	}
+  canOpenSettings() {
+    return false
+  }
+
+  openSettings() {
+    return Promise.reject("'openSettings' is Depricated on android")
+  }
+
+  getTypes() {
+    return Object.keys(RNPTypes)
+  }
+
+  check(permission) {
+    const androidPermission = RNPTypes[permission]
+    if (!androidPermission)
+      return Promise.reject(
+        `ReactNativePermissions: ${
+          permission
+        } is not a valid permission type on Android`,
+      )
+
+    const shouldShowRationale =
+      ReactNative.NativeModules.PermissionsAndroid
+        .shouldShowRequestPermissionRationale
+
+    return RNPermissions.check(androidPermission).then(isAuthorized => {
+      if (isAuthorized) return 'authorized'
+
+      return getDidAskOnce(permission).then(didAsk => {
+        if (didAsk) {
+          return shouldShowRationale(androidPermission).then(
+            shouldShow => (shouldShow ? 'denied' : 'restricted'),
+          )
+        }
+        return 'undetermined'
+      })
+    })
+  }
+
+  request(permission) {
+    const androidPermission = RNPTypes[permission]
+    if (!androidPermission)
+      return Promise.reject(
+        `ReactNativePermissions: ${
+          permission
+        } is not a valid permission type on Android`,
+      )
+
+    return RNPermissions.request(androidPermission).then(res => {
+      // RNPermissions.request() to native module resolves to boolean
+      // rather than string if running on OS version prior to Android M
+      if (typeof res === 'boolean') return res ? 'authorized' : 'denied'
+      return setDidAskOnce(permission).then(() => RESULTS[res])
+    })
+  }
+
+  checkMultiple(permissions) {
+    return Promise.all(permissions.map(this.check.bind(this))).then(res =>
+      res.reduce((pre, cur, i) => {
+        var name = permissions[i]
+        pre[name] = cur
+        return pre
+      }, {}),
+    )
+  }
 }
 
 module.exports = new ReactNativePermissions()

+ 63 - 50
index.ios.js

@@ -1,70 +1,83 @@
-'use strict';
+'use strict'
 
-const ReactNative = require('react-native');
-const RNPermissions = ReactNative.NativeModules.ReactNativePermissions;
+const ReactNative = require('react-native')
+const RNPermissions = ReactNative.NativeModules.ReactNativePermissions
 
 const RNPTypes = [
-	'location',
-	'camera',
-	'microphone',
-	'photo',
-	'contacts',
-	'event',
-	'reminder',
-	'bluetooth',
-	'notification',
-	'backgroundRefresh',
-	'speechRecognition',
+  'location',
+  'camera',
+  'microphone',
+  'photo',
+  'contacts',
+  'event',
+  'reminder',
+  'bluetooth',
+  'notification',
+  'backgroundRefresh',
+  'speechRecognition',
 ]
 
 const DEFAULTS = {
-	'location' : 'whenInUse',
-	'notification': ['alert', 'badge', 'sound'],
+  location: 'whenInUse',
+  notification: ['alert', 'badge', 'sound'],
 }
 
 class ReactNativePermissions {
-	canOpenSettings() {
-		return RNPermissions.canOpenSettings()
-	}
+  canOpenSettings() {
+    return RNPermissions.canOpenSettings()
+  }
 
-	openSettings() {
-		return RNPermissions.openSettings()
-	}
+  openSettings() {
+    return RNPermissions.openSettings()
+  }
 
-	getTypes() {
-		return RNPTypes;
-	}
+  getTypes() {
+    return RNPTypes
+  }
 
-	check(permission, type) {
-  	if (!RNPTypes.includes(permission)) {
-			return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on iOS`);
-		}
-		
-		return RNPermissions.getPermissionStatus(permission, type);
-	}
+  check(permission, type) {
+    if (!RNPTypes.includes(permission)) {
+      return Promise.reject(
+        `ReactNativePermissions: ${
+          permission
+        } is not a valid permission type on iOS`,
+      )
+    }
 
-	request(permission, type) {
-		if (!RNPTypes.includes(permission)) {
-			return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on iOS`);
-		}
+    return RNPermissions.getPermissionStatus(permission, type)
+  }
 
-		if (permission == 'backgroundRefresh') {
-			return Promise.reject('ReactNativePermissions: You cannot request backgroundRefresh')
-		}
+  request(permission, type) {
+    if (!RNPTypes.includes(permission)) {
+      return Promise.reject(
+        `ReactNativePermissions: ${
+          permission
+        } is not a valid permission type on iOS`,
+      )
+    }
 
-		type = type || DEFAULTS[permission]
+    if (permission == 'backgroundRefresh') {
+      return Promise.reject(
+        'ReactNativePermissions: You cannot request backgroundRefresh',
+      )
+    }
 
-		return RNPermissions.requestPermission(permission, type)
-	}
+    type = type || DEFAULTS[permission]
 
-	checkMultiple(permissions) {
-		return Promise.all(permissions.map(permission => this.check(permission)))
-			.then(res => res.reduce((pre, cur, i) => {
-				var name = permissions[i]
-				pre[name] = cur
-				return pre
-			}, {}))
-	}
+    return RNPermissions.requestPermission(permission, type)
+  }
+
+  checkMultiple(permissions) {
+    return Promise.all(
+      permissions.map(permission => this.check(permission)),
+    ).then(res =>
+      res.reduce((pre, cur, i) => {
+        var name = permissions[i]
+        pre[name] = cur
+        return pre
+      }, {}),
+    )
+  }
 }
 
 module.exports = new ReactNativePermissions()

+ 10 - 0
package.json

@@ -15,6 +15,16 @@
     "type": "git",
     "url": "https://github.com/yonahforst/react-native-permissions.git"
   },
+  "scripts": {
+    "precommit": "lint-staged",
+    "prettier": "prettier --write '**/*.js'"
+  },
+  "lint-staged": {
+    "**/*.js": [
+      "prettier --write",
+      "git add"
+    ]
+  },
   "devDependencies": {
     "husky": "^0.14.3",
     "lint-staged": "^5.0.0",