HBuilderX configuration automatic formatting, unified code specification

1 Introduction


  Teamwork is an essential issue for companies with a small scale, and it is also an important cornerstone for project development progress assurance. Only by working as a team and maximizing each individual's skills can we ensure the maximum productivity of the team. As the team increases, a unified code style becomes more and more important. For this purpose, use HBuilderX to configure automatic formatting and unify code specifications.

2. Code style


  • Overall style
    • javascriptgenerally follow ESlintthe norm
    • html, wxmlgenerally follow prettyhtmlthe norms
  • javascriptdetail adjustment
    • no semicolon at the end
    • Newlines beyond 140 characters
    • use single quotes
    • no trailing comma
    • Arrow function single parameter without semicolon
    • Spaces before parentheses are prohibited in function declarations

3. Prepare the plug-in


HBuilderX plugin

4. Personalized configuration


1. Open method
  • Tools -> Plugin Installation -> Installed Plugins [Plugin Name] -> Configure

personalized configuration

2. Personalized configuration, the specific operation steps are configured in the reference materials below
// 详细配置教程请参考:http://eslint.cn/docs/user-guide/configuring
module.exports = {
	"plugins": [
		"html"
	],
	"parserOptions": {
		"ecmaVersion": 2018,
		"sourceType": "module",
		"ecmaFeatures": {
			"jsx": true
		},
		"allowImportExportEverywhere": false
	},
	"rules": {
		"no-alert": 0,
		"eqeqeq": ["error", "always"], // 用强等于做判断
		"semi": ["error", "never"], // 结尾不分号
		"no-multi-spaces": "error",
		"quotes": ["error", "single"], // 使用单引号
		"arrow-parens": ["error", "as-needed"], // 简略箭头函数
		"object-curly-newline": ["error", { "multiline": true }], // 在属性内部或属性之间有换行符,就要求有换行符
		"object-curly-spacing": ["error", "always"] // 要求花括号内有空格 (除了 {})
	}
};

module.exports = {
	'extends': 'plugin:vue/essential',
	'parserOptions': {
		ecmaVersion: 2018,
		sourceType: 'module'
	},
	'rules': {
		'no-alert': 0,
		'eqeqeq': ['error', 'always'], // 用强等于做判断
		'semi': ['error', 'never'], // 结尾不分号
		'no-multi-spaces': 'error',
		'quotes': ['error', 'single'], // 使用单引号
		'arrow-parens': ['error', 'as-needed'], // 简略箭头函数
		'object-curly-newline': ['error', {
			'multiline': true
		}], // 在属性内部或属性之间有换行符,就要求有换行符
		'object-curly-spacing': ['error', 'always'], // 要求花括号内有空格 (除了 {})
		//在computed properties中禁用异步actions
		'vue/no-async-in-computed-properties': 'error',
		//不允许重复的keys
		'vue/no-dupe-keys': 'error',
		//不允许重复的attributes
		'vue/no-duplicate-attributes': 'warn',
		//在 <template> 标签下不允许解析错误
		'vue/no-parsing-error': ['error', {
			'x-invalid-end-tag': false,
		}],
		//不允许覆盖保留关键字
		'vue/no-reserved-keys': 'error',
		//强制data必须是一个带返回值的函数
		// 'vue/no-shared-component-data': 'error',
		//不允许在computed properties中出现副作用。
		'vue/no-side-effects-in-computed-properties': 'error',
		//<template>不允许key属性
		'vue/no-template-key': 'warn',
		//在 <textarea> 中不允许mustaches
		'vue/no-textarea-mustache': 'error',
		//不允许在v-for或者范围内的属性出现未使用的变量定义
		'vue/no-unused-vars': 'warn',
		//<component>标签需要v-bind:is属性
		'vue/require-component-is': 'error',
		// render 函数必须有一个返回值
		'vue/require-render-return': 'error',
		//保证 v-bind:key 和 v-for 指令成对出现
		'vue/require-v-for-key': 'error',
		// 检查默认的prop值是否有效
		'vue/require-valid-default-prop': 'error',
		// 保证computed属性中有return语句
		'vue/return-in-computed-property': 'error',
		// 强制校验 template 根节点
		'vue/valid-template-root': 'error',
		// 强制校验 v-bind 指令
		'vue/valid-v-bind': 'error',
		// 强制校验 v-cloak 指令
		'vue/valid-v-cloak': 'error',
		// 强制校验 v-else-if 指令
		'vue/valid-v-else-if': 'error',
		// 强制校验 v-else 指令
		'vue/valid-v-else': 'error',
		// 强制校验 v-for 指令
		'vue/valid-v-for': 'error',
		// 强制校验 v-html 指令
		'vue/valid-v-html': 'error',
		// 强制校验 v-if 指令
		'vue/valid-v-if': 'error',
		// 强制校验 v-model 指令
		'vue/valid-v-model': 'error',
		// 强制校验 v-on 指令
		'vue/valid-v-on': 'error',
		// 强制校验 v-once 指令
		'vue/valid-v-once': 'error',
		// 强制校验 v-pre 指令
		'vue/valid-v-pre': 'error',
		// 强制校验 v-show 指令
		'vue/valid-v-show': 'error',
		// 强制校验 v-text 指令
		'vue/valid-v-text': 'error',
		'vue/comment-directive': 0
	}
};

  • keytab completion
工具 -> 设置 -> `编辑器配置`里选中 `tab键自动插入代码助手选中项`

key completion

  • Set the indent length to 2
工具 -> 设置 -> 常用配置 -> 制表符长度 改为 2

indentation

  • Destructuring assignment does not wrap curly braces
工具 -> 设置 -> 插件配置 -> 自定义`jsbeautify`格式化规则的`jsbeautifyrc.js`中将 "brace_style"选项添加"preserve-inline"

Destructuring assignment does not wrap curly braces

References

Guess you like

Origin blog.csdn.net/qq_29088015/article/details/117229218