Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Enforcing `const` keyword checks?

Hello

 

While developing a watchface, I found out that the declaration of const variables is not enforced later on.

 

For example:

const foo = "foo";
foo = "bar";

console.log(foo);

Will print "bar" (since the declaration is transpiled to "var foo")

 

I'm not familiar enough with the Javascript ecosystem.

Is there a way to enforce a syntax check during the build?

Best Answer
0 Votes
2 REPLIES 2

Found out a solution that is working for my particular case:

 

  • In package.json, add "eslint": "^5.16.0" to the devDependencies
  • in the scripts section, replace the "build" entry : "build": "eslint ./app/**/*.js && fitbit-build"
  • finally the .eslintrc.js may need some tweaks for development:

 

module.exports = {
    "env": {
        "browser": true,
        "es6": true
    },
    "extends": "eslint:recommended",
    "globals": {
    },
    "parserOptions": {
        "ecmaVersion": 2018,
        "sourceType": "module"
    },
    "rules": {
        "no-console": "off",
// unused vars should be stripped by the transpile step "no-unused-vars": "warn", } };

 

 

Best Answer
0 Votes

The watch only supports ES5, which predates const being added to JavaScript. If you use const in companion/settings, it'll behave as you expect.

 

In the future, we might be able to detect at compile time with the help of TypeScript when you've attempted to modify a const value.

Best Answer
0 Votes