Skip to Content
🎉 Univer 0.5.5 is released.Read more →
GuidesUniver GoAdvanced FeaturesEditor

Editor

ESLint Rules

Univer Go’s Code Editor includes built-in support for syntax error detection and ESLint integration.

Below are the ESLint rules configured for the editor, along with explanations and examples of each rule.

import { Linter } from 'eslint-linter-browserify'; export const lint = new Linter(); export const eslintConfig = { rules: { 'no-use-before-define': 'warn', // Warn about use before definition 'curly': 'error', // Enforce the use of curly braces 'no-param-reassign': ['warn'], // Warn about modifying function parameters 'quotes': ['warn', 'single', { avoidEscape: true }], // Enforce single quotes, unless avoiding escape 'indent': ['warn', 4], // Indent with 4 spaces 'no-cond-assign': 'warn', // Warn about assignment in conditional statements 'no-new': 'warn', // Warn about using `new` to create an object without assignment 'no-multiple-empty-lines': ['warn', { max: 1, maxEOF: 1 }], // Allow at most one empty line 'eqeqeq': ['warn', 'always'], // Enforce the use of strict equality 'no-debugger': 'error', 'semi': ['warn', 'always'], // Or 'never' 'no-redeclare': 'error', 'no-unreachable': 'error', 'valid-typeof': ['error', { requireStringLiterals: true }], 'no-const-assign': 'error', // Disallow reassigning const 'prefer-const': ['warn', { destructuring: 'all' }], // Prefer using const 'no-var': 'error', // Disallow using var }, } as const;

Rule Descriptions and Examples

no-use-before-define

  • Description: Warn about using variables or functions before they are defined.
  • Severity: warn
// @errors: 1 console.log(a); // Warning: 'a' used before it is defined const a = 10;

curly

  • Description: Enforce the use of curly braces for all control statements.
  • Severity: error
// @errors: 1 if (true) console.log('No curly braces'); // Error: Expected { after 'if' condition

no-param-reassign

  • Description: Warn about reassigning function parameters.
  • Severity: warn
// @errors: 1 function updateValue(obj) { obj = {}; // Warning: Assignment to function parameter 'obj' }

quotes

  • Description: Enforce the use of single quotes unless escaping.
  • Severity: warn
// @errors: 1 const message = "double quotes"; // Warning: Strings must use single quotes

indent

  • Description: Enforce consistent indentation of 4 spaces.
  • Severity: warn
// @errors: 1 function greet() { console.log('Hi'); // Warning: Expected indentation of 4 spaces }

no-cond-assign

  • Description: Warn about assignments in conditional expressions.
  • Severity: warn
// @errors: 1 if (x = 10) { // Warning: Assignment in conditional expression console.log(x); }

no-new

  • Description: Warn about using new to create an object without assignment.
  • Severity: warn
// @errors: 1 new MyClass(); // Warning: 'new' used without assignment

no-multiple-empty-lines

  • Description: Allow at most one empty line.
  • Severity: warn
// @errors: 1 console.log('Too many empty lines'); // Warning: Multiple empty lines not allowed

eqeqeq

  • Description: Enforce the use of strict equality (===).
  • Severity: warn
// @errors: 1 if (x == 10) { // Warning: Expected '===' and instead saw '==' console.log('Use strict equality'); }

no-debugger

  • Description: Disallow the use of debugger in code.
  • Severity: error
// @errors: 1 debugger; // Error: 'debugger' is disallowed

semi

  • Description: Enforce the use of semicolons at the end of statements.
  • Severity: warn
// @errors: 1 let x = 10 // Warning: Missing semicolon

no-redeclare

  • Description: Disallow variable redeclaration.
  • Severity: error
// @errors: 1 let x = 10; let x = 20; // Error: 'x' has already been declared

no-unreachable

  • Description: Disallow unreachable code after a return, throw, continue, or break statement.
  • Severity: error
// @errors: 1 function foo() { return; console.log('Unreachable code'); // Error: Unreachable code detected }

valid-typeof

  • Description: Enforce the use of string literals for typeof comparisons.
  • Severity: error
// @errors: 1 if (typeof x === 'object') { // Error: Use 'string' instead of 'object' console.log('x is an object'); }

no-const-assign

  • Description: Disallow reassigning const variables.
  • Severity: error
// @errors: 1 const x = 10; x = 20; // Error: 'x' is a constant and cannot be reassigned

prefer-const

  • Description: Prefer using const when variables are not reassigned.
  • Severity: warn
// @errors: 1 let x = 10; // Warning: 'x' can be 'const' instead of 'let'

no-var

  • Description: Disallow the use of var and encourage let or const.
  • Severity: error
// @errors: 1 var x = 10; // Error: 'var' is disallowed, use 'let' or 'const' instead

For more details, refer to the ESLint documentation.

Last updated on

Was this page helpful?