TIL

0l0lยท2021๋…„ 9์›” 7์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
71/86

1. ESLint ๊ตฌ๋ฌธ ๋ถ„์„ ์˜ค๋ฅ˜

๐Ÿ“Œ Unexpected token

ESLint error

ESLint ๊ตฌ๋ฌธ ๋ถ„์„์—์„œ ์˜ˆ๊ธฐ์น˜ ์•Š์€ ํ† ํฐ ์˜ค๋ฅ˜๋Š” JavaScript ES6~7์˜ ์ง€์†์ ์ธ ๋ณ€๊ฒฝ์œผ๋กœ ์ธํ•ด ๊ฐœ๋ฐœ ํ™˜๊ฒฝ๊ณผ ESLint์˜ ํ˜„์žฌ ๊ตฌ๋ฌธ ๋ถ„์„ ๊ธฐ๋Šฅ ๊ฐ„์˜ ๋น„ํ˜ธํ™˜์„ฑ์œผ๋กœ ์ธํ•ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

ESLint๋ฅผ ํ˜ธํ™˜ ๊ฐ€๋Šฅํ•œ ํŒŒ์„œ(์˜ˆ: @babel/eslint-parser ๋˜๋Š” babel-eslint v7 ์ดํ•˜ babel ๋ฒ„์ „์šฉ)๋กœ ๊ตฌ๋ฌธ ๋ถ„์„์„ ํ†ตํ•ด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
1. Terminal์— npm install @babel/eslint-parser --save-dev ์‹คํ–‰
2. .eslintrc ํŒŒ์ผ์— "parser": "@babel/eslint-parser" ์ถ”๊ฐ€

*์ฐธ๊ณ  ์‚ฌ์ดํŠธ: ESLint Parsing error: Unexpected token

2. ESLint Babel ์˜ค๋ฅ˜

eslint babel ์˜ค๋ฅ˜

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์„ ๋•Œ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค.

๐Ÿ“Œ const์— ๋Œ€ํ•œ ์˜ค๋ฅ˜

The 'const' modifier can only be used in TypeScript files.ts(8009)

๐Ÿ“Œ validator์— ๋Œ€ํ•œ ์˜ค๋ฅ˜

Parsing error: Unexpected token

const validator = {
    id: input => input.length >= 10,
    email: input => {
      const regExp = /^[a-zA-Z\d+-.]+@[a-zA-Z\d+-.]+\.[a-zA-Z]{2,3}$/;
      return regExp.test(input);
    },
    password: input => {
      const regExp =
        /^(?=.*[A-Za-z])(?=.*\d)(?=.*[!@#$%^&*-_])[A-Za-z\d!@#$%^&*-_]{10,}$/;
      return regExp.test(input);
    },
  };

const ์˜ˆ์•ฝ์–ด๋ฅผ ์‚ญ์ œํ–ˆ์„ ๋ฟ์ธ๋ฐ ํ•ด๊ฒฐ..?
1๋ฒˆ์—์„œ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€๋กœ ์ž‘์„ฑํ•œ ์„ค์ • ๋‹ค์‹œ ๋˜๋Œ๋ฆผ.
์ด์œ ๋Š” ๋‚˜์ค‘์— ์•Œ๋ ค์ฃผ์‹ ๋‹ค ํ•จ..

3. request-response ๋ฆฌํŒฉํ† ๋ง

๋กœ๊ทธ์ธ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ์ถœ๋ ฅ๋˜๋Š” alert ๋ฉ”์‹œ์ง€๋ฅผ messages ๊ฐ์ฒด๋กœ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.
response message(key)์— ๋”ฐ๋ผ ํ•ด๋‹นํ•˜๋Š” ๋ฉ”์‹œ์ง€(value)๊ฐ€ ์ถœ๋ ฅ๋˜๋„๋ก if-else ์กฐ๊ฑด๋ฌธ์„ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.
(์ฐธ๊ณ : key๋กœ ์„ค์ •ํ•œ ๋ฉ”์‹œ์ง€๋Š” ๋ฐฑ์—”๋“œ์—์„œ ์„ค์ •ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.)

const messages = {
  LOGIN_SUCCESS: `๋กœ๊ทธ์ธ์„ ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค. ${this.state.id}๋‹˜ ๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค!`,
  INVALID_USER: "์ž˜๋ชป๋œ ํšŒ์›์ •๋ณด์ž…๋‹ˆ๋‹ค. ์•„์ด๋””๋ฅผ ๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.",
  INVALID_PASSWORD: "์ž˜๋ชป๋œ ํšŒ์›์ •๋ณด์ž…๋‹ˆ๋‹ค. ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.",
};

if (res.message === "LOGIN_SUCCESS") {
  alert(messages[res.message]);
} else if (res.message === "INVALID_USER") {
  alert(messages[res.message]);
} else if (res.message === "INVALID_PASSWORD") {
  alert(messages[res.message]);
}

๐Ÿ“Œ Refactoring
alert(messages[res.message])๋งŒ ์žˆ์–ด๋„ response message์— ๋”ฐ๋ฅธ ๊ฐ’์„ ๊ฐ๊ฐ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

alert(messages[res.message]);

res๋งŒ์œผ๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์—†๊ณ  res.message๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
(res.MESSAGE์™€ ๊ฐ™์ด ๋Œ€๋ฌธ์ž๋กœ ์ž‘์„ฑํ–ˆ์„ ๋•Œ์™€์˜ ์ฐจ์ด๋Š” ๋ฌด์—‡์ผ๊นŒ?)

profile
์ฒœ๋ฐฉ์ง€์ถ• ๋น™๊ธ€๋น™๊ธ€

0๊ฐœ์˜ ๋Œ“๊ธ€