Suppress Code Analysis For a Single JS File - visual-studio

My project has a vendor.js file that is showing warnings (TS7027 unreachable code detected).
I don't want to fix those warnings since it's vendors code. But I don't want to see those warnings either.
Is there a way to tell Visual Studio (2017) to suppress all messages for this file (e.g. with the CodeAnalysis.SuppressMessage attribute)?
I am not using resharper, just the built-in code analysis tool.

Related

Bizzare behaviour with Visual Studio Code Analysis Rule 1804

We are using Visual Studio Code Analysis Rules for code quality checks. We have created our custom .ruleset file and marked some violations as Errors, so that every time anyone's violating these rules, he will get a compile error and have to fix the issue.
Now everything run fine with debug mode, but when I build my project in release mode, I am not getting any error with rule 1804 i.e. Remove unused locals. In fact this violation is totally absent from violation list, not even as warning!!
Knowing that one need to have build configuration set to run your code analysis rules, I have enabled code analysis for all configurations in project properties, like this -
Doing this works good for all other rules but 1804.
Is it seems like a bug with code analysis, or it's a valid behavior for some reason I am not aware of, and is there's anything to get it work?
I am using Visual Studio 2013
This is not as strange as you may think. FxCop acts on the binaries, not on the sources. And in Release Mode the compiler will apply optimizations, such as removing unused locals, simplifying if/else statements and other things which lead to the same execution, but with higher performance or lower memory usage.
After these pieces of code have been optimized away, FxCop won't be able to detect it.
This is why it makes most sense do to binary analysis on the non-optimized binaries.
With Visual Studio 2015, most of the analysis has been moved into Roslyn Analyzers, which will catch these issues by analyzing the source code, instead of the binary output. This will allow Code Analysis (in 2015) to detect these issues, even if they didn't make it into the final binaries.

Which editor and debugger for typescript

I am working on a nodejs project in which all code is written in typescript. It follows a microservice pattern (and each microservice is an independent project) hence lot of projects needs to be opened and debugged at the same time.
I tried webstorm and visual studio (with NTVS) but not satisified with both of them. Webstorm ignores many build errors (very important as those will fail during CI) and not as good as visual studio in intellisense and ease of use. On the other hand, Visualstudio is not as fast as I want. Also I am tied to windows and the filename length problem in node_modules is very frequent and annoying.
Others (atom, VSCode) doesn't have typescript debug support (or am I missing something?).
Can you please share your experience? Also I was wondering which editor google developers might be using to develop angular 2 :)
I have been working on a TypeScript project for 1.5 years now and am quite happy with using Visual Studio because we used .NET for back-end stuffs. For myself I consider the speed as "Ok", it is not that good - but the other stuffs e.g. intellisense is what we really like. I also personally think that as a "son" of Microsoft, TypeScript will get the best support from its "cousin" - Visual Studio.
However if you are a Eclipse person, you may be interested in this TypEcs (http://typecsdev.com/). There was also a post by Steve Fenton talking about TypeScript IDEs you may be interested (https://www.stevefenton.co.uk/Content/Blog/Date/201409/Blog/Which-TypeScript-IDE/), he listed a few of the popular ones with a few sentences of comments, including Atom and the atom-typescript extension.
A bit late to the conversation.
I recently tried VisualStudio 2015, VisualStudio Code 0.9, Atom (with Atom-Typescript package). And quite comfortable with Atom.
Atom-TypeScript uses the latest version of TypeScript so you can use all new features like async/await, string interpolation etc. The package created by community so we don't need to wait for major IDE release to use new TypeScript features.
For debugging couldn't find one. But love to use node-inspector for its light weight. The problem is you need to debug it in javascript version not in typescript version. Not so sure but maybe if you can provide map file you can debug the typescript version? like we have on Google Chrome Inspector.
VSCode can debug in Node. No browser debugging yet.
Not a recommendation, but I suggest you do not buy JetBrains' products (IntelliJ/WebStorm/ReSharper) for TypeScript editing unless you have evaluated them properly.
They reused a lot of their own logic for other languages instead of taking full benefit of the TypeScript language service, which means they behave more like a fancy JavaScript editor that can make some clever guesses as opposed to a robust IDE for a statically typed langauge. (Their type inference is different from the compiler and may fool you into believing an untyped expression (implicit any) is well-typed. Also, all the refactoring are the same as the JS ones, so they do not use or preserve any type information. No type-information-based refactoring (like safe renaming and moving) are available either.)
As for debugging, you may have more milage debugging the generated JS. Not only is it well supported, it also reveals problems that occur in compiler-inserted code. The latter would be otherwise difficult to understand if you're only looking at the TypeScript source. Also, in the generated code, you can actually set break points in single line lambda function bodies.
I too use Visual Studio. I haven't had any problem with its speed; I find everything is as immediate as I expect it too.
However, I haven't been using "TypeScript" projects. I have been using generic "Web" projects with TypeScript files. Plus, I don't use Visual Studio's own building and compilation facilities; I use my own Gulp tasks for my project (including rebuilding, watching for saves, minification, testing, etc), and Task Runner Explorer to manage/run them from inside Visual Studio. I think it's the best of both worlds: the snappiness and features of Visual Studio, and the flexibility of running my own custom tasks independent of platform.
I have to admit I don't do any deep debugging (e.g. breakpoints, stepping) from the IDE. Does VS do that with TypeScript? If I catch an error during execution (in the browser), I always have my source maps indicating where the error is, so I go back to the code, attempt to fix it, save, and reload in the browser.
I am using VSCode to actively debug an angular/node/typescript application. It does work, but you will need to do some setup/configuration. You must tell VSCode how to debug your applications. For instance I have multiple options in my launch.json configuration to debug my gulp scripts, my node server.js file, and the angular/typescript application via chrome using the "Debugger for Chrome" extension installed in VSCode. It is all possible with some configuration. Finally, depending on your exact situation you may need to have and correctly configure the map files for your project from javascript to typescript.
I also had the problem of file paths too long in the node_modules folder in visual studio when opening a project with npm. This is due to Visual Studio trying to download all your packages - incorrectly so I might add. The only way to avoid this for my project was to mark the node_modules folder as a hidden folder in windows (top level only).

Visual Studio 2010 Warning numbers. Where do I find them?

I suppress a few warnings, 1591 is the XML comments warning. It's not that I don't use the comments, there are some false positives.
Now to find the fact that XML comments warning is 1591 took a whole load of googling. Is there a list somewhere? Or a way to display the error number in Visual Studio itself?
Thanks
The Error List hides the error and warning numbers, but if you open the Output panel in Visual Studio (menu : Debug > Windows > Output) and set it to show output from Build, you can find the warning number somewhere in the wall of text.
You could also try building the project from the command line (using msbuild) which will output and highlight all error and warning numbers.
You can find a whole list of them here:
Compiler and Warning messages for C/C++
Compiler and Warning messages for C#
It honestly took me less than 5 seconds of Googling to find that, using the search terms "1591" and "visual studio". The top hit is right on the money: http://www.google.com/search?q=1591+visual+studio
So suffice it to say, this is how I usually find them.
But they're all documented on the page you land at with the above search results: C# Compiler Errors
Do note that the warning numbers are often different for different languages. In particular, C#, VB.NET, and C/C++ all use different compilers and therefore all emit different error codes.
C# Compiler Errors
C/C++ Build Errors
Also, it's worth noting that on VS 2008 and later, you can right-click over a warning in the "Error List" to display documentation related to that warning. This tells you the error level, the error number, and whatever other information you need.
I stumbled on this thread while trying to sort out how to find these in VS2015 with Beckhoff TwinCAT and found the Beckhoff-specific ones under Project->Properties, in the Compiler Warnings tab (as below in 1).
screencap of TwinCAT project properties' compiler warning list

Custom Compiler Warnings in Visual Studio 2008

Custom Compiler Warnings and
C#: Create custom warning in Visual Studio if certain method is used in source code
haven't helped as they deal with code that is under the author's control.
We are using a 3rd party suite of UI controls (DevExpress) in our software and I want to generate a warning when someone uses MessageBox.Show("blah"); instead of XtraMessageBox.Show("blah");
Is there a way to do that?
This sort of thing can be addressed relatively easily via a custom rule for FxCop/Visual Studio Code Analysis. If you are using Visual Studio Developer Edition, you will even see the rule failures displayed along-side your compilation warnings and errors in the IDE.
While there's no way you can do real custom compile-time error in .NET, there's a number of third-party tools (both free and commercial) that can inject their validation logic into the build process, usually after the compilation.
Here are three ways I know of to solve you problem:
Resharper 5.0($) will support custom rules / warnings.
In PostSharp(free) you can define OnMethodBoundary aspect, overwrite its CompileTimeValidate method and emit a [post]compile-time error from it.
NDepend can be integrated with your build process ($) to enforce coding policies like that
No there is no direct way. If you think about it you are looking for a compiler warning for some code that you don't even compile.
If you really want this you could use Reflection methods on YOUR compiled assembly to check if any methods/assemblies you don't want have been called. Cecil has a lot of the functionality you need. You could then make this part of your build process.

Excluding Code Analysis rule in source

In a project I'm working on FxCop shows me lots of (and I mean more than 400) errors on the InitializeComponent() methods generated by the Windows Forms designer. Most of those errors are just the assignment of the Text property of labels.
I'd like to suppress those methods in source, so I copied the suppression code generated by FxCop into AssemblyInfo.cs, but it doesn't work.
This is the attribute that FxCop copied to the clipboard.
[module: SuppressMessage("Microsoft.Globalization",
"CA1303:DoNotPassLiteralsAsLocalizedParameters",
Scope = "member",
Target = "WindowsClient.MainForm.InitializeComponent():System.Void",
MessageId = "System.Windows.Forms.Control.set_Text(System.String)")]
Anyone knows the correct attribute to suppress this messages?
PS: I'm using Visual Studio 2005, C#, FxCop 1.36 beta.
You've probably got the right code, but you also need to add CODE_ANALYSIS as a precompiler defined symbol in the project properties. I think those SuppressMessage attributes are only left in the compiled binaries if CODE_ANALYSIS is defined.
In FxCop 1.36 there is actually a project option on the "Spelling & Analysis" tab that will supress analysis for any generated code.
If you don't want to turn analysis off for all generated code, you need to make sure that you add a CODE_ANALYSIS symbol to the list of conditional compilation symbols (project properties, Build tab). Without this symbol defined, the SupressMessage attributes will be removed from the compiled code so FxCop won't see them.
The other problem with your SuppressMessage attribute is that you are listing a "Target" of a specific method name (in this case WindowsClient.MainForm.InitializeComponent():System.Void) and listing a specific "Scope". You may want to try removing these; otherwise you should add this SuppressMessage to each instance of the method.
You should also upgrade to the RTM version of FxCop 1.36, the beta will not automatically detect the newer version.
Module level suppression messages need to be pasted into the same file as the code that is raising the FxCop error before the namespace declaration or in assemblyinfo.cs. Additionally, you will need to have CODE_ANALYSIS defined as a conditional compiler symbols (Project > Properties > Build). Once that is in place, do a complete rebuild of project and the next time you run FxCop the error should be moved to the "Excluded in Source" tab.
Also, one small tip, but if you are dealing with a lot of FxCop exclusions it might be useful to wrap a region around them so you can get them out of the way.

Resources