import { A, B, C } from './file';
console.log('No imports used');
export const D = 4;
tslint.json
๊ตฌ์ฑ:
{
"rules": {
"no-unused-variable": [true, {"ignore-pattern": "^_"}]
}
}
The 'no-unused-variable' rule threw an error in '/Users/andrew.mitchell/Documents/Projects/test/test.ts':
TypeError: Cannot read property '1' of null
at walk (/Users/andrew.mitchell/Documents/test/node_modules/tslint/lib/rules/noUnusedVariableRule.js:105:54)
at Rule.AbstractRule.applyWithFunction (/Users/andrew.mitchell/Documents/test/node_modules/tslint/lib/language/rule/abstractRule.js:39:9)
at Rule.applyWithProgram (/Users/andrew.mitchell/Documents/test/node_modules/tslint/lib/rules/noUnusedVariableRule.js:32:21)
at Linter.applyRule (/Users/andrew.mitchell/Documents/test/node_modules/tslint/lib/linter.js:194:29)
at /Users/andrew.mitchell/Documents/test/node_modules/tslint/lib/linter.js:139:85
at Object.flatMap (/Users/andrew.mitchell/Documents/test/node_modules/tslint/lib/utils.js:151:29)
at Linter.getAllFailures (/Users/andrew.mitchell/Documents/test/node_modules/tslint/lib/linter.js:139:32)
at Linter.lint (/Users/andrew.mitchell/Documents/test/node_modules/tslint/lib/linter.js:99:33)
at /Users/andrew.mitchell/Documents/test/node_modules/tslint/lib/runner.js:209:32
at step (/Users/andrew.mitchell/Documents/test/node_modules/tslint/node_modules/tslib/tslib.js:133:27)
๊ท์น์ 1ํ์ ๋ํด All imports in import declaration are unused.
๋ฅผ ๊ฒฝ๊ณ ํด์ผ ํฉ๋๋ค.
์ด๊ฒ์ failure
๋ฉ์์ง๊ฐ ์ ๊ท์์์ ์ฐพ์ ๋ณ์ ์ด๋ฆ์ด ์๋ All imports in import declaration are unused.
์ด๊ธฐ ๋๋ฌธ์ noUnusedVariableRule.ts#L123 ์ ์ํด ๋ฐ์ํฉ๋๋ค.
์ด ๊ท์น์ ignore-pattern
์ต์
์ ์ง์ ํ์ง ์์ผ๋ฉด ๋ณ์ ์ด๋ฆ์ ํ์ธํ์ง ์๊ธฐ ๋๋ฌธ์ ์ฌ๋ฐ๋ฅด๊ฒ ์๋ํฉ๋๋ค.
@hotforfeature๋ฅผ ์ ์ฐพ์์ต๋๋ค. https://github.com/palantir/tslint/pull/3919 ์์ ์ด ๋ฌธ์ ๋ฅผ ๋ค๋ฃจ๊ธฐ ์์ํ์ต๋๋ค. ์ด ๋ฌธ์ ๋ฅผ ์ ํ์ํฌ TS 2.9์ ์ผ๋ถ ๋ณ๊ฒฝ ์ฌํญ๊ณผ ๊ด๋ จ์ด ์๊ธฐ ๋๋ฌธ์ ๋๋ค.
๋ด PR์ ์์ธ๊ฐ ๋ฐ์ํ๋ ๊ฒ์ ๋ฐฉ์งํ์ง๋ง ์ธ๊ธํ ๊ฒฝ์ฐ ignorePattern
๋ฅผ ๋ฌด์ํ๋ ๊ฒ์ ์ข์ง ์์ต๋๋ค. ์ง๊ธ ๋น์ฅ import autofixs๋ฅผ ์ํด ํ๋ ๊ฒ๊ณผ ๊ฐ์ ๋ณต์กํ ์ฝ๋๋ฅผ ์ถ๊ฐํด์ผ ํ๋ค๊ณ ์๊ฐํฉ๋๋ค./
์ด ๊ท์น์ ๋ ์ด์ ์ฌ์ฉํ์ง ์์์ผ ํ๋์ง ๊ถ๊ธํฉ๋๋ค. tsc๋ ์ด์ ๊ฒฝ๊ณ ๋ฅผ ๋ฌด์ํ๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๊ณ IDE๋ฅผ ํตํด ๊ฒฝ๊ณ ์ ๋ํ ์๋ ์์ ์ ์ง์ํฉ๋๋ค.
๋ด๊ฐ ๋ณผ ์ ์๋ ํ, ์ด ๊ท์น์ ์ฃผ์ ์ด์ ์ ignore-pattern
์ด์ง๋ง, ์ด์ ๊ท์น์ด tsc ์ง๋จ์ ์์กดํ๋๋ก ์์ฑ๋ ๋ฐฉ์์ผ๋ก ์ฌ๋ฐ๋ฅด๊ฒ ๊ตฌํํ๊ธฐ๊ฐ ์ด๋ ต์ต๋๋ค. TSLint๋ ๋ํ ํน์ ํ์ผ์์ ๊ท์น์ ๋นํ์ฑํํ๋ ๊ฒ๊ณผ ๊ด๋ จํ์ฌ tsc๋ณด๋ค ์ฝ๊ฐ ๋ ์ ์ฐํฉ๋๋ค. @suchanlee ์ด ๋ชจ๋
์ด ๊ท์น์ ์ฃผ์ ์ฌ์ฉ ์ฌ๋ก๋ lintingํ ๋ git hook์์ ์ปค๋ฐ ์ ๋ฌธ์ ๋ฅผ ์๋ ์์ ํ๋ ๊ฒ์ ๋๋ค. ๋ฐ๋ผ์ tsc๊ฐ IDE ํตํฉ์ ํตํด ๋ฌด์ธ๊ฐ๋ฅผ ์ ๊ณตํ๋๋ผ๋ ์ฌ๊ธฐ์๋ ์ฌ์ ํ ํฐ ๊ฐ์น๊ฐ ์๋ค๊ณ ์๊ฐํฉ๋๋ค.
@JKillian ์ ์๊ฐ์๋ ์ฌ๋ฌ ๋ฒ์ ์ Typescript์ ๋ํ TSLint์ ์ง์๊ณผ Typescript ๋์์ ์ง์์ ์ธ ๋ณ๊ฒฝ์ ์ง์ํ๊ธฐ ์ํด ๊ท์น์ด ์ ์ ๋ ๋น์ธ์ง๊ธฐ ์์ํ๋ค๊ณ ์๊ฐํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ Typescript๊ฐ ์ด ๊ธฐ๋ฅ์ ์ง์ํ๊ธฐ ๋๋ฌธ์ ์ฐ๋ฆฌ๋ ๊ทธ๊ฒ์ ์ฌ์ฉํ๋ ์ชฝ์ผ๋ก ๊ธฐ์ธ์ด์ผ ํฉ๋๋ค. ํ์ง๋ง ์ด์ ๋ฒ์ ์ TS์์๋ ์ง์ํ์ง ์๊ธฐ ๋๋ฌธ์ ์ญ์ ํด์ผ ํ๋ค๊ณ ์๊ฐํ์ง ์์ต๋๋ค. ๊ทธ๋ฌ๋ TS์์ ํ์ฌ TSLint ์ํฌํ๋ก๋ฅผ ๋ ์ ์ง์ํ๊ธฐ ์ํด ๊ท์น์ ์ฌ์ฉํ์ง ์๋ ๊ฒ์ ๋ํด ์๊ฐํ๊ณ TS ๋ด๋น์์ ํ๋ ฅํด์ผ ํ๋ค๊ณ ์๊ฐํฉ๋๋ค.
๋๋ @suchanlee ์ ์ ์์ ๋ ๋ฒ์งธ๋ก ํ ๊ฒ์ ๋๋ค. ์ด ๊ท์น์ ๋ฌธ์ ์์ ๋ง์ด ๋์ค๋ ๊ฒ ๊ฐ์ต๋๋ค. ์ต๊ทผ TS ๋ฒ์ ์ด ๋ฌธ์ ์ ๋ํด ์ ์ ํ๊ฒ ๊ฒฝ๊ณ ํ๋ค๋ ์ ์ ๊ณ ๋ คํ๋ฉด ๋๋ฌด ๋ง์ต๋๋ค. ๋จ๊ณ์ ์ผ๋ก ์์ ์!
@hotforfeature - ์ฌ์ฉ
์ธ์ ๋์ฒ๋ผ ๋๊ตฌ๋ ์ง ๊ท์น์ ์์ค ์ฝ๋๋ฅผ ๋ณต์ฌํ์ฌ ์ธ๋ถ ํจํค์ง๋ก ์ฎ๊ธฐ๊ณ ๊ณ์ ์ฌ์ฉ/๊ฐ์ ํ ์ ์์ต๋๋ค!
๊ฐ์น ์๋ ์ผ:
{
"compilerOptions": {
"noUnusedLocals": true,
"noUnusedParameters": true,
}
}
tsconfig
์์ ์์ ๋ด์ฉ์ ์ฌ์ฉํ๋ฉด no-unused-variable
๋ฆฐํธ ๊ท์น์ ๋นํ์ฑํํ ์ ์์ต๋๋ค.
no-unused-variable
๋ ์ด์ ๋ ์ด์ ์ฌ์ฉ๋์ง ์์ผ๋ฉฐ ์์ compilerOptions
๋ ์ด์ ๊ณต์ ์๋ฃจ์
์
๋๋ค.
noUnusedLocals
๋ฐ noUnusedParameters
๋ฅผ compilerOptions
๊ฒ์ tslint์ no-unused-variable
์ ์์ ํ ๋์ผํ์ง ์์ต๋๋ค. ํ์ฌ๋ก์๋ ์ฌ์ฉํ์ง ์์ ๋ณ์๊ฐ ๋น๋๋ฅผ ์ค๋จ์ํค๊ฑฐ๋ ๋์ ๋์ง ์๊ฒ ์ ์ง๋๋ฉฐ ๋ ์ด์ ๊ฒฝ๊ณ ๋ชจ๋๊ฐ ์์ต๋๋ค ๐
@giladgray , @killtheliterate ๋ ๋ค์๊ณผ
tsconfig์์ ์์ ์ฌํญ์ ์ฌ์ฉํ๋ฉด no-unused-variable ๋ฆฐํธ ๊ท์น์ ๋นํ์ฑํํ ์ ์์ต๋๋ค.
@kachkaev๊ฐ ์ด ๊ฒ์ฒ๋ผ ์ด ๊ท์น์ ๋นํ์ฑํํ๊ณ ์ถ์ง ์์ต๋๋ค.
noUnusedLocals
๋ฐnoUnusedParameters
๋ฅผcompilerOptions
๊ฒ์ tslint์no-unused-variable
์ ์์ ํ ๋์ผํ์ง ์์ต๋๋ค.
๋ด ์ฌ์ฉ ์ฌ๋ก๋ ์์ฑ๋ ์ฝ๋์ ๋๋ค. ์๋์ผ๋ก ์์ฑํ ์ฝ๋๊ฐ ์ด ๊ท์น์ ๋ฐ๋ฅด๊ธฐ๋ฅผ ์ํ์ง๋ง ์ฝ๋ ์์ฑ๊ธฐ๋ฅผ ์์ฒญ๋๊ฒ ๋ณต์กํ๊ฒ ๋ง๋๋ ๊ฒ์ ํผํ๊ธฐ ์ํด ์์ฑ๋ ์ฝ๋(๋๋ ์ ์ด๋ ์์ฑ๋ ์ฝ๋์ ์ผ๋ถ ์น์ ์์)์์ ์ด ๊ท์น์ ๋นํ์ฑํํ๊ณ ์ถ์ต๋๋ค. TypeScript ์ปดํ์ผ๋ฌ ์ต์ ์ผ๋ก ์ํํ ์ ์์ต๋๋ค ๐
์ต์ํ ์ด ์ง์ ์ค๋จ์ TSLint ์น์ฌ์ดํธ ์ ๋ฌธ์ํ๋์ด์ผ ํ์ง๋ง ์ด ์ง์ ์ค๋จ์ด ์๊ธฐ์์กฐ์๋ค๋ ๋ฐ ๊ฐ๋ ฅํ ๋์ํฉ๋๋ค. ์ฌ๊ฒํ ํ ๊ณํ์ด ์์ต๋๊น? ์ปค๋ฎค๋ํฐ ๊ธฐ๋ถ๊ฐ ํ์ฉ๋ฉ๋๊น?
์ด ๋น์ถ์ฒ์ ์ฐ์ค๊ฝ์ค๋ฝ์ต๋๋ค. ์ปดํ์ผ๋ฌ ํ๋๊ทธ๋ TERRIBLE ๋์ฒดํ์
๋๋ค. ๊ทธ๋ค์ ๋น๋๋ฅผ ๊นจ๊ณ ์ค์ค๋ก ๊ณ ์น ์ ์์ต๋๋ค. ๊ทธ๊ฒ์ด ์ด๋ป๊ฒ ์ฌ๋๋ค์ ๊ฐ๋ฆฌํค๋ ํฉ๋ฆฌ์ ์ธ ํด๊ฒฐ์ฑ
์
๋๊น? ignore-pattern
์ต์
์ด ์ง๊ธ ์๋ํ๊ธฐ ๋๋ฌด ์ด๋ ต๋ค๋ฉด ๋์ ์ฌ์ฉํ์ง ๋ง์ญ์์ค.
๐ ์ฌ๋ฌ๋ถ - ์ด๊ฒ์ https://github.com/palantir/tslint/issues/4232์ ์ฐ๊ฒฐํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค no-unused-variable
๋ํ ์ถฉ๋ถํ ๋์ฒดํ์ด ์๋๋ผ๋ ๊ฒ์ ์ ์ดํด๋๊ณ ๋์ํฉ๋๋ค. ๊ท์น์ ์๋ ๊ตฌํ์ ๋ค๋ฅธ TS ๋๊ตฌ์ ์ ์๋ํ์ง ์์ ๋นํ์ฑํํด์ผ ํ์ต๋๋ค. #4232๋ ๋ค์ ํ์ฑํํ๋ ๋ฐฉ๋ฒ์ ์ฐพ๋ ๊ณผ์ ์ ์ถ์ ํฉ๋๋ค.
๊ทธ ๋์ tsc --noEmit --noUnusedLocals --noUnusedParameters
์ ๊ฐ์ ๊ฒ์ ๋ณ๋์ ๋๊ตฌ๋ก ์ฌ์ฉํ์ฌ ๋ด์ฅ ์ํ๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์, ๊ตฌ์ฑ ๊ฐ๋ฅํ no-unused-variables
๋งํผ ์ข์ง ์์ต๋๋ค.
#4100 ๋ฐ #4232์์ ๊ณ์ ํ ๋ก
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
noUnusedLocals
๋ฐnoUnusedParameters
๋ฅผcompilerOptions
๊ฒ์ tslint์no-unused-variable
์ ์์ ํ ๋์ผํ์ง ์์ต๋๋ค. ํ์ฌ๋ก์๋ ์ฌ์ฉํ์ง ์์ ๋ณ์๊ฐ ๋น๋๋ฅผ ์ค๋จ์ํค๊ฑฐ๋ ๋์ ๋์ง ์๊ฒ ์ ์ง๋๋ฉฐ ๋ ์ด์ ๊ฒฝ๊ณ ๋ชจ๋๊ฐ ์์ต๋๋ค ๐