Skip to content

Commit

Permalink
More restrictive typing - noImplicitAny
Browse files Browse the repository at this point in the history
  • Loading branch information
Bajdzis committed Jul 31, 2019
1 parent 14458f1 commit 1d72e94
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 25 deletions.
81 changes: 68 additions & 13 deletions package-lock.json

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

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"react": "^16.0.0"
},
"devDependencies": {
"@types/prop-types": "^15.7.1",
"@types/react": "^16.8.24",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-plugin-istanbul": "^5.1.0",
Expand All @@ -72,9 +74,9 @@
"jsdom": "^11.12.0",
"nyc": "^13.2.0",
"pre-commit": "^1.2.2",
"prop-types": "^15.6.2",
"react": "^16.7.0",
"react-dom": "^16.7.0",
"prop-types": "^15.7.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"ts-node": "^8.3.0",
"typescript": "^3.5.3"
},
Expand Down
20 changes: 20 additions & 0 deletions src/domain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

export type FunctionReturnBoolean = () => boolean;

export type BemValueInObject = boolean | FunctionReturnBoolean;

export type BemValue = string | string[] | { [key: string]: BemValueInObject };

export type BemSetting = {
elementDelimiter: string;
modifierDelimiter: string;
bemIndicationElement: RegExp;
bemIndicationMod: RegExp;
bemIndicationSeparator: string;
};

export type BemInfo = {
className: string,
blocksName: string[],
elementsName: string[]
}
20 changes: 12 additions & 8 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as PropTypes from 'prop-types';
import { BemValue, BemInfo } from './domain';

/**
* Default setting
Expand Down Expand Up @@ -37,7 +38,7 @@ export function addModifiersToClassName(className: string, modifiers: string[] =
* @param {string} element
* @return {bool}
*/
export function checkBemInfoCondition(bemInfo, block: string, element: string = ''): boolean {
export function checkBemInfoCondition(bemInfo: BemInfo, block: string, element: string = ''): boolean {
if (bemInfo.blocksName.length > 0 && bemInfo.blocksName.indexOf(block) === -1) {
return false;
}
Expand All @@ -53,7 +54,7 @@ export function checkBemInfoCondition(bemInfo, block: string, element: string =
* @param {object} setting
* @return {object}
*/
export function getStringBemInfo(className: string, setting = DEFAULT_BEM_SETTING) {
export function getStringBemInfo(className: string, setting = DEFAULT_BEM_SETTING): BemInfo {
const blocksName = [];
const elementsName = [];
const [onlyClassName] = className.split(setting.bemIndicationSeparator);
Expand All @@ -80,14 +81,13 @@ export function getStringBemInfo(className: string, setting = DEFAULT_BEM_SETTIN
* @param {string|object|string[]} bemValue
* @return {string[]}
*/
export function convertBemValueToArray(bemValue): string[] {
const type = typeof bemValue;
export function convertBemValueToArray(bemValue: BemValue): string[] {

if(type === 'string'){
if (typeof bemValue === 'string') {
return bemValue.split(' ');
}

if(type === 'object'){
if (typeof bemValue === 'object') {

if(bemValue === null){
return [];
Expand All @@ -98,7 +98,10 @@ export function convertBemValueToArray(bemValue): string[] {
}

return Object.keys(bemValue)
.filter(key => typeof bemValue[key] === 'function' ? bemValue[key]() : bemValue[key]);
.filter(key => {
const value = bemValue[key];
return typeof value === 'function' ? value() : value;
});
}

return [];
Expand All @@ -109,7 +112,8 @@ export function convertBemValueToArray(bemValue): string[] {
* @param {object} props
* @return {object}
*/
export function cleanUpProps(props){
type ObjectWithAnyValue = {[key: string]: any};
export function cleanUpProps(props: ObjectWithAnyValue): ObjectWithAnyValue {
props = {...props};

delete props.bemName;
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"outDir": "./lib",
"allowJs": true,
"target": "es5",
"noImplicitAny": true
},
"include": [
"./**/*"
Expand Down
2 changes: 1 addition & 1 deletion types/react-simple-bem.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';

declare module "react-simple-bem" {
export const BemSetting: BemSettingComponent;
Expand Down

0 comments on commit 1d72e94

Please sign in to comment.