Ignore javascript error in HTML file (VS Code) - javascript

I have an HTML file which is a Django template. For the most part, it's pretty boring, there's a bit of HTML and some Javascript code inside script tags.
The problem is that I've inserted a single line of Django template language (with the double curly braces), and it cascades into a thousand different errors on every line. How do I ignore this? All I can find is // #ts-ignore on Google which doesn't seem to work with HTML files.
I don't even know where to begin. Is this a linting issue? What linter am I using, which documentation should I look at, etc. I assume I should be using the default tools for javascript. Please help!
The line in question is:
var achievementFlag = {{ achievement_flag|yesno:"true,false" }};
Naturally, the double curly braces is bad, as is the | and the :. And now the javascript just has squiggles all over it.

Add this line to settings.json
"html.validate.scripts": false,
this line will make vscode ignores javascript validation in HTML files
credits: https://github.com/Microsoft/vscode/issues/17433#issuecomment-273870328

Related

How to make prettier recognise double curly brackets as JavaScript in Google Tag Manager?

I use VS Code editor with prettier to format my code, but in Google Tag Manager, you wrap variables with double curly brackets around it. E.g to define a variable in Google Tag Manager, you do
{{my_own_variable}}
This causes issues with prettier as it's not valid JavaScript code. What can I do to make prettier simply see this as a variable? I am willing to write a plugin for this (if possible) or is there some kind of config I can use etc. Any help would be appreciated
Edit: I want to make prettier accept and format the file, just like normal JavaScript. E.g I don't want to comment out EVERY line of code where it contains a GTM variable, can prettier simply recognise that say {{my_own_variable}}.map() is the same as my_own_variable.map()

How to check html snippets in my JS code?

I have html snippets as template literals in my JavaScript code. As the snippets are spread over multiple files and can be quite large and complex in structure and nesting, it can happen that some closing tags are mistakenly omitted or nesting is wrong.
Is there a way to automate checking of these html pieces for correct nesting and balance of opening and closing tags?
Try to go with best IDE like Netbeans, VScode or sublime where you can easy track the HTML tags opening and closing , Code Syntax(PHP,JS,CSS etc).
You can surely opt for an IDE, otherwise you can use a html validator in gulp task and verify your html files. Gulp task will first remove literals from the file(temporarily) and then stream the output to the validator.

Javascript is not recognizing a Flask variable

I'm passing a set of variables into a Flask template, and I would like to first manipulate them with Javascript. The problem is that when I use the {{ var }} syntax, Javascript isn't recognizing it.
The errors look like this. The left brackets give an "Identifier or string literal or numeric literal expected" error, and the variable names give an "Expression statement is not assignment or call" error.
I use {{ var }} syntax later, within the HTML portion of the document, and when I do that they appear just fine. Also, enclosing it in quotes as I do for a different variable doesn't work either. Anyone know what the issue could be? Thanks.
Jinja2 (flask's templating engine) is a preprocessor, meaning that its output is the real JS and it does not care about you're using it with HTML, JS or whatever, it only prints text.
That error you're getting is your text editor trying to help you but it's not smart enough to realize you're writing Jinja2 instead of javascript.
Edit: also, as #davidism says, you have to use jinja2 blocks.

JavaScript critical error

I am unable to include any javascript file in either the _Layout.cshtml or any other view. When I do so, I get the error "javascript critical error at line 3...". I'm using IE to browse. Errors are not shown in Chrome though. I am including the js file at the bottom of the view like this:
<script src="~/Content/bootstrap/js/bootstrap.min.js"></script>
The error is not specific to this file, any js file gives the error. I also tried sing the url.content, no luck. Thanks
To me it seems quite unrelated to ASP.NET since you are including a static JS file, so it's pure HTML.
I suppose in this case you should remove the tilda character ~ from file URL.

HTML syntax highlighting in javascript strings in vim

I don't know if this is possible/sensible, but I was curious to know if I can have my strings in javascript files have html highlighting. I discovered that strings in php could have SQL syntax highlighting, so I believe it is possible.
But, I don't know vim-scripting, so any help on this appreciated.
I am using the Better Javascript syntax.
PS: If there could be an option to turn it on and off while editing a js file, that would be wonderful
Thanks
Yes, it's possible if you don't mind some syntax file hacking. First you need to include the HTML syntax file from within the Javascript syntax file -- see :help syn-include for info on that; second you need to declare that HTML syntax can be found inside of certain elements (i.e. strings). Third, if you want to have the option of enabling and disabling it, you can make those commands dependent on a global variable, and write some mappings that set or unset the variable and then reload the syntax file.
For examples on how inclusion works, take a look at syntax/html.vim (which includes the Javascript and CSS syntax files), syntax/perl.vim (which includes the POD syntax file), or php.vim (which includes SQL syntax highlighting in strings, conditional on a global ariable).
Edit: did some work on actually making this happen in my copy.
In the head of syntax/javascript.vim, just below syn case ignore, add
syn include #javaScriptHTML syntax/html.vim
unlet b:current_syntax
syn spell default " HTML enables spell-checking globally, turn it off
Then add #javaScriptHTML to the contained= lists for javaScriptStringD and javaScriptStringS.
Finally you have to edit syntax/html.vim to prevent it from trying to include syntax/javascript.vim if it was loaded from javascript: find the line that reads
if main_syntax != 'java' || exists("java_javascript")
and change it to
if main_syntax != 'javascript' && ( main_syntax != 'java' || exists("java_javascript")

Categories