Skip to content

Commit

Permalink
refactor: mocha 支持es6,ts代码规范化
Browse files Browse the repository at this point in the history
  • Loading branch information
wall-wxk committed Aug 7, 2019
1 parent 55a08e8 commit f7e64c5
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 74 deletions.
11 changes: 3 additions & 8 deletions .babelrc
@@ -1,12 +1,7 @@
{
"presets": [
["@babel/preset-env", {
"modules": false,
"useBuiltIns": false
}],
["@babel/preset-typescript"]
],
"plugins": [
"@babel/plugin-transform-runtime"
]
"modules": false
}]
]
}
8 changes: 5 additions & 3 deletions .eslintrc.js
Expand Up @@ -17,7 +17,6 @@ module.exports = {
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
], // 核心规则
"globals": { // 全局变量
Expand All @@ -27,8 +26,11 @@ module.exports = {
"@typescript-eslint"
],
"rules": {
"prefer-const": "off",
"no-unused-vars": "warn",
"no-console": "warn",
"generator-star-spacing": "off"
"no-console": "off",
"generator-star-spacing": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/explicit-function-return-type": "off",
}
};
2 changes: 1 addition & 1 deletion example/lib/function-sniffer.cjs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/lib/function-sniffer.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -10,7 +10,7 @@
"clean:example": "rimraf example/lib",
"dev": "npm run clean:example && npm run clean && rollup --watch --config scripts/rollup/rollup.config.dev.js",
"build": "npm run clean && tsc && rollup --config scripts/rollup/rollup.config.prod.js",
"eslint": "eslint src --fix --ext .ts",
"eslint": "eslint src --ext .ts",
"push:publish": "git push --follow-tags origin master && npm publish",
"test": "nyc mocha",
"coveralls": "npm run test && nyc report --reporter=text-lcov | coveralls"
Expand Down Expand Up @@ -76,7 +76,7 @@
},
"lint-staged": {
"src/**/*.ts": [
"eslint src --fix --ext .ts",
"eslint src --ext .ts",
"git add"
]
},
Expand Down
14 changes: 12 additions & 2 deletions scripts/rollup/rollup.config.base.js
Expand Up @@ -15,13 +15,23 @@ export default {
}),
babel({
exclude: 'node_modules/**', // 只编译我们的源代码
runtimeHelpers: true
runtimeHelpers: true,
presets: [
["@babel/preset-env", {
"modules": false,
"useBuiltIns": false
}],
["@babel/preset-typescript"]
],
plugins: [
"@babel/plugin-transform-runtime"
]
}),
commonjs({
include: 'node_modules/**'
}),
eslint({
include: ['src/**/*.js']
include: ['src/**/*.ts']
})
]
};
6 changes: 2 additions & 4 deletions scripts/rollup/rollup.config.prod.js
Expand Up @@ -14,13 +14,11 @@ import {
libraryName
} from '../config.js';

const banner = `
/*!
const banner = `/*!
* ${name}.js v${version}
* (c) 2019-${new Date().getFullYear()} ${author.name}
* Released under the ${license} License.
*/
`;
*/`;

let override = { compilerOptions: {
"declaration": false,
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Expand Up @@ -5,7 +5,7 @@ import {
Toption
} from './utils/type'

let list:Tlist = [];
let list: Tlist = [];

export default {
/**
Expand All @@ -24,7 +24,7 @@ export default {
Sniffer.run.apply(window, [ {'name':'Wall.mytext.init'}, 45, false ])
```
*/
run: function(...args: any[]){
run: function(...args: any[]): any{
return run(list, ...args);
},
/**
Expand All @@ -37,7 +37,7 @@ export default {
* 另外,调用trigger方法的前提是,订阅方法所在js已经加载并解析完毕
* 不管触发成功与否,都会清除list中对应的项
*/
trigger: function(option: Toption){
trigger: function(option: Toption): any{
return trigger(list, option);
}
}
18 changes: 9 additions & 9 deletions src/utils/checkMethod.ts
@@ -1,19 +1,19 @@
type Tresult = {
success: boolean,
func: object | ((...args: any[])=>any)
};
interface Tresult{
success: boolean;
func: object | ((...args: any[]) => any);
}
/**
* @function {private} 检测方法是否可用
* @param {string} funcName -- 方法名***.***.***
* @param {object} base -- 方法所依附的对象
*/
export default function(funcName:string, base:object): Tresult{
export default function(funcName: string, base: object): Tresult{
const methodList = funcName.split('.'); // 方法名list
let readyFunc = base; // 检测合格的函数部分
let result:Tresult = {
'success': true,
'func': ()=>{}
}; // 返回的检测结果
let result: Tresult = {
'success': true,
'func': ()=>{}
}; // 返回的检测结果
let methodName; // 单个方法名
let i;

Expand Down
14 changes: 7 additions & 7 deletions src/utils/run.ts
Expand Up @@ -5,18 +5,18 @@ import {
} from './type';

export default function (
list:Tlist,
list: Tlist,
...args
) {
if (args.length < 1 || typeof args[0] != 'object') {
throw new Error('Sniffer.run parameter error');
}

// 0位为Object类型,方便做扩展
const name:string = args[0].name; // 函数名
let subscribe:boolean = args[0].subscribe || false; // 订阅当函数可执行时,调用该函数, true:订阅; false:不订阅
const prompt:string = args[0].prompt || ''; // 是否显示提示语(当函数未能执行的时候)
const base:object = args[0].base || window; // 基准对象,函数查找的起点
const name: string = args[0].name; // 函数名
let subscribe: boolean = args[0].subscribe || false; // 订阅当函数可执行时,调用该函数, true:订阅; false:不订阅
const prompt: string = args[0].prompt || ''; // 是否显示提示语(当函数未能执行的时候)
const base: object = args[0].base || window; // 基准对象,函数查找的起点
const showPromptFn = args[0].showPromptFn || window.alert;
const funcArgs = Array.prototype.slice.call(args).slice(1); // 函数的参数列表
let result = checkMethod(name, base); // 检测结果
Expand All @@ -37,12 +37,12 @@ export default function (

//将订阅的函数缓存起来
if (subscribe) {
let callbackFunc:TcallbackFunc = {
let callbackFunc: TcallbackFunc = {
name: ''
}; // 临时存放需要回调的函数
callbackFunc.name = name;
callbackFunc.base = base;
callbackFunc.args = funcArgs;
list.push(callbackFunc);
}
};
}
6 changes: 3 additions & 3 deletions src/utils/trigger.ts
Expand Up @@ -4,14 +4,14 @@ import {
Toption
} from './type'

export default function (list: Tlist, option: Toption):void {
export default function (list: Tlist, option: Toption): void {
if (typeof option !== 'object') {
throw new Error('Sniffer.trigger parameter error');
}

const funcName = option.name || ''; // 函数名
const base = option.base || window; // 基准对象,函数查找的起点
let newList:Tlist = []; // 用于更新list
let newList: Tlist = []; // 用于更新list
let i; // 遍历list
let param; // 临时存储list[i]

Expand All @@ -37,4 +37,4 @@ export default function (list: Tlist, option: Toption):void {
}

list = newList;
};
}
20 changes: 10 additions & 10 deletions src/utils/type.ts
@@ -1,12 +1,12 @@
export type TcallbackFunc = {
name: string,
base?: object,
args?: Array<any>
};
export interface TcallbackFunc{
name: string;
base?: object;
args?: any[];
}

export type Tlist = Array<TcallbackFunc>;
export type Tlist = TcallbackFunc[];

export type Toption = {
name: string,
base?: object
};
export interface Toption{
name: string;
base?: object;
}
10 changes: 6 additions & 4 deletions test/checkMethod.test.js
@@ -1,7 +1,9 @@
var Sniffer = require('../dist/function-sniffer.cjs.js');
var expect = require('chai').expect;
import Sniffer from '../dist/function-sniffer.cjs';
import {
expect
} from 'chai';

var Test = {
let Test = {
say: function(str){
return str;
},
Expand All @@ -25,7 +27,7 @@ describe('checkMethod', function(){
})
it('checkMethod readyFunc is not object.', function(){
Test.undefined = undefined;
var result = Sniffer.run({
const result = Sniffer.run({
base: Test,
name:'undefined.undefined'
})
Expand Down
1 change: 1 addition & 0 deletions test/mocha.opts
@@ -1,2 +1,3 @@
--require jsdom-global/register
--require @babel/register
--require esm
16 changes: 9 additions & 7 deletions test/run.test.js
@@ -1,7 +1,9 @@
var Sniffer = require('../dist/function-sniffer.cjs.js');
var expect = require('chai').expect;
import Sniffer from '../dist/function-sniffer.cjs';
import {
expect
} from 'chai';

var Test = {
let Test = {
say: function(str){
return str;
},
Expand All @@ -15,14 +17,14 @@ var Test = {

describe('run()', function(){
it('run default', function(){
var result = Sniffer.run({
const result = Sniffer.run({
base: Test,
name: 'say'
}, 'hello');
expect(result).to.equal('hello');
})
it('run function not exit', function(){
var result = Sniffer.run({
const result = Sniffer.run({
base: window,
name: 'say'
}, 'hello');
Expand All @@ -44,7 +46,7 @@ describe('run()', function(){
}).to.throw('Sniffer.run parameter error');
})
it('run is not function', function(){
var result = Sniffer.run({
const result = Sniffer.run({
base: Test,
name: 'next.value'
}, 1, 2)
Expand All @@ -54,7 +56,7 @@ describe('run()', function(){
window.sayHello = function(){
return true;
}
var result = Sniffer.run({
const result = Sniffer.run({
name: 'sayHello',
});

Expand Down
22 changes: 12 additions & 10 deletions test/trigger.test.js
@@ -1,7 +1,9 @@
var Sniffer = require('../dist/function-sniffer.cjs.js');
var expect = require('chai').expect;
import Sniffer from '../dist/function-sniffer.cjs';
import {
expect
} from 'chai';

var Test = {
let Test = {
say: function(str){
return str;
},
Expand All @@ -15,7 +17,7 @@ var Test = {

describe('trigger()', function(){
it('trigger default', function(){
var result = Sniffer.run({
const result = Sniffer.run({
base: Test,
name: 'do',
subscribe: true
Expand All @@ -27,15 +29,15 @@ describe('trigger()', function(){
return str
};

var result2 = Sniffer.trigger({
const result2 = Sniffer.trigger({
base: Test,
name: 'do'
});

expect(result2).to.equal(true);
})
it('trigger default base', function(){
var result = Sniffer.run({
const result = Sniffer.run({
base: window,
name: 'todo',
subscribe: true
Expand All @@ -47,7 +49,7 @@ describe('trigger()', function(){
return str
};

var result2 = Sniffer.trigger({
const result2 = Sniffer.trigger({
name: 'todo'
});

Expand All @@ -72,7 +74,7 @@ describe('trigger()', function(){
subscribe: true
}, true);

var result = Sniffer.trigger({
const result = Sniffer.trigger({
base: Test,
name: 'notExist'
});
Expand All @@ -88,7 +90,7 @@ describe('trigger()', function(){

Test.notFn = {};

var result = Sniffer.trigger({
const result = Sniffer.trigger({
base: Test,
name: 'notFn'
});
Expand All @@ -107,7 +109,7 @@ describe('trigger()', function(){
})
it('checkMethod readyFunc is undefined.', function(){
Test.undefined = undefined;
var result = Sniffer.run({
const result = Sniffer.run({
base: Test,
name:'undefined.undefined'
})
Expand Down

0 comments on commit f7e64c5

Please sign in to comment.