惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
L
LINUX DO - 最新话题
TaoSecurity Blog
TaoSecurity Blog
S
Security Affairs
H
Hacker News: Front Page
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hacker News: Ask HN
Hacker News: Ask HN
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
Google DeepMind News
Google DeepMind News
Schneier on Security
Schneier on Security
云风的 BLOG
云风的 BLOG
I
InfoQ
The Register - Security
The Register - Security
T
Tor Project blog
T
Threat Research - Cisco Blogs
Spread Privacy
Spread Privacy
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
The GitHub Blog
The GitHub Blog
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
Recent Announcements
Recent Announcements
Vercel News
Vercel News
F
Fortinet All Blogs
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
G
Google Developers Blog
N
Netflix TechBlog - Medium
U
Unit 42
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
O
OpenAI News
博客园 - 叶小钗
T
Tailwind CSS Blog
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Help Net Security
Help Net Security
A
About on SuperTechFans
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
D
DataBreaches.Net
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
The Blog of Author Tim Ferriss
PCI Perspectives
PCI Perspectives
F
Full Disclosure
美团技术团队
L
Lohrmann on Cybersecurity
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - Liaofy

前端新范式:用 AI 提效开发,用 E2E 保证迭代质量 把SPA的加载速度提升一倍,有没有搞头? 使用mumu模拟器抓包 andriod app 如何在本地给 vue-router4 扩展功能? Vue3 路由优化,使页面初次渲染效率翻倍 vue 项目使用 charles 代理线上页面到本地后显示404 避免使用单个数字作为对象的key 高扩展弹出层组件设计实现(H5&小程序) 轻松搞懂POST与PUT的区别 vite(or rollup) 实现 webpackChunkName webapp项目架构畅想(vue3).md vite试玩:老项目启动从6分钟到秒开 修剪AST树减少webapck打包尺寸 【uniapp】改善中大型uniapp小程序项目开发体验 vscode 调试 webpack 打包 5分钟,搞清 Unicode 与 UTF-8 的区别 deAsync.js 真正让异步变同步 盘的转——使用缓动函数完成动画 多个请求间断失败,如何优雅处理?
【eslint 插件开发】禁用 location 跳转外部链接
Liaofy · 2023-02-17 · via 博客园 - Liaofy

背景

公司 h5 项目需要为跳转的外部链接统一增加参数。举个例子,假设有如下代码:

location.href = 'https://www.test.com/a?id=xxx'
location.replace('https://www.test.com/a?id=xxx')

我们需要把所有链接都增加参数uid:

location.href = 'https://www.test.com/a?id=xxx&uid=someuid'
location.replace('https://www.test.com/a?id=xxx&uid=someuid')

需求分析

使用函数统一跳转逻辑

方便维护和更新,我们需要使用函数来统一跳转逻辑。

// 直接跳转
location.href = 'abc' 转化为 navigateTo('abc')
location.replace('abc') 转化为 redirectTo('abc')

虽然可以查找整个项目进行手动替换,但多个项目的人工操作,显然是费时费力的。

禁止后续提交使用 location 跳转

当我们完成当前代码的替换后,还需要防止其他人使用 location 进行跳转。一个基本事实是,无法用人工监督来杜绝这个行为(或者说可以监督但成本太高)。

使用 eslint 插件,自定义错误规则和修复程序,可以完美解决这两个问题。

创建插件 eslint-plugin-huoli

如果首次创建插件,建议先阅读官方文档Create Plugins

或者参考我的项目eslint-plugin-huoli

插件的写法很简单,就是针对某个 AST 节点类型进行处理。你可以在ast 官网查看某条代码对应的 AST 数据结构和节点类型。

注意点

遍历节点时机:enter、exit

可能大家更熟悉 babel 插件,了解遍历 AST 节点有两个时机:进入和退出:

module.exports = {
  ...
  AssignmentExpression: {
    enter: node => {},
    exit: node => {},
  }
}

eslint 也有一样,不过写法略有不同:

module.exports = {
  ...
  AssignmentExpression() {},
  'AssignmentExpression:exit'(node) {},
}

如何调试插件

开发插件过程中,如果能轻松的进行 debug 会极大提升开发效率。参考eslint-plugin-huoli

首先,使用Ruletester创建测试用例。

然后,在package.json中增加脚本:

{
  scripts: {
    ...
    "debug": "node node_modules/jest/bin/jest",
  }
}

使用 vscode 进行调试