From 1a9a051ee99d5e4039e31c169ce045369f895ea1 Mon Sep 17 00:00:00 2001 From: Mikael Sand Date: Mon, 24 Jul 2017 00:08:28 +0300 Subject: [PATCH] Fix linting, warnings, add noinspection suppression comments. --- README.md | 1 + elements/Image.js | 1 + elements/Polygon.js | 1 + elements/Polyline.js | 1 + elements/Shape.js | 1 + elements/Svg.js | 5 +++- elements/TSpan.js | 5 +++- elements/Text.js | 5 +++- elements/TextPath.js | 2 +- index.js | 1 + lib/Matrix2D.js | 43 +++++++++++++++++++++------------ lib/SvgTouchableMixin.js | 1 + lib/attributes.js | 5 ++-- lib/extract/extractBrush.js | 3 +-- lib/extract/extractFill.js | 3 +-- lib/extract/extractGradient.js | 1 + lib/extract/extractProps.js | 2 +- lib/extract/extractStroke.js | 2 +- lib/extract/extractText.js | 3 ++- lib/extract/extractTransform.js | 25 ++++++------------- package.json | 4 ++- 21 files changed, 66 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 5123e968..a3089c1f 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ Here's a simple example. To render output like this: Use the following code: ```javascript +import 'react'; import Svg,{ Circle, Ellipse, diff --git a/elements/Image.js b/elements/Image.js index 4248ed0e..2b3c4b5d 100644 --- a/elements/Image.js +++ b/elements/Image.js @@ -5,6 +5,7 @@ import createReactNativeComponentClass from 'react-native/Libraries/Renderer/shi import {ImageAttributes} from '../lib/attributes'; import {numberProp, touchableProps, responderProps} from '../lib/props'; import Shape from './Shape'; +//noinspection JSUnresolvedVariable import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource'; import {meetOrSliceTypes, alignEnum} from '../lib/extract/extractViewBox'; import extractProps from '../lib/extract/extractProps'; diff --git a/elements/Polygon.js b/elements/Polygon.js index d0e18f1d..23e37025 100644 --- a/elements/Polygon.js +++ b/elements/Polygon.js @@ -16,6 +16,7 @@ export default class extends Component{ }; setNativeProps = (...args) => { + //noinspection JSUnresolvedFunction this.root.getNativeElement().setNativeProps(...args); }; diff --git a/elements/Polyline.js b/elements/Polyline.js index 5520c21d..34aacc6d 100644 --- a/elements/Polyline.js +++ b/elements/Polyline.js @@ -16,6 +16,7 @@ export default class extends Component{ }; setNativeProps = (...args) => { + //noinspection JSUnresolvedFunction this.root.getNativeElement().setNativeProps(...args); }; diff --git a/elements/Shape.js b/elements/Shape.js index f77e6d91..fbf257eb 100644 --- a/elements/Shape.js +++ b/elements/Shape.js @@ -8,6 +8,7 @@ class Shape extends Component { _.forEach(SvgTouchableMixin, (method, key) => { this[key] = method.bind(this); }); + //noinspection JSUnusedGlobalSymbols this.state = this.touchableGetInitialState(); } } diff --git a/elements/Svg.js b/elements/Svg.js index f5e08145..377dadd8 100644 --- a/elements/Svg.js +++ b/elements/Svg.js @@ -1,3 +1,4 @@ +//noinspection JSUnresolvedVariable import React, { Component } from 'react'; @@ -12,6 +13,7 @@ import { import extractViewBox from '../lib/extract/extractViewBox'; import {ViewBoxAttributes} from '../lib/attributes'; +/** @namespace NativeModules.RNSVGSvgViewManager */ const RNSVGSvgViewManager = NativeModules.RNSVGSvgViewManager; // Svg - Root node of all Svg elements @@ -43,6 +45,7 @@ class Svg extends Component{ super(...arguments); id++; this.id = id; + //noinspection JSUnusedGlobalSymbols this.onDataURLCallbacks = []; } measureInWindow = (...args) => { @@ -61,7 +64,7 @@ class Svg extends Component{ this.root.setNativeProps(...args); }; - toDataURL = (callback: Function) => { + toDataURL = (callback) => { callback && RNSVGSvgViewManager.toDataURL(findNodeHandle(this.root), callback); }; diff --git a/elements/TSpan.js b/elements/TSpan.js index ddb1bc2d..540291f6 100644 --- a/elements/TSpan.js +++ b/elements/TSpan.js @@ -2,7 +2,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import createReactNativeComponentClass from 'react-native/Libraries/Renderer/shims/createReactNativeComponentClass'; import extractText from '../lib/extract/extractText'; -import {numberProp, pathProps, fontProps} from '../lib/props'; +import {pathProps, fontProps} from '../lib/props'; import {TSpanAttibutes} from '../lib/attributes'; import extractProps from '../lib/extract/extractProps'; import Shape from './Shape'; @@ -18,16 +18,19 @@ export default class extends Shape { dy: PropTypes.string, }; + //noinspection JSUnusedGlobalSymbols static childContextTypes = { isInAParentText: PropTypes.bool }; + //noinspection JSUnusedGlobalSymbols getChildContext() { return { isInAParentText: true }; } + //noinspection JSUnusedGlobalSymbols getContextTypes() { return { isInAParentText: PropTypes.bool diff --git a/elements/Text.js b/elements/Text.js index cbcb3a7f..d8d68b29 100644 --- a/elements/Text.js +++ b/elements/Text.js @@ -2,7 +2,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import createReactNativeComponentClass from 'react-native/Libraries/Renderer/shims/createReactNativeComponentClass'; import extractText from '../lib/extract/extractText'; -import {numberProp, pathProps, fontProps} from '../lib/props'; +import {pathProps, fontProps} from '../lib/props'; import {TextAttributes} from '../lib/attributes'; import extractProps from '../lib/extract/extractProps'; import Shape from './Shape'; @@ -17,16 +17,19 @@ export default class extends Shape { dy: PropTypes.string, }; + //noinspection JSUnusedGlobalSymbols static childContextTypes = { isInAParentText: PropTypes.bool }; + //noinspection JSUnusedGlobalSymbols getChildContext() { return { isInAParentText: true }; } + //noinspection JSUnusedGlobalSymbols getContextTypes() { return { isInAParentText: PropTypes.bool diff --git a/elements/TextPath.js b/elements/TextPath.js index 78c1602f..c715f11e 100644 --- a/elements/TextPath.js +++ b/elements/TextPath.js @@ -4,7 +4,7 @@ import createReactNativeComponentClass from 'react-native/Libraries/Renderer/shi import {TextPathAttributes} from '../lib/attributes'; import extractText from '../lib/extract/extractText'; import Shape from './Shape'; -import {pathProps, fontProps, numberProp} from '../lib/props'; +import {pathProps, fontProps} from '../lib/props'; import extractProps from '../lib/extract/extractProps'; import TSpan from './TSpan'; diff --git a/index.js b/index.js index a72b59c6..57625482 100644 --- a/index.js +++ b/index.js @@ -42,4 +42,5 @@ export { ClipPath }; +//noinspection JSUnusedGlobalSymbols export default Svg; diff --git a/lib/Matrix2D.js b/lib/Matrix2D.js index ae634e53..3a880017 100644 --- a/lib/Matrix2D.js +++ b/lib/Matrix2D.js @@ -79,13 +79,13 @@ export default class Matrix2D { * @return {Matrix2D} This instance. Useful for chaining method calls. */ setTransform = function(a, b, c, d, tx, ty) { - /*eslint eqeqeq:0*/ - this.a = a == null ? 1 : a; + this.a = a === null || a === undefined ? 1 : a; this.b = b || 0; this.c = c || 0; - this.d = d == null ? 1 : d; + this.d = d === null || d === undefined ? 1 : d; this.tx = tx || 0; this.ty = ty || 0; + //noinspection JSValidateTypes return this; }; @@ -97,6 +97,7 @@ export default class Matrix2D { reset = function() { this.a = this.d = 1; this.b = this.c = this.tx = this.ty = 0; + //noinspection JSValidateTypes return this; }; @@ -109,6 +110,7 @@ export default class Matrix2D { return [this.a, this.b, this.c, this.d, this.tx, this.ty]; }; + //noinspection JSUnusedGlobalSymbols /** * Copies all properties from the specified matrix to this matrix. * @method copy @@ -116,9 +118,11 @@ export default class Matrix2D { * @return {Matrix2D} This matrix. Useful for chaining method calls. */ copy = function(matrix) { + //noinspection JSUnresolvedVariable return this.setTransform(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty); }; + //noinspection JSUnusedGlobalSymbols /** * Clones current instance and returning a new matrix. * @method clone @@ -142,9 +146,9 @@ export default class Matrix2D { * @return {Matrix2D} This matrix. Useful for chaining method calls. **/ prepend = function(a, b, c, d, tx, ty) { - var a1 = this.a; - var c1 = this.c; - var tx1 = this.tx; + const a1 = this.a; + const c1 = this.c; + const tx1 = this.tx; this.a = a * a1 + c * this.b; this.b = b * a1 + d * this.b; @@ -152,6 +156,7 @@ export default class Matrix2D { this.d = b * c1 + d * this.d; this.tx = a * tx1 + c * this.ty + tx; this.ty = b * tx1 + d * this.ty + ty; + //noinspection JSValidateTypes return this; }; @@ -168,10 +173,10 @@ export default class Matrix2D { * @return {Matrix2D} This matrix. Useful for chaining method calls. **/ append = function(a, b, c, d, tx, ty) { - var a1 = this.a; - var b1 = this.b; - var c1 = this.c; - var d1 = this.d; + const a1 = this.a; + const b1 = this.b; + const c1 = this.c; + const d1 = this.d; if (a !== 1 || b !== 0 || c !== 0 || d !== 1) { this.a = a1 * a + c1 * b; this.b = b1 * a + d1 * b; @@ -180,6 +185,7 @@ export default class Matrix2D { } this.tx = a1 * tx + c1 * ty + this.tx; this.ty = b1 * tx + d1 * ty + this.ty; + //noinspection JSValidateTypes return this; }; @@ -202,10 +208,11 @@ export default class Matrix2D { * @return {Matrix2D} This matrix. Useful for chaining method calls. **/ appendTransform = function(x, y, scaleX, scaleY, rotation, skewX, skewY, regX, regY) { + let cos, sin; if (rotation % 360) { - var r = rotation * DEG_TO_RAD; - var cos = Math.cos(r); - var sin = Math.sin(r); + const r = rotation * DEG_TO_RAD; + cos = Math.cos(r); + sin = Math.sin(r); } else { cos = 1; sin = 0; @@ -226,9 +233,11 @@ export default class Matrix2D { this.tx -= regX * this.a + regY * this.c; this.ty -= regX * this.b + regY * this.d; } + //noinspection JSValidateTypes return this; }; + //noinspection JSUnusedGlobalSymbols /** * Generates matrix properties from the specified display object transform properties, and prepends them to this matrix. * For example, you could calculate the combined transformation for a child object using: @@ -255,10 +264,11 @@ export default class Matrix2D { * @return {Matrix2D} This matrix. Useful for chaining method calls. **/ prependTransform = function(x, y, scaleX, scaleY, rotation, skewX, skewY, regX, regY) { + let cos, sin; if (rotation % 360) { - var r = rotation * DEG_TO_RAD; - var cos = Math.cos(r); - var sin = Math.sin(r); + const r = rotation * DEG_TO_RAD; + cos = Math.cos(r); + sin = Math.sin(r); } else { cos = 1; sin = 0; @@ -277,6 +287,7 @@ export default class Matrix2D { } else { this.prepend(cos * scaleX, sin * scaleX, -sin * scaleY, cos * scaleY, x, y); } + //noinspection JSValidateTypes return this; }; } diff --git a/lib/SvgTouchableMixin.js b/lib/SvgTouchableMixin.js index 790cf0b5..0fd3c2eb 100644 --- a/lib/SvgTouchableMixin.js +++ b/lib/SvgTouchableMixin.js @@ -1,6 +1,7 @@ import Touchable from 'react-native/Libraries/Components/Touchable/Touchable'; const PRESS_RETENTION_OFFSET = {top: 20, left: 20, right: 20, bottom: 30}; +//noinspection JSUnusedGlobalSymbols export default { ...Touchable.Mixin, diff --git a/lib/attributes.js b/lib/attributes.js index 0872e7a2..eec6c0a0 100644 --- a/lib/attributes.js +++ b/lib/attributes.js @@ -1,13 +1,12 @@ function arrayDiffer(a, b) { - /*eslint eqeqeq:0*/ - if (a == null || b == null) { + if (!a || !b) { return true; } if (a.length !== b.length) { return true; } - for (var i = 0; i < a.length; i++) { + for (let i = 0; i < a.length; i++) { if (a[i] !== b[i]) { return true; } diff --git a/lib/extract/extractBrush.js b/lib/extract/extractBrush.js index 039ec0f4..ffa2fb33 100644 --- a/lib/extract/extractBrush.js +++ b/lib/extract/extractBrush.js @@ -2,8 +2,7 @@ import Color from 'color'; import patternReg from './patternReg'; export default function (colorOrBrush) { - /*eslint eqeqeq:0*/ - if (colorOrBrush === 'none' || colorOrBrush == null) { + if (colorOrBrush === 'none' || !colorOrBrush) { return null; } diff --git a/lib/extract/extractFill.js b/lib/extract/extractFill.js index 430c77d0..b2fd7c22 100644 --- a/lib/extract/extractFill.js +++ b/lib/extract/extractFill.js @@ -18,8 +18,7 @@ export default function(props, styleProperties) { return { // default fill is black - /*eslint eqeqeq:0*/ - fill: extractBrush(props.fill == null ? '#000' : props.fill), + fill: extractBrush(props.fill || '#000'), fillOpacity: extractOpacity(props.fillOpacity), fillRule: fillRules[props.fillRule] === 0 ? 0 : 1 }; diff --git a/lib/extract/extractGradient.js b/lib/extract/extractGradient.js index 7befb7fe..41aeb4d9 100644 --- a/lib/extract/extractGradient.js +++ b/lib/extract/extractGradient.js @@ -21,6 +21,7 @@ export default function(props) { let offset = percentToFloat(child.props.offset); // add stop + //noinspection JSUnresolvedFunction stops[offset] = Color(child.props.stopColor).alpha(extractOpacity(child.props.stopOpacity)); } } else { diff --git a/lib/extract/extractProps.js b/lib/extract/extractProps.js index 1fd574b0..c1be5c44 100644 --- a/lib/extract/extractProps.js +++ b/lib/extract/extractProps.js @@ -30,7 +30,7 @@ export default function(props, ref) { let transform = props.transform; if (transform) { if (typeof transform === 'string') { - var transformParsed = tp.parse(transform); + const transformParsed = tp.parse(transform); if (transformParsed.matrix) { // TODO: Extract scaling values for coordinate system // Especially scaleY for calculating scaling of fontSize diff --git a/lib/extract/extractStroke.js b/lib/extract/extractStroke.js index a4c1ebdc..ccc93b71 100644 --- a/lib/extract/extractStroke.js +++ b/lib/extract/extractStroke.js @@ -44,7 +44,7 @@ export default function(props, styleProperties) { strokeLinecap: caps[props.strokeLinecap] || 0, strokeLinejoin: joins[props.strokeLinejoin] || 0, strokeDasharray: strokeDasharray || null, - strokeWidth: strokeWidth || "1", + strokeWidth: strokeWidth || '1', strokeDashoffset: strokeDasharray ? (+props.strokeDashoffset || 0) : null, strokeMiterlimit: props.strokeMiterlimit || 4 }; diff --git a/lib/extract/extractText.js b/lib/extract/extractText.js index 214f297c..11b9be37 100644 --- a/lib/extract/extractText.js +++ b/lib/extract/extractText.js @@ -1,4 +1,5 @@ import _ from 'lodash'; +//noinspection JSUnresolvedVariable import React, {Children} from 'react'; import TSpan from '../../elements/TSpan'; @@ -28,7 +29,7 @@ function parseFontString(font) { return null; } const fontFamily = extractSingleFontFamily(match[3]); - const fontSize = match[2] || "12"; + const fontSize = match[2] || '12'; const isBold = /bold/.exec(match[1]); const isItalic = /italic/.exec(match[1]); const fontWeight = isBold ? 'bold' : 'normal'; diff --git a/lib/extract/extractTransform.js b/lib/extract/extractTransform.js index fb097eb2..7f1b2329 100644 --- a/lib/extract/extractTransform.js +++ b/lib/extract/extractTransform.js @@ -15,11 +15,11 @@ function transformToMatrix(props, transform) { class TransformParser { constructor() { - var floating = '(\\-?[\\d\\.e]+)'; - var commaSpace = '\\,?\\s*'; + const floating = '(\\-?[\\d\\.e]+)'; + const commaSpace = '\\,?\\s*'; this.regex = { - split: /[\s*(\s*|\s*)\s*|\s*,\s*]/, + split: /[\s*()|,]/, matrix: new RegExp( '^matrix\\(' + floating + commaSpace + @@ -33,7 +33,7 @@ class TransformParser { parse(transform) { if (transform) { - var retval = {}; + const retval = {}; let transLst = _.filter( transform.split(this.regex.split), (ele) => { @@ -50,11 +50,11 @@ class TransformParser { break; case 'translate': retval.translateX = transLst[i + 1]; - retval.translateY = (3 === transLst.length) ? transLst[i + 2] : 0; + retval.translateY = (transLst.length === 3) ? transLst[i + 2] : 0; break; case 'scale': retval.scaleX = transLst[i + 1]; - retval.scaleY = (3 === transLst.length) ? transLst[i + 2] : retval.scaleX; + retval.scaleY = (transLst.length === 3) ? transLst[i + 2] : retval.scaleX; break; case 'rotate': retval.rotation = transLst[i + 1]; @@ -72,17 +72,6 @@ class TransformParser { return retval; } } - - parseMatrix(transform) { - var matrix = this.regex.matrix.exec(transform); - if (matrix) { - matrix.shift(); - for (var i = matrix.length - 1; i >= 0; i--) { - matrix[i] = parseFloat(matrix[i]); - } - } - return matrix; - } } export const tp = new TransformParser(); @@ -91,7 +80,7 @@ export const tp = new TransformParser(); function appendTransform(transform) { if (transform) { if (typeof transform === 'string') { - var transformParsed = tp.parse(transform); + const transformParsed = tp.parse(transform); if (transformParsed.matrix) { pooledMatrix.append(...transformParsed.matrix); } diff --git a/package.json b/package.json index 770ebdd2..800fb38b 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,9 @@ "babel-eslint": "^6.1.2", "eslint": "^2.13.1", "eslint-plugin-react": "^4.3.0", - "react-native": "^0.46.0" + "prop-types": "^15.5.10", + "react": "^16.0.0-alpha.12", + "react-native": ">=0.46.0" }, "nativePackage": true }