ESlint adds spaces inside array brackets, prettier removes them.
I use vscodium with ESLint and Prettier extensions. I run the command in package.json:
{
"lint": "eslint --fix ./index.js && prettier --write ./index.js"
}
.eslint.json:
{
"env": {
"browser": true,
"es2021": true
},
"extends": ["prettier"],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
"no-console": "off",
"array-bracket-spacing": ["error", "always"] // the rule
}
}
index.js:
const myTestFunction = () => {
const [num1] = [1, 3]; // the error
//const [ num1 ] = [ 1, 3 ]; // expected
return console.log(num1);
};
myTestFunction();
I solved that inside my CRA project by:
Install Create React App with Typescript
npx create-react-app my-app --template typescript
ESLint is already in CRA, no need to
install it.
Initialize it - npm init #eslint/config
Install Prettier
npm install --save-dev --save-exact prettier
Integrate it into eslint (I prefer
standard way) -
https://github.com/prettier/eslint-config-prettier#installation
npm install --save-dev eslint-config-prettier
Configure .eslintrc.json
Install Airbnb ESLint configs. It is
made for JS.
Install
eslint-config-airbnb-typescript
to bind it with typescript.
npm install --save-dev eslint-config-airbnb-typescript
Configure .eslintrc.json
Some rules are not clear for me now, so I look up and escape them.
For example
jsx-a11y/label-has-associated-control
"rules": {
"jsx-a11y/label-has-associated-control": [ "error", {
"required": {
"some": [ "nesting", "id" ]
}
}],
"jsx-a11y/label-has-for": [ "error", {
"required": {
"some": [ "nesting", "id" ]
}
}]
}
Add scripts to package.json
{
"format": "prettier --write src/**/*.{ts,tsx}",
"lint": "eslint --fix src/**/*.{ts,tsx}",
"test": "npm run format && npm run lint"
}
Profit!!! No more packages or settings are required.
Related
I'm trying to use typescript pre-commit Husky hooks with LintStaged, but when I do a commit, the pre-commit script is not running, I just receive the common git response after commits.
.huskyrc.json
{
"hooks": {
"pre-commit": "lint-staged"
}
}
.lintstagedrc.json
{
"*.ts": [
"eslint 'src/**' --fix",
"npm run test:staged"
]
}
My file structure
What am I doing wrong?
The another way of doing it is adding the scripts to the package.json file.
// package.json
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.ts": [
"eslint 'src/**' --fix",
"npm run test:staged"
],
}
In other threads I read that the issue might come when eslint and tsconfig.json are not in the same working folder, but I have all the files in the project's root folder. Somehow, after initializing the project, the thrown error seems to try to find tsconfig.json in the parent folder of the project (so, outside the project):
Let's say the project is in: '\parentFolder\ProjectRoot'
tsconfig is in the route: '\parentFolder\ProjectRoot\tsconfig.json
The eslint error I get (at the top of index.ts) is the following:
Parsing error: Cannot read file 'parentFolder\tsconfig.json'
Note that eslint is somehow looking for tsconfig in the wrong folder (the parent of the root folder)
The contents of \parentFolder\ProjectRoot are:
The steps to get to this point have been:
npm init
npm i -D typescript
add "tsc": "tsc" script to package.json
npm run tsc -- --init
npm i express
npm i -D eslint #types/express #typescript-eslint/eslint-plugin #typescript-eslint/parser
npm i -D ts-node-dev
.eslintrc:
{
"extends": [
"eslint:recommended",
"plugin:#typescript-eslint/recommended",
"plugin:#typescript-eslint/recommended-requiring-type-checking"
],
"plugins": ["#typescript-eslint"],
"env": {
"browser": true,
"es6": true
},
"rules": {
"#typescript-eslint/semi": ["error"],
"#typescript-eslint/explicit-function-return-type": 0,
"#typescript-eslint/no-unused-vars": [
"error", { "argsIgnorePattern": "^_" }
],
"#typescript-eslint/no-explicit-any": 1,
"no-case-declarations": 0
},
"parser": "#typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "ES6",
"outDir": "./build/",
"module": "commonjs",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"esModuleInterop": true
}
}
package.json (looks like changing index.js to index.ts in "main" does nothing)
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"tsc": "tsc",
"dev": "ts-node-dev index.ts",
"lint": "eslint --ext .ts ."
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#types/express": "^4.17.9",
"#typescript-eslint/eslint-plugin": "^4.9.0",
"#typescript-eslint/parser": "^4.9.0",
"eslint": "^7.14.0",
"ts-node-dev": "^1.0.0",
"typescript": "^4.1.2"
},
"dependencies": {
"express": "^4.17.1"
}
}
index.ts
(I get implicity errors here in req and res, I imagine caused by the fact that tsconfig can't be found).
const express = require('express');
const app = express();app.use(express.json());
const PORT = 3000;
app.get('/ping', (_req, res) => {
res.send('pong');
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
edit:
I had typescript installed globally (and also as a dev dependency in these projects). Thinking this might be the issue, I uninstalled the global version of typescript.
When checking all global packages with npm list -g --depth 0, I get the following:
I don't know if that would be related to the issue.
What worked for me was on the .eslintrc of the folder you are on to add:
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname // <-- this did the trick for me
}
I'm putting this answer here as an idiot-proofing measure. This can also happen if you open the project in the wrong folder. I got this error when I opened the parent directory of the project instead of the project directory itself. I had a project in GitLab -> projectFolder and opened GitLab. I got the error saying tsconfig.json could not be read.
Is there any way to either (a) disallow a node application from require/importing globally-installed npm modules, or (b) at least output a warning when a globally-installed module is used?
Reason being: I've repeatedly wound up in situations where a developer (myself included) incorporates a module in a node application but fails to add it to package.json because it happens to be globally installed on their machine, and therefore there's no error on the local machine; but upon deployment to a system that doesn't have that module installed globally, of course, it fails. Would be convenient to just ensure that all modules are in fact included in package.json.
ESLint Way
1) use husky module: npm install husky --save-dev
2) install eslint and it's deps (see package.json example below).
3) example package.json:
{
"name": "shopping-cart-estimator-test",
"version": "1.0.0",
"description": "",
"scripts": {
"start": "node ports/http.js",
"eslint-check": "./node_modules/eslint/bin/eslint.js .",
"eslint-fix": "./node_modules/eslint/bin/eslint.js . --fix",
"test": "./node_modules/.bin/mocha test --exit"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
"husky": "^4.2.5",
"mocha": "^7.1.2",
"eslint": "^7.0.0",
"eslint-config-import": "^0.13.0",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1"
},
"dependencies": {
"lodash": "^4.17.15"
},
"husky": {
"hooks": {
"pre-commit": "npm run eslint-check && npm test",
"pre-push": "npm run eslint-check && npm test"
}
}
}
4) create .eslintrc.js file with import/no-extraneous-dependencies rule (it requires eslint-plugin-import, make sure it's in package.json):
module.exports = {
"extends": "standard",
"parser": "babel-eslint",
"rules": {
"semi": ["error", "always"],
"no-unused-vars": 1,
"spaced-comment": ["warn"],
"no-trailing-spaces": ["warn"],
"comma-dangle": ["error", {
"arrays": "always",
"objects": "always",
"imports": "never",
"exports": "never",
"functions": "never"
}],
"space-before-function-paren": ["error", {
"anonymous": "always",
"named": "never",
"asyncArrow": "always"
}],
"import/no-extraneous-dependencies": ["error", {"packageDir": __dirname}],
},
"overrides": [{
"files": ["spec/tests/*.js", "spec/tests/**/*.js"],
"rules": {
"no-unused-expressions": 0,
"no-unused-vars": 1
}
}]
};
"Custom Script way"
1) use husky module: npm install husky --save-dev
2) add hook to pre-commit, pre-push in package.json:
"husky": {
"hooks": {
"pre-commit": "npm test && node scripts/check-deps.js",
"pre-push": "npm test && node scripts/check-deps.js"
}
}
3) install dependency-tree: npm i --save dependency-tree
4) write scripts/check-deps.js script that will find external (non package.json) dependencies and if they exist will:
console.warn('Found external dependency');
process.exit(-1);
I have a Nodejs project which uses ESLint to keep consistency.
On my Mac machine, I have no troubles all works bur on Windows I got this error
No files matching the pattern "'./*'" were found.
Please check for typing mistakes in the pattern.
My setup for ESLint is
{
"env": {
"es6": true,
"node": true
},
"extends": [
"plugin:prettier/recommended",
"airbnb-base"
],
"plugins": [
"prettier"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
"prettier/prettier": "error",
"linebreak-style": "off"
}
}
Package.json
{
"name": "new-architecture-solution",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"prod": "node -r esm server.js",
"dev": "nodemon -r esm server.js",
"debug": "ndb nodemon -r esm server.js",
"lint": "eslint . --ext .js,.jsx --quiet",
"fix": "eslint './*' --fix",
"prettier": "prettier --write src/**/*.{js,css}"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"eslintIgnore": [
"package.json",
"package-lock.json",
"combined.log",
"swagger.json",
"README.md"
],
"lint-staged": {
"./**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
"npm run prettier",
"npm run lint --color",
"npm run fix",
"git add"
]
},
I'm unable to find asolution and I would like to have it work in both my machines
I haven't tried it yet on Windows, but according to this post replacing the single quotes with \" might do the trick. I've tried it on my Mac and it seems to work properly.
Edit: confirmed to work on Windows machines as well.
I am using babel-cli for jsx and es6 features to transpile
i have changed my build command
from
"build": "node build",
to
"build": "babel-node build",
Everything was working fine before
But when i run the build command i get this error
Error: locals[0] does not appear to be a module object with Hot Module replacement API enabled. You should disable react-transform-hmr in production by using env section in Babel configuration. See the example in README: https://github.com/gaearon/react-transform-hmr
See the screenshot also
So i should disable react-transform-hmr in production by using env section in Babel configuration
and this is my .babelrc like the instructions
{
"presets": ["react", "es2015"],
"env": {
"development": {
"plugins": [
["transform-object-rest-spread"],
["transform-react-display-name"],
["react-transform", {
"transforms": [{
"transform": "react-transform-hmr",
"imports": ["react"],
"locals": ["module"]
}, {
"transform": "react-transform-catch-errors",
"imports": ["react", "redbox-react"]
}]
}]
]
},
"production": {
"plugins": [
["transform-object-rest-spread"],
["transform-react-display-name"]
]
}
}
}
what am i doing wrong? Any recommendation?
I had to add NODE_ENV=production to the command
"build": "NODE_ENV=production babel-node build"