"abc" is read-only while parsing file:
export const abc = 20;
abc = 30; // Reassigning the value
Will this fail across all browsers?
If the browsers are on 'use strict mode' declaring a const will work but the export will fail.
Just found out the answer. Browsers do throw error if we try to change the value of a const and they don't allow to rewrite. But safari doesn't handle this, it doesn't throws any error.
Related
I am trying to replace dynamically "import" statements.
Here is an example that checks if the import ends with a Plus.
module.exports = function(babel) {
return {
visitor: {
ImportDeclaration: function(path, state) {
// import abc from "./logic/+"
if( ! path.node.source.value.endsWith("/+"))
return;
path.replaceWithSourceString('import all from "./logic/all"')
}
}
}
}
This gives an error of
SyntaxError: src/boom.js: Unexpected token (1:1) - make sure this is an expression.
> 1 | (import all from "./logic/all")
The problem is that replaceWithSourceString is wrapping the string in rounded braces.
If I change the replaceWithSourceString to
path.replaceWithSourceString('console.log("Hi")')
and this works.. ¯_(ツ)_/¯
Any and all help you be great
replaceWithSourceString should really be avoided, because it is just not a very good API, as you are seeing. The recommended approach for creating ASTs to insert into the script is to use template. Assuming this is for Babel 7.x, you can do
const importNode = babel.template.statement.ast`import all from "./logic/all"`;
path.replaceWith(importNode);
I am developing an extension for visual studio code using language server protocol, and I am including the support for "Go to symbol in workspace". My problem is that I don't know how to select the matches...
Actually I use this function I wrote:
function IsInside(word1, word2)
{
var ret = "";
var i1 = 0;
var lenMatch =0, maxLenMatch = 0, minLenMatch = word1.length;
for(var i2=0;i2<word2.length;i2++)
{
if(word1[i1]==word2[i2])
{
lenMatch++;
if(lenMatch>maxLenMatch) maxLenMatch = lenMatch;
ret+=word1[i1];
i1++;
if(i1==word1.length)
{
if(lenMatch<minLenMatch) minLenMatch = lenMatch;
// Trying to filter like VSCode does.
return maxLenMatch>=word1.length/2 && minLenMatch>=2? ret : undefined;
}
} else
{
ret+="Z";
if(lenMatch>0 && lenMatch<minLenMatch)
minLenMatch = lenMatch;
lenMatch=0;
}
}
return undefined;
}
That return the sortText if the word1 is inside the word2, undefined otherwise. My problem are cases like this:
My algorithm see that 'aller' is inside CallServer, but the interface does not mark it like expected.
There is a library or something that I must use for this? the code of VSCode is big and complex and I don't know where start looking for this information...
VSCode's API docs for provideWorkspaceSymbols() provide the following guidance (which I don't think your example violates):
The query-parameter should be interpreted in a relaxed way as the editor will apply its own highlighting and scoring on the results. A good rule of thumb is to match case-insensitive and to simply check that the characters of query appear in their order in a candidate symbol. Don't use prefix, substring, or similar strict matching.
These docs were added in response to this discussion, where somebody had very much the same issue as you.
Having a brief look at VSCode sources, internally it seems to use filters.matchFuzzy2() for the highlighting (see here and here). I don't think it's exposed in the API, so you would probably have to copy it if you wanted the behavior to match exactly.
I have an api that in the past developments would receive comma separated strings as valid input and used the following as the validator:
Joi.string()
But now I want to implement the same variable using array of strings as mentioned here https://github.com/glennjones/hapi-swagger/issues/119. So the new check would be:
Joi.array().items(Joi.string())
But I do not want to break the backward compatibility of the code. Is there a way to check both conditions for the variable?
I am a newbie to Joi, so any help or guidance would be appreciated. Thanks in advance.
Take a look at .alternatives().try() which supports multiple schemas for a single field.
For example:
Joi.alternatives().try(Joi.array().items(Joi.string()), Joi.string())
This will validate both arrays of strings and plain strings, however as I'm sure you know, you'll still need server side logic to check which format the value is so you can process it properly.
You can use alternatives.try or shorthand [schema1, schema2]
const Joi = require('joi');
const schema1 = {
param: Joi.alternatives().try(Joi.array().items(Joi.string()), Joi.string())
};
const result1 = Joi.validate({param: 'str1,str2'}, schema1);
console.log(result1.error); // null
const result2 = Joi.validate({param: ['str1', 'str2']}, schema1);
console.log(result2.error); // null
const schema2 = {
param: [Joi.array().items(Joi.string()), Joi.string()]
};
const result3 = Joi.validate({param: 'str1,str2'}, schema2);
console.log(result3.error); // null
const result4 = Joi.validate({param: ['str1', 'str2']}, schema2);
console.log(result4.error); // null
I'm trying to take something like var a = 5; and transpile it to something like thing.a = 5.
Using this code below in my visitor, it tells me unexpected token .
VariableDeclarator: {
enter: function (path, state) {
path.replaceWith(
t.assignmentExpression(
'=',
t.memberExpression(
t.identifier('abc'),
t.identifier('def')
),
t.stringLiteral('xyz')
)
)
}
}
What am I not taking into account here?
What's the canonical way to accomplish this?
Turns out I was operating on a Declarator rather than a Declaration. So what I was doing was causing it to compile to something like var a.4 = 'def'. Naturally, that fails.
I am struggling to include! some automatically generated code into a module. My module looks like...
use libc::c_int;
mod nif_versions {
include!(concat!(env!("OUT_DIR"), "/nif_versions.snippet"));
}
... and nif_versions.snippet looks like ...
const NIF_MAJOR_VERSION: c_int = 2;
const NIF_MINOR_VERSION: c_int = 7;
But this gives me the following error:
/home/goertzen/ruster/target/build/ruster-7f1b6b5473eea720/out/nif_versions.snippet:1:26: 1:31 error: use of undeclared type name `c_int`
/home/goertzen/ruster/target/build/ruster-7f1b6b5473eea720/out/nif_versions.snippet:1 const NIF_MAJOR_VERSION: c_int = 2;
^~~~~
note: in expansion of include!
If I skip the include! and paste those 2 lines manually, everything works. Also, I had to wrap the include in a module to make it work at all. I would like to get rid of that if possible.
You just need to move use libc::c_int to the inner mod declaration because uses are only valid inside the module they are used:
mod nif_versions {
use libc::c_int;
include!(concat!(env!("OUT_DIR"), "/nif_versions.snippet"));
}