Vim errorformat multiline string - javascript

I'm trying to parse this error output for Flow. In the first error, it's the second file and line number that really matters, not the first. I would like to tell vim to use the second file entry. (So in this case, the location list should jump to source.js line 94, not line 20)
/Users/asdf/source.js:20:22,34:1: property hidden
Property not found in
/Users/asdf/source.js:94:10,106:3: object literal
/Users/asdf/source.js:25:14,18: identifier strin
Could not resolve name
Here's the error format currently defined for flow: It works except that it uses the first filename instead of the second.
let errorformat =
\ '%f:%l:%c:%n: %m,' .
\ '%f:%l:%c: %m'
Attempted Solution
I've been trying to use multi-line error formats, but I don't know what I'm doing. After reading :help errorformat, it seems like something like this should work, but it just loads the whole error into the message, with no file / line information. It also matches the second error using the first entry rather than the third.
let errorformat =
\ '%E%m,%C%m,%Z%f:%l:%c:%n: %m,' .
\ '%f:%l:%c:%n: %m,' .
\ '%f:%l:%c: %m'
Update
Thanks to lcd047, who pointed out that I'm not matching the correct output. Syntastic formats the error as follows, all on one line:
/Users/seanhess/projects/serials/web/app/model/source.js:20:22: property hidden Property not found in object literal (/Users/seanhess/projects/serials/web/app/model/source.js:94:10,106:3)
So, new question, how can I match the second location instead of the first? I think I'd prefer to ignore the first location.
Update Answer - this worked:
let errorformat =
\ '%.%#: %m (%f:%l:%c\,%.%#),' .
\ '%f:%l:%c:%n: %m,' .
\ '%f:%l:%c: %m'

You are not getting the file/line information because the %E specifies to use the rest of the line as the beginning of a multi-line message.
Try changing the "%Em" to "%E%f:%l:%c:%n: %m" for starters, since you basically want to keep all the original data being grabbed from the first version of errorformat.
You may want to change the "%Cm" to "%+Cm" to capture all of the lines in your error output.
The "%Z%f:%l:%c:%n: %m" looks good and should be used on the second line, since the %E will catch the first one.
Try that.

Related

A variable which is recognised in one line is is not recognised two lines later

chosenCategory[i] = questionsCats[randomCat].category;
console.log(chosenCategory[i]);
console.log(questionCats[randomCat].category;
This is three consecutive lines of code. In the second line, the log shows that the previous line worked and therefore that questionCats[randomCat].category was found. In the last line I get an error - Uncaught ReferenceError: questionCats is not defined
at initStart().
How is it possible that after doing nothing except reading it, it is suddenly undefined and how do I resolve it.
Thanks
Sometimes you might need to be more explicit with the codes and show the ouside context of your codes.
Mean while you have 2 errors in your codes:
Missing closing parenthese
Typo in the variable, you checked on questionsCats on the 1st line, and ask for questionCats on the last line
The 3rd line in codes should be with questionsCats:
chosenCategory[i] = questionsCats[randomCat].category;
console.log(chosenCategory[i]);
console.log(questionsCats[randomCat].category);
1 TIP: Consider that the Compiler is always right. So when it says questionCats is not defined at initStart()., take it as:
Well I did not have a variable name questionCats in that function, or maybe I made some typo in it.

Replacing comma by plus in javascript for entire project

I use winston to perform logging currently and have written a common method for it to be used all over project. Problem is, many of logging statements are like, logger.info("here is the data" , data)
With comma as concatenator, i couldn't log data in console. data can also be a content containing comma so I wouldn't be able to just use replace ',' by '+' .
My idea is regex can be like, if text starts with ' or " and its next character is ',' at end of quotes, replace with '+'
Ain't sure if it would be right but still, please help with your suggestions.
Perhaps you can monkey-patch it with something like
logger.info = ((infoFunc) => {
// create the patched info that concatenates all arguments
// before calling the original logger.info
let patch = () => {
// put any logic here that you need to achieve the desired result.
var message = args.join('');
infoFunc(message);
};
return patch;
})(logger.info);
Not tested
Just make sure it's run right after the logger is set up.
This will work as a quick fix to get things running but I wouldn't recommend leaving it in your code and should be removed once all calls to logger.info have been cleaned up.

nodeJS for each new line in a variable, add a hyphen?

I have a single string variable that when logged to console looks like the following with line breaks:
index
project.json
extras
I would like to be able to print it like:
-index
-project.json
-extras
what would be a good way to start on this? I am still in the process of learning JS, so I really have nothing to show for what I've tried, sorry.
I have tried some of the methods(console.log('-' + files_var)), but this does:
that does this:
- index
project.json
extras
You can replace all the line breaks with a line break and a - using .replace() passing a regex /...regex.../ and giving it a global so it replaces all instances. Remember the first string would not have a line break so we will need to add it manually.
console.log("-" + files_var.replace(/\n\r/g, '\n\r -'))
DEMO

Javascript regex match fails on actual page, but regex tests work just fine

I have a very specific problem concerning a regular expression matching in Javascript. I'm trying to match a piece of source code, more specifically a portion here:
<TD WIDTH=100% ALIGN=right>World Boards | Olympa - Trade | <b>Bump when Yasir...</b></TD>
The part I'm trying to match is boardid=106121">Olympa - Trade</a>, the part I actually need is "Olympa". So I use the following line of JS code to get a match and have "Olympa" returned:
var world = document.documentElement.innerHTML.match('/boardid=[0-9]+">([A-Z][a-z]+)( - Trade){0,1}<\/a>/i')[1];
the ( - Trade) part is optional in my problem, hence the {0,1} in the regex.
There's also no easier way to narrow down the code by e.g. getElementsByTagName, so searching the complete source code is my only option.
Now here's the funny thing. I have used two online regex matchers (of which one was for JS-regex specifically) to test my regex against the complete source code. Both times, it had a match and returned "Olympa" exactly as it should have. However, when I have Chrome include the script on the actual page, it gives the following error:
Error in event handler for 'undefined': Cannot read property '1' of null TypeError: Cannot read property '1' of null
Obviously, the first part of my line returns "null" because it does not find a match, and taking [1] of "null" doesn't work.
I figured I might not be doing the match on the source code, but when I let the script output document.documentElement.innerHTML to the console, it outputs the complete source code.
I see no reason why this regex fails, so I must be overlooking something very silly. Does anyone else see the problem?
All help appreciated,
Kenneth
You're putting your regular expression inside a string. It should not be inside a string.
var world = document.documentElement.innerHTML.match(/boardid=[0-9]+">([A-Z][a-z]+)( - Trade){0,1}<\/a>/i)[1];
Another thing — it appears you have a document object, in which case all this HTML is already parsed for you, and you can take advantage of that instead of reinventing a fragile wheel.
var element = document.querySelector('a[href*="boardid="]');
var world = element.textContent;
(This assumes that you don't need <=IE8 support. If you do, there remains a better way, though.)
(P.S. ? is shorthand for {0,1}.)

textmate reformat with 2 spaces

I've set textmate to use softtabs 2 spaces on my file. But when I try to reformat the entire document, it uses 2 hard tabs as the indents.
Regular indents work as I want it to, just the document format doesn't. Anyway to get textmate to be obedient?
Thanks.
The JavaScript bundle's "Reformat Document / Selection" command is passing the document's text to the js_beautify function in the bundle's beautify.php file (found on my system and probably by default at /Applications/TextMate.app/Contents/SharedSupport/Bundles/JavaScript.tmbundle/Support/lib/beautify.php). If you take a look at the function definition you'll see that there's a second parameter, $tab_size, with a default value of 4. There's a line in the bundle that reads print js_beautify($input);. Change this to print js_beautify($input, 2); and you should, I expect, get tab stops with two spaces.
To make it a bit more flexible, use the TextMate environment variable TM_TAB_SIZE, as in print js_beautify( $input, getenv('TM_TAB_SIZE' ) );, which should update how the command operates if you ever change your tab size.
Note, I've tested none of this. :) Just took a look at the bundle and tracked down what seems to be necessary.
So, I tried chuck's suggestion and it gave me an error. I did this to "fix it". I'm sure it could be done more elegantly, but this worked for me.
Open up the same file Chuck says to open up, line 50 (or so) should look like this:
function js_beautify($js_source_text, $tab_size = 4)
change $tab_size to 1
function js_beautify($js_source_text, $tab_size = 1)
Now, around line 56 where it says:
$tab_string = str_repeat(' ', $tab_size);
change the space to a tab like so:
$tab_string = str_repeat("\t", $tab_size);
That worked for me.

Categories