avoid_js_rounded_ints: JavaScript에서 값이 반올림되는 정수를 피하세요
avoid_js_rounded_ints: JavaScript에서 값이 반올림되는 정수를 피하세요
avoid_js_rounded_ints는 JavaScript로 컴파일했을 때 정확히 표현되지 못하는 정수 리터럴을 쓰지 말라고 알려주는 린트(lint) 규칙이에요. 코드를 JavaScript로 변환했을 때 값이 살짝 바뀌는 문제를 미리 잡아줘요.
본문
상세 설명
JavaScript로 정확히 표현할 수 없는 정수 리터럴은 피하세요. 프로그램을 JavaScript로 컴파일하면 int와 double이 모두 JavaScript의 Number가 돼요. 너무 큰 정수(value < Number.MIN_SAFE_INTEGER 또는 value > Number.MAX_SAFE_INTEGER)는 가장 가까운 Number 값으로 반올림될 수 있어요.
예를 들어 1000000000000000001은 JavaScript Number로 정확히 표현할 수 없어서, 대신 1000000000000000000이 사용돼요.
BAD:
int value = 9007199254740995;
GOOD:
BigInt value = BigInt.parse('9007199254740995');
활성화하기
avoid_js_rounded_ints 규칙을 활성화하려면 analysis_options.yaml 파일의 linter > rules 아래에 avoid_js_rounded_ints를 추가해요.
linter:
rules:
- avoid_js_rounded_ints
대신 YAML map 문법으로 린터 규칙을 설정한다면, linter > rules 아래에 avoid_js_rounded_ints: true를 추가해요.
linter:
rules:
avoid_js_rounded_ints: true