HoriSun / closure-compiler

Automatically exported from code.google.com/p/closure-compiler
0 stars 0 forks source link

Code with typo resulting in adding unintended property compiles without errors or warnings. #1283

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Compile this file:

/**
   @constructor
 */
function Class() {
    /** @type{ !number } */
    this.start = 1;
}

/** @type{ !Class } */
var instance = new Class;

instance.statr = 4; //TYPO IN THIS LINE

2. I used the command java.exe -jar closure_compiler.jar --compilation_level 
ADVANCED_OPTIMIZATIONS --summary_detail_level 3 --warning_level VERBOSE --js 
test.js --js_output_file ./test_result.js

What is the expected output? What do you see instead?

I expect the compiler to produce a compilation error, as the property statr is 
a typo. Instead I see 0 error(s), 0 warning(s), 100.0% typed.

What version of the product are you using? On what operating system?

java.exe -jar closure_compiler.jar --version
Closure Compiler (http://code.google.com/closure/compiler)
Version: v20140303
Built on: 2014/03/04 10:53

Windows 7

Please provide any additional information below.

The error should be shown in this scenario. Otherwise any typo just adds a new 
property to the object, silently hiding the bug from the programmer. There is 
currently no means in closure compiler to prevent such typo bugs AFAIK. For any 
class where the programmer took time to strongly type it (constructor and all 
properties), compiler should at least warn (but best fail) when adding a new 
unknown property outside of constructor.

I tried also with the @final annotation to prevent extending the object, but it 
didn't help. I think the @final only applies to inheritance extensions, not 
property extensions.

If producing an error would break too much legacy code, I suggest adding a new 
annotation @very-strongly-typed, or similar, where the programmer can mark a 
class for which all properties are typed in the constructor and any new 
unintended ones added by typos should give errors. Also, a global warning 
command line parameter would help a lot for peple who have only strongly typed 
classes throughout the codebase.

Original issue reported on code.google.com by closure....@gmail.com on 18 Mar 2014 at 10:03

GoogleCodeExporter commented 9 years ago
JavaScript allows adding properties outside the constructor, and the compiler 
can't know if an assignment to a property is a typo or a new property.

If you only want to create new properties in the constructor, use @struct.
https://developers.google.com/closure/compiler/docs/js-for-compiler#tag-struct

Original comment by dim...@google.com on 18 Mar 2014 at 11:26