Skip to content

Commit

Permalink
feat: use eslint to lint codes
Browse files Browse the repository at this point in the history
  • Loading branch information
kekee000 committed Sep 17, 2020
1 parent 6d8c4b5 commit 144edbd
Show file tree
Hide file tree
Showing 97 changed files with 1,422 additions and 1,424 deletions.
File renamed without changes.
39 changes: 39 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// eslint-disable-next-line import/no-commonjs
module.exports = {
'env': {
'browser': true,
'es2021': true,
'node': true
},
'extends': ['eslint:recommended', 'esnext'],
'parserOptions': {
'ecmaVersion': 12,
'sourceType': 'module'
},
'rules': {
'indent': [
'error',
4
],
'linebreak-style': [
'error',
'unix'
],
'quotes': [
'error',
'single'
],
'semi': [
'error',
'always'
],
'eqeqeq': [
'error',
'always',
{
'null': 'ignore'
}
],
'class-methods-use-this': 0
}
};
26 changes: 0 additions & 26 deletions .fecsrc

This file was deleted.

2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fonteditor-core",
"version": "0.0.33",
"version": "2.1.2",
"homepage": "https://github.com/kekee000/fonteditor-core",
"authors": [
"kekee000 <[email protected]>",
Expand Down
17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fonteditor-core",
"version": "2.1.1",
"version": "2.1.2",
"description": "fonts (ttf, woff, woff2, eot, svg, otf) parse, write, transform, glyph adjust.",
"keywords": [
"sfnt",
Expand Down Expand Up @@ -34,12 +34,21 @@
}
],
"scripts": {
"precommit": "npm run lint",
"publish:npm": "npm run build && npm publish --registry=https://registry.npmjs.org/",
"test": "./node_modules/.bin/mocha --require @babel/register test/spec/*.spec.js test/spec/**/*.spec.js",
"test:node": "npm run build && ./node_modules/mocha/bin/mocha test/node-spec/**/*.spec.js ",
"dev": "webpack-dev-server --open --config ./config/webpack.dev.js",
"build": "babel src --out-dir lib",
"lint": "fecs ./src --reporter=baidu --rule"
"lint": "eslint ./src"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"src/**/*.{jsx,txs,ts,js,vue}": ["eslint"]
},
"dependencies": {
"xmldom": "~0.1.19"
Expand All @@ -63,7 +72,11 @@
"babel-plugin-module-resolver": "^3.2.0",
"commander": "^3.0.2",
"css-loader": "^3.2.0",
"eslint": "^7.9.0",
"eslint-config-esnext": "^4.1.0",
"html-webpack-plugin": "^3.2.0",
"husky": "^4.3.0",
"lint-staged": "^10.4.0",
"mocha": "^6.2.1",
"pako": "^1.0.10",
"style-loader": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/common/DOMParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author mengke01([email protected])
*/

/* eslint-disable no-undef, fecs-no-require */
/* eslint-disable no-undef */
export default typeof exports !== 'undefined'
? require('xmldom').DOMParser
: window.DOMParser;
6 changes: 3 additions & 3 deletions src/common/I18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*/

function appendLanguage(store, languageList) {
languageList.forEach(function (item) {
let language = item[0];
languageList.forEach(item => {
const language = item[0];
store[language] = Object.assign(store[language] || {}, item[1]);
});
return store;
Expand Down Expand Up @@ -65,7 +65,7 @@ export default class I18n {
* @return {string} 语言字符串
*/
get(path) {
let ref = path.split('.');
const ref = path.split('.');
let refObject = this.lang;
let level;
while (refObject != null && (level = ref.shift())) {
Expand Down
16 changes: 7 additions & 9 deletions src/common/ajaxFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
* @param {Object=} options.params 参数集合
*/
export default function ajaxFile(options) {
let xhr = new XMLHttpRequest();
const xhr = new XMLHttpRequest();

xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
let status = xhr.status;
const status = xhr.status;
if (status >= 200 && status < 300 || status === 304) {
if (options.onSuccess) {
if (options.type === 'binary') {
let buffer = xhr.responseBlob || xhr.response;
const buffer = xhr.responseBlob || xhr.response;
options.onSuccess(buffer);
}
else if (options.type === 'xml') {
Expand All @@ -38,20 +38,18 @@ export default function ajaxFile(options) {
}

}
else {
if (options.onError) {
options.onError(xhr, xhr.status);
}
else if (options.onError) {
options.onError(xhr, xhr.status);
}
}
};

let method = (options.method || 'GET').toUpperCase();
const method = (options.method || 'GET').toUpperCase();
let params = null;
if (options.params) {

let str = [];
Object.keys(options.params).forEach(function (key) {
Object.keys(options.params).forEach(key => {
str.push(key + '=' + encodeURIComponent(options.params[key]));
});
str = str.join('&');
Expand Down
28 changes: 16 additions & 12 deletions src/common/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export function isDate(obj) {
}

export function isEmptyObject(object) {
for (let name in object) {
for (const name in object) {
// eslint-disable-next-line no-prototype-builtins
if (object.hasOwnProperty(name)) {
return false;
}
Expand All @@ -43,7 +44,8 @@ export function isEmptyObject(object) {
*/
export function curry(fn, ...cargs) {
return function (...rargs) {
let args = cargs.concat(rargs);
const args = cargs.concat(rargs);
// eslint-disable-next-line no-invalid-this
return fn.apply(this, args);
};
}
Expand Down Expand Up @@ -78,7 +80,7 @@ export function overwrite(thisObj, thatObj, fields) {

// 这里`fields`未指定则仅overwrite自身可枚举的字段,指定`fields`则不做限制
fields = fields || Object.keys(thatObj);
fields.forEach(function (field) {
fields.forEach(field => {
// 拷贝对象
if (
thisObj[field] && typeof thisObj[field] === 'object'
Expand Down Expand Up @@ -112,7 +114,7 @@ export function clone(source) {
}
else if (isObject(source) && 'isPrototypeOf' in source) {
cloned = {};
for (let key of Object.keys(source)) {
for (const key of Object.keys(source)) {
cloned[key] = clone(source[key]);
}
}
Expand All @@ -130,15 +132,16 @@ export function throttle(func, wait) {
let timeout;
let result;
let previous = 0;
let later = function () {
const later = function () {
previous = new Date();
timeout = null;
result = func.apply(context, args);
};

return function (...args) {
let now = new Date();
let remaining = wait - (now - previous);
const now = new Date();
const remaining = wait - (now - previous);
// eslint-disable-next-line no-invalid-this
context = this;
if (remaining <= 0) {
clearTimeout(timeout);
Expand All @@ -163,15 +166,16 @@ export function debounce(func, wait, immediate) {
let result;

return function (...args) {
let context = this;
let later = function () {
// eslint-disable-next-line no-invalid-this
const context = this;
const later = function () {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
}
};

let callNow = immediate && !timeout;
const callNow = immediate && !timeout;

clearTimeout(timeout);
timeout = setTimeout(later, wait);
Expand Down Expand Up @@ -208,8 +212,8 @@ export function equals(thisObj, thatObj, fields) {

// 这里`fields`未指定则仅overwrite自身可枚举的字段,指定`fields`则不做限制
fields = fields || (typeof thisObj === 'object'
? Object.keys(thisObj)
: []);
? Object.keys(thisObj)
: []);

if (!fields.length) {
return thisObj === thatObj;
Expand Down
11 changes: 5 additions & 6 deletions src/common/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ export default {
*/
decodeHTML(source) {

let str = String(source)
const str = String(source)
.replace(/&quot;/g, '"')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&amp;/g, '&');

// 处理转义的中文和实体字符
return str.replace(/&#([\d]+);/g, function ($0, $1) {
return String.fromCodePoint(parseInt($1, 10));
});
return str.replace(/&#([\d]+);/g, ($0, $1) => String.fromCodePoint(parseInt($1, 10)));
},

/**
Expand All @@ -47,6 +45,7 @@ export default {
* @return {number} 长度
*/
getLength(source) {
// eslint-disable-next-line no-control-regex
return String(source).replace(/[^\x00-\xff]/g, '11').length;
},

Expand All @@ -58,8 +57,8 @@ export default {
* @return {string} 格式化后字符串
*/
format(source, data) {
return source.replace(/\$\{([\w.]+)\}/g, function ($0, $1) {
let ref = $1.split('.');
return source.replace(/\$\{([\w.]+)\}/g, ($0, $1) => {
const ref = $1.split('.');
let refObject = data;
let level;

Expand Down
24 changes: 12 additions & 12 deletions src/graphics/computeBoundingBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function computeBoundingBox(points) {
let bottom = points[0].y;

for (let i = 1; i < points.length; i++) {
let p = points[i];
const p = points[i];

if (p.x < left) {
left = p.x;
Expand Down Expand Up @@ -87,14 +87,14 @@ function computeQuadraticBezierBoundingBox(p0, p1, p2) {
t1 = Math.max(Math.min(t1, 1), 0);
t2 = Math.max(Math.min(t2, 1), 0);

let ct1 = 1 - t1;
let ct2 = 1 - t2;
const ct1 = 1 - t1;
const ct2 = 1 - t2;

let x1 = ct1 * ct1 * p0.x + 2 * ct1 * t1 * p1.x + t1 * t1 * p2.x;
let y1 = ct1 * ct1 * p0.y + 2 * ct1 * t1 * p1.y + t1 * t1 * p2.y;
const x1 = ct1 * ct1 * p0.x + 2 * ct1 * t1 * p1.x + t1 * t1 * p2.x;
const y1 = ct1 * ct1 * p0.y + 2 * ct1 * t1 * p1.y + t1 * t1 * p2.y;

let x2 = ct2 * ct2 * p0.x + 2 * ct2 * t2 * p1.x + t2 * t2 * p2.x;
let y2 = ct2 * ct2 * p0.y + 2 * ct2 * t2 * p1.y + t2 * t2 * p2.y;
const x2 = ct2 * ct2 * p0.x + 2 * ct2 * t2 * p1.x + t2 * t2 * p2.x;
const y2 = ct2 * ct2 * p0.y + 2 * ct2 * t2 * p1.y + t2 * t2 * p2.y;

return computeBoundingBox(
[
Expand All @@ -121,14 +121,14 @@ function computeQuadraticBezierBoundingBox(p0, p1, p2) {
*/
function computePathBoundingBox(...args) {

let points = [];
let iterator = function (c, p0, p1, p2) {
const points = [];
const iterator = function (c, p0, p1, p2) {
if (c === 'L') {
points.push(p0);
points.push(p1);
}
else if (c === 'Q') {
let bound = computeQuadraticBezierBoundingBox(p0, p1, p2);
const bound = computeQuadraticBezierBoundingBox(p0, p1, p2);

points.push(bound);
points.push({
Expand All @@ -139,13 +139,13 @@ function computePathBoundingBox(...args) {
};

if (args.length === 1) {
pathIterator(args[0], function (c, p0, p1, p2) {
pathIterator(args[0], (c, p0, p1, p2) => {
if (c === 'L') {
points.push(p0);
points.push(p1);
}
else if (c === 'Q') {
let bound = computeQuadraticBezierBoundingBox(p0, p1, p2);
const bound = computeQuadraticBezierBoundingBox(p0, p1, p2);

points.push(bound);
points.push({
Expand Down
Loading

0 comments on commit 144edbd

Please sign in to comment.