Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes and type annotations #52

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

3bl3gamer
Copy link

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 #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
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant