eslint - ignore unused vars in constructor - eslint

I'm using nest js, and this pattern in the constructor is quite common:
constructor(private var : myType) {}
I've got a rule in eslint that says that unused vars are error:
'no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
can I ignore unused vars in the constructor only? I tried something like this:
'no-unused-vars': ['error', { argsIgnorePattern: '^private ' }], // notice the space
but, it doesn't seem to work. Not quite sure how to specify this. I'd like to ignore unused vars for both private and public args in the constructor.

Related

NestJs unit test crashes due to decorators

I have a NestJS Jest-based unit test that keep crashing due to some parameter-scoped decorators somewhere in the import tree, for example, in this case, I had some methods whose first parameter had a #Args decorator from Graphql:
async getUserById(#((0, _graphql.Args)('id'))
^
SyntaxError: Invalid or unexpected token
If I try to enable babel-plugin-parameter-decorator in babel.config, it starts breaking other things, for example, it stops recognizing #Prop types correctly on my schemas, or throws a message-less "TypeError" when I try to reference my schema's .name property like here:
// my.service.ts:
#Injectable
export class MyService {
private logger = new Logger(MyService.name)
constructor(
#InjectModel(MyModel.name) private myModel: Model<MyModel>,
) {
}
}
Any idea how I should get jest to work?
as jay-mcdoniel hinted, that was the problem. I was not using ts-jest. Had to add to package.json's jest section:
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},

Linting to detect unused exported variables

I'm trying to figure out how to detect when a exported variable (const, function, ...) is not used and therefore could be deleted. I have a React App with ESLint configured.
In the following example, MAGIC_NUMBER is exported and used in file2.js but doMagic is never imported or used in any other file.
file1.js
export const MAGIC_NUMBER = 7;
file2.js
import { MAGIC_NUMBER } from "./file1.js"
export function doMagic() {
return MAGIC_NUMBER + 1;
}
I would like to know if there is any way to detect that doMagic is unused.
Right now, I'm using ESLint with the the default rule :
"no-unused-vars": ["error", { "vars": "all", "args": "after-used", "ignoreRestSiblings": false }]
It detects unused variables inside the same file, but not across multiple files.
Thank you very much!

Declare interface in TypeScript .d.ts file and consume from JavaScript file

I'm using Visual Studio Code and would like to have intellisense hint when declaring my configuration variable as plain javascript object
jsconfig.json
{
"compilerOptions": {
"checkJs": true
},
"include": [
"src/**/*.js",
"types/**/*.d.ts"
],
"exclude": [
"node_modules"
]
}
types/index.d.ts
interface Foo {
a: string;
b: number;
}
declare var fooConfig: Foo;
src/app.js
const fooConfig = {
a: 'hello',
b: 123
}
I expect when declaring using const fooConfig VS Code might offer intellisense about a and b, the current result I got complaint message about re-declaring the variable fooConfig
P.S. I don't really know the possibility but I would like to have some intellisense so I can declare my configuration variable easily
Please guide
Thanks
You can use the JSDoc syntax to describe types in JS files:
src/app.js
/** #type {Foo} */
const fooConfig = {
a: 'hello',
b: 123
}
It works implicitly with d.ts files you have in your project, so there is no need to "include" them and there is no need to declare the var in the d.ts file.
Try const fooConfig: Foo = {...} in your JS file, after importing the interface. As it stands, TS is right you're declaring it twice.

require.js shim, export myOwnGlobal name

I'm not sure about the use of "exports" on shim config, following the example on the requireJS API, I can use Backbone (B in capital letter) to export it to a global scope.
This means that it will be a window object property.
But I realized that I'm forced to use that name, and I can't export it by other reference name, ie: "MyGlobalBackbone"
require.config({
paths: {
backboneAlias:'backbone'
},
shim : {
backboneAlias : {
deps : [ 'underscore', 'jquery-1.9.1' ],
exports : 'MyGlobalBackbone'
}
}
});
require(['backboneAlias'],function(backboneAsAliasDependency){
console.log(backboneAsAliasDependency);//Loaded Ok
console.log(MyGlobalBackbone); //Uncaught ReferenceError: MyGlobalBackbone is not defined
});
This code only works if I use "Backbone" instead of "MyGlobalBackbone"...
Actually you got it the other way around: shimming doesn't export a variable to global scope, it imports it FROM the global scope. The name ("Backbone") was set by Backbone's author, and this is the part you're explaining to RequireJS in shim config element.
See it in the API:
http://requirejs.org/docs/api.html#config-shim
Look at this sentence:
//Once loaded, use the global 'Backbone' as the
//module value.
Let's see it in that way, you will understand it:
//Once loaded, use a global variable 'Backbone' that defined by the backbone vendor as the
//module value.
You should use map to make an alias.
require.config({
paths: {
...
},
shim : {
...
},
map: {
'*': {
'MyGlobalBackbone': 'Backbone'
}
}
});
This will allow you to use MyGlobalBackbone instead of Backbone for all (*) modules.

Having trouble defining global var in require.js

I'm trying to define a global object that i can reference across all of my modules. however, in the modules, i am unable to reference my path, and it's saying that "g" does not exist.
In main1.js, i have this:
requirejs.config({
paths: {
Underscore: 'lib/underscore/1.3.3/underscore.min',
Backbone: 'lib/backbone/0.9.2/backbone.min',
Globals: 'lib/backbone/ globalVars'
}
});
require([ 'views/pages', 'views/filters'], function(allPages, filters) {
filters.render();
allPages.render();
});
inside globalVars.js, i have this:
(function() {
var Globals = {
isDemo: false
}
console.log('in globalvars') // this shows in my console
}).call(this);
and finally, inside of view/pages.js, i have this:
define([
'Globals',
'Underscore',
'Backbone'
], function(g, _, Backbone){
console.log(g.isDemo) //<-- returns "TypeError: g is undefined"
If i use a define inside my main1.js like this:
define( 'Globals', function() {
return {
isDemo: true
}
})
it works just fine. I haven't had much luck with trying to figure out why this is not working. I'd like to be able to just include a path to the globalVars rather than boilerplate pasting a define block in each and every module that needs it, since changing isDemo to false would require updating many other module pages (main2.js, main3.js, etc) as well. thanks!
Well, to start with, your globalVars.js is not in the module pattern, so requirejs doesn't know what you're trying to register as the module. If you change that file to use the pattern, like the define you added to main1.js, you should be all set. Is there a reason you aren't defining it as a module?

Resources