parse-css icon indicating copy to clipboard operation
parse-css copied to clipboard

Bug fixes and type annotations

Open 3bl3gamer opened this issue 1 year ago • 0 comments

This PR consists of two parts so they can be reverted if you want to.

Fixed some bugs highlighted by static analyzers

Corrected some errors/warns highlighted by ESLint and TypeScript. These changes partially overlap with https://github.com/tabatkins/parse-css/pull/51.

I've used these configs (not included to PR)

jsconfig.json

{
  "compilerOptions": {
    "checkJs": true,
    "noEmit": true,
    "strict": true,
    "noImplicitAny": false,
    "lib": ["ES2015", "DOM"]
  },
  "include": ["parse-css.js"]
}

.eslintrc.json

{
  "env": {
    "es6": true,
    "browser": true
  },
  "extends": "eslint:recommended",
  "rules": {
    "no-constant-condition": ["warn", { "checkLoops": false }]
  }
}

Added JSDoc type annotations for output values

I've added type annotation for class properties and some function arguments so return types of exported functions (like parseARule()) can now be inferred.

This may be very useful, for example, in this code:

const comp = parseAComponentValue('...')
if (comp.type === "FUNCTION") {
	console.log(comp.name, comp.value.length)
}

a editor with enabled TypeScript features (such as VSCode) will suggest that comp has a type property with one of predefined values ("BLOCK", "IDENT", "FUNCTION", ...) and inside if condition the comp type will be narrowed down to Func with string name and array value.

For simplicity, arguments of exported functions and some of the internal code is not type annotated.

while(1) loops are now while(true) because "1" is not an "endless" condition for TS

This function return type will be number | undefined, but with while(true) the type will be number as expected:

function f() {
  while(1) {
    return 0
  }
}

3bl3gamer avatar Jan 21 '24 08:01 3bl3gamer