@ttsc/lint 0.18.4 → 0.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +125 -43
- package/go.mod +5 -4
- package/internal/rulecode/rulecode.go +88 -0
- package/lib/index.d.ts +10 -1
- package/lib/index.js +45 -17
- package/lib/index.js.map +1 -1
- package/lib/structures/ITtscLintConfig.d.ts +3 -0
- package/lib/structures/TtscLintRuleSetting.d.ts +3 -2
- package/lib/structures/format/ITtscLintFormat.d.ts +3 -2
- package/lib/structures/format/ITtscLintFormatSortImports.d.ts +20 -4
- package/lib/structures/rules/ITtscLintBoundariesRuleOptions.d.ts +80 -8
- package/lib/structures/rules/ITtscLintBoundariesRules.d.ts +6 -8
- package/lib/structures/rules/ITtscLintContributorRules.d.ts +18 -2
- package/lib/structures/rules/ITtscLintCoreRuleOptions.d.ts +347 -0
- package/lib/structures/rules/ITtscLintCoreRuleOptions.js +3 -0
- package/lib/structures/rules/ITtscLintCoreRuleOptions.js.map +1 -0
- package/lib/structures/rules/ITtscLintCoreRules.d.ts +101 -67
- package/lib/structures/rules/ITtscLintRegexpRules.d.ts +7 -2
- package/lib/structures/rules/ITtscLintRuleOptionsMap.d.ts +53 -7
- package/lib/structures/rules/ITtscLintRules.d.ts +6 -2
- package/lib/structures/rules/ITtscLintSolidRules.d.ts +0 -8
- package/lib/structures/rules/ITtscLintTypeScriptRuleOptions.d.ts +206 -0
- package/lib/structures/rules/ITtscLintTypeScriptRuleOptions.js +8 -0
- package/lib/structures/rules/ITtscLintTypeScriptRuleOptions.js.map +1 -0
- package/lib/structures/rules/ITtscLintTypeScriptRules.d.ts +55 -42
- package/lib/structures/rules/ITtscLintUnicornRuleOptions.d.ts +276 -0
- package/lib/structures/rules/ITtscLintUnicornRuleOptions.js +3 -0
- package/lib/structures/rules/ITtscLintUnicornRuleOptions.js.map +1 -0
- package/lib/structures/rules/ITtscLintUnicornRules.d.ts +44 -36
- package/lib/structures/rules/index.d.ts +3 -0
- package/lib/structures/rules/index.js +3 -0
- package/lib/structures/rules/index.js.map +1 -1
- package/linthost/ast_helpers.go +521 -23
- package/linthost/ast_selector_matcher.go +1628 -0
- package/linthost/ast_selector_parser.go +628 -0
- package/linthost/comment_scan.go +154 -0
- package/linthost/compile.go +129 -79
- package/linthost/config.go +352 -88
- package/linthost/config_format.go +15 -1
- package/linthost/contrib_adapter.go +47 -8
- package/linthost/declaration_rules.go +12 -11
- package/linthost/directives.go +9 -67
- package/linthost/dispatch.go +37 -3
- package/linthost/engine.go +368 -53
- package/linthost/escape_string.go +77 -0
- package/linthost/fix.go +133 -23
- package/linthost/flags_gen.go +18 -17
- package/linthost/format.go +73 -167
- package/linthost/format_editor_settings.go +134 -26
- package/linthost/grapheme_tables_gen.go +1920 -0
- package/linthost/host.go +159 -61
- package/linthost/literal_escape_scan.go +117 -0
- package/linthost/lsp.go +545 -98
- package/linthost/numeric_literal_precision.go +228 -0
- package/linthost/polyfill_browserslist.go +1439 -0
- package/linthost/polyfill_browserslist_config.go +603 -0
- package/linthost/polyfill_corejs.go +750 -0
- package/linthost/polyfill_data.go +113 -0
- package/linthost/polyfill_data_gen.json +39868 -0
- package/linthost/print_nodes_array.go +7 -1
- package/linthost/print_nodes_object.go +7 -1
- package/linthost/project_engine.go +231 -0
- package/linthost/project_rules.go +78 -0
- package/linthost/regex_clean.go +191 -0
- package/linthost/regex_tree.go +1544 -0
- package/linthost/regex_tree_optimizer.go +1685 -0
- package/linthost/regexp_cache.go +44 -0
- package/linthost/rule_codes.go +105 -0
- package/linthost/rule_codes.json +745 -0
- package/linthost/rules_ban_ts_comment.go +312 -0
- package/linthost/rules_boundaries.go +128 -43
- package/linthost/rules_boundaries_dependencies.go +1013 -0
- package/linthost/rules_core_extra.go +62 -92
- package/linthost/rules_cypress.go +1 -1
- package/linthost/rules_default_case.go +87 -11
- package/linthost/rules_dupes.go +38 -13
- package/linthost/rules_empty.go +188 -20
- package/linthost/rules_escape.go +62 -48
- package/linthost/rules_format_arrow_parens.go +19 -5
- package/linthost/rules_format_bracket_spacing.go +1 -1
- package/linthost/rules_format_clause_join.go +1 -1
- package/linthost/rules_format_declaration_header.go +31 -19
- package/linthost/rules_format_indent.go +1 -1
- package/linthost/rules_format_jsdoc.go +1 -1
- package/linthost/rules_format_orphan_semi.go +1 -1
- package/linthost/rules_format_parameter_properties.go +17 -7
- package/linthost/rules_format_print_width.go +1 -1
- package/linthost/rules_format_quote_props.go +1 -1
- package/linthost/rules_format_quotes.go +1 -1
- package/linthost/rules_format_semi.go +106 -79
- package/linthost/rules_format_sort_imports.go +79 -45
- package/linthost/rules_format_statement_split.go +1 -1
- package/linthost/rules_format_ternary_nullish_parens.go +1 -1
- package/linthost/rules_format_trailing_comma.go +64 -1
- package/linthost/rules_format_whitespace.go +1 -1
- package/linthost/rules_functional.go +21 -22
- package/linthost/rules_gap.go +80 -3
- package/linthost/rules_grouped_accessor_pairs.go +84 -25
- package/linthost/rules_guard_for_in.go +64 -147
- package/linthost/rules_jsx_a11y.go +232 -22
- package/linthost/rules_logic.go +84 -38
- package/linthost/rules_nextjs.go +105 -16
- package/linthost/rules_no_class_assign.go +70 -0
- package/linthost/rules_no_duplicate_imports.go +266 -0
- package/linthost/rules_no_else_return.go +156 -38
- package/linthost/rules_no_extend_native.go +113 -17
- package/linthost/rules_no_fallthrough.go +1013 -0
- package/linthost/rules_no_func_assign.go +70 -0
- package/linthost/rules_no_import_assign.go +278 -86
- package/linthost/rules_no_inner_declarations.go +186 -0
- package/linthost/rules_no_loop_func.go +438 -53
- package/linthost/rules_no_mixed_operators.go +208 -52
- package/linthost/rules_no_param_reassign.go +376 -61
- package/linthost/rules_no_promise_executor_return.go +156 -0
- package/linthost/rules_no_restricted_imports.go +1013 -57
- package/linthost/rules_no_restricted_syntax.go +190 -26
- package/linthost/rules_no_useless_constructor.go +6 -36
- package/linthost/rules_problems.go +154 -311
- package/linthost/rules_promise.go +2578 -109
- package/linthost/rules_react_extras.go +71 -45
- package/linthost/rules_react_hooks.go +18 -2
- package/linthost/rules_react_perf.go +1 -0
- package/linthost/rules_react_refresh.go +1 -1
- package/linthost/rules_regexp.go +230 -14
- package/linthost/rules_security.go +45 -19
- package/linthost/rules_self.go +21 -1
- package/linthost/rules_solid.go +13 -7
- package/linthost/rules_storybook.go +1 -1
- package/linthost/rules_suggestions.go +490 -84
- package/linthost/rules_testing_library.go +12 -1
- package/linthost/rules_ts.go +227 -54
- package/linthost/rules_ts_async.go +0 -131
- package/linthost/rules_ts_no_base_to_string.go +3 -4
- package/linthost/rules_ts_no_misused_promises.go +817 -0
- package/linthost/rules_ts_no_restricted_types.go +320 -34
- package/linthost/rules_ts_no_unnecessary_qualifier.go +132 -55
- package/linthost/rules_ts_no_unsafe_assignment.go +608 -60
- package/linthost/rules_ts_require_array_sort_compare.go +5 -5
- package/linthost/rules_ts_switch_exhaustiveness_check.go +556 -86
- package/linthost/rules_unicorn_better_regex.go +224 -16
- package/linthost/rules_unicorn_consistent_destructuring.go +1002 -7
- package/linthost/rules_unicorn_consistent_existence_index_check.go +208 -29
- package/linthost/rules_unicorn_consistent_function_scoping.go +1041 -8
- package/linthost/rules_unicorn_consistent_template_literal_escape.go +178 -7
- package/linthost/rules_unicorn_escape_case.go +30 -19
- package/linthost/rules_unicorn_filename_case.go +763 -16
- package/linthost/rules_unicorn_import_style.go +783 -17
- package/linthost/rules_unicorn_isolated_functions.go +934 -14
- package/linthost/rules_unicorn_no_hex_escape.go +26 -11
- package/linthost/rules_unicorn_no_typeof_undefined.go +234 -25
- package/linthost/rules_unicorn_no_unnecessary_polyfills.go +376 -21
- package/linthost/rules_unicorn_no_unused_properties.go +756 -19
- package/linthost/rules_unicorn_number_literal_case.go +58 -39
- package/linthost/rules_unicorn_prefer_at.go +7 -3
- package/linthost/rules_unicorn_prefer_number_properties.go +381 -48
- package/linthost/rules_unicorn_prefer_optional_catch_binding.go +24 -24
- package/linthost/rules_unicorn_prefer_simple_condition_first.go +597 -19
- package/linthost/rules_unicorn_prefer_string_raw.go +147 -16
- package/linthost/rules_unicorn_prevent_abbreviations.go +1963 -95
- package/linthost/rules_unicorn_string_content.go +611 -15
- package/linthost/rules_unicorn_switch_case_break_position.go +172 -15
- package/linthost/rules_unicorn_template_indent.go +923 -18
- package/linthost/rules_unicorn_text_encoding_identifier_case.go +202 -42
- package/linthost/rules_unicorn_throw_new_error.go +218 -26
- package/linthost/rules_var.go +829 -142
- package/linthost/string_length.go +222 -0
- package/package.json +4 -3
- package/rule/project.go +206 -0
- package/rule/rule.go +58 -1
- package/src/index.ts +58 -17
- package/src/structures/ITtscLintConfig.ts +3 -0
- package/src/structures/TtscLintRuleSetting.ts +3 -2
- package/src/structures/format/ITtscLintFormat.ts +3 -2
- package/src/structures/format/ITtscLintFormatSortImports.ts +21 -4
- package/src/structures/rules/ITtscLintBoundariesRuleOptions.ts +122 -8
- package/src/structures/rules/ITtscLintBoundariesRules.ts +6 -8
- package/src/structures/rules/ITtscLintContributorRules.ts +18 -2
- package/src/structures/rules/ITtscLintCoreRuleOptions.ts +465 -0
- package/src/structures/rules/ITtscLintCoreRules.ts +122 -67
- package/src/structures/rules/ITtscLintRegexpRules.ts +7 -2
- package/src/structures/rules/ITtscLintRuleOptionsMap.ts +88 -7
- package/src/structures/rules/ITtscLintRules.ts +7 -2
- package/src/structures/rules/ITtscLintSolidRules.ts +0 -9
- package/src/structures/rules/ITtscLintTypeScriptRuleOptions.ts +248 -0
- package/src/structures/rules/ITtscLintTypeScriptRules.ts +64 -42
- package/src/structures/rules/ITtscLintUnicornRuleOptions.ts +350 -0
- package/src/structures/rules/ITtscLintUnicornRules.ts +60 -36
- package/src/structures/rules/index.ts +3 -0
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
package linthost
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"math"
|
|
5
|
+
"math/big"
|
|
6
|
+
"strings"
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
const (
|
|
10
|
+
largestFiniteDecimalMagnitude = 308
|
|
11
|
+
smallestRoundedDecimalMagnitude = -324
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
type normalizedDecimalLiteral struct {
|
|
15
|
+
coefficient string
|
|
16
|
+
magnitude *big.Int
|
|
17
|
+
significantDigits string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// numericLiteralLosesPrecision reports whether JavaScript's IEEE-754 Number
|
|
21
|
+
// conversion preserves the significant digits requested by a numeric literal.
|
|
22
|
+
// It handles every Number spelling while leaving BigInt literals alone.
|
|
23
|
+
func numericLiteralLosesPrecision(text string) bool {
|
|
24
|
+
clean := strings.ReplaceAll(strings.TrimSpace(text), "_", "")
|
|
25
|
+
if clean == "" || strings.HasSuffix(strings.ToLower(clean), "n") {
|
|
26
|
+
return false
|
|
27
|
+
}
|
|
28
|
+
if clean[0] == '+' || clean[0] == '-' {
|
|
29
|
+
clean = clean[1:]
|
|
30
|
+
}
|
|
31
|
+
if clean == "" {
|
|
32
|
+
return false
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if base, digits, ok := nonDecimalInteger(clean); ok {
|
|
36
|
+
return integerLiteralLosesPrecision(digits, base)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
literal, ok := normalizeDecimalLiteral(clean)
|
|
40
|
+
if !ok || literal.significantDigits == "" {
|
|
41
|
+
return false
|
|
42
|
+
}
|
|
43
|
+
if literal.magnitude.Cmp(big.NewInt(largestFiniteDecimalMagnitude)) > 0 ||
|
|
44
|
+
literal.magnitude.Cmp(big.NewInt(smallestRoundedDecimalMagnitude)) < 0 {
|
|
45
|
+
return true
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
magnitude := int(literal.magnitude.Int64())
|
|
49
|
+
intended := decimalLiteralValue(literal.significantDigits, magnitude)
|
|
50
|
+
stored, _ := intended.Float64()
|
|
51
|
+
if math.IsInf(stored, 0) || stored == 0 {
|
|
52
|
+
return true
|
|
53
|
+
}
|
|
54
|
+
coefficient, storedMagnitude := roundFloatToDecimalPrecision(
|
|
55
|
+
stored,
|
|
56
|
+
len(literal.coefficient),
|
|
57
|
+
)
|
|
58
|
+
return storedMagnitude != magnitude || coefficient != literal.coefficient
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
func nonDecimalInteger(text string) (base int, digits string, ok bool) {
|
|
62
|
+
lower := strings.ToLower(text)
|
|
63
|
+
for _, prefix := range []struct {
|
|
64
|
+
marker string
|
|
65
|
+
base int
|
|
66
|
+
}{
|
|
67
|
+
{marker: "0b", base: 2},
|
|
68
|
+
{marker: "0o", base: 8},
|
|
69
|
+
{marker: "0x", base: 16},
|
|
70
|
+
} {
|
|
71
|
+
if strings.HasPrefix(lower, prefix.marker) {
|
|
72
|
+
return prefix.base, text[len(prefix.marker):], true
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if len(text) > 1 && text[0] == '0' {
|
|
76
|
+
for index := 1; index < len(text); index++ {
|
|
77
|
+
if text[index] < '0' || text[index] > '7' {
|
|
78
|
+
return 10, text, false
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return 8, text[1:], true
|
|
82
|
+
}
|
|
83
|
+
return 10, text, false
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
func integerLiteralLosesPrecision(digits string, base int) bool {
|
|
87
|
+
intended, ok := new(big.Int).SetString(digits, base)
|
|
88
|
+
if !ok {
|
|
89
|
+
return false
|
|
90
|
+
}
|
|
91
|
+
stored, _ := new(big.Float).SetInt(intended).Float64()
|
|
92
|
+
actual := new(big.Rat).SetFloat64(stored)
|
|
93
|
+
return actual == nil || actual.Cmp(new(big.Rat).SetInt(intended)) != 0
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
func normalizeDecimalLiteral(text string) (normalizedDecimalLiteral, bool) {
|
|
97
|
+
coefficientText := text
|
|
98
|
+
exponent := new(big.Int)
|
|
99
|
+
if index := strings.IndexAny(text, "eE"); index >= 0 {
|
|
100
|
+
if strings.IndexAny(text[index+1:], "eE") >= 0 {
|
|
101
|
+
return normalizedDecimalLiteral{}, false
|
|
102
|
+
}
|
|
103
|
+
coefficientText = text[:index]
|
|
104
|
+
exponentText := text[index+1:]
|
|
105
|
+
if exponentText == "" {
|
|
106
|
+
return normalizedDecimalLiteral{}, false
|
|
107
|
+
}
|
|
108
|
+
if exponentText[0] == '+' || exponentText[0] == '-' {
|
|
109
|
+
if len(exponentText) == 1 {
|
|
110
|
+
return normalizedDecimalLiteral{}, false
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if _, ok := exponent.SetString(exponentText, 10); !ok {
|
|
114
|
+
return normalizedDecimalLiteral{}, false
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
decimalPoints := 0
|
|
119
|
+
decimalPoint := -1
|
|
120
|
+
digits := make([]byte, 0, len(coefficientText))
|
|
121
|
+
for index := 0; index < len(coefficientText); index++ {
|
|
122
|
+
char := coefficientText[index]
|
|
123
|
+
if char == '.' {
|
|
124
|
+
decimalPoints++
|
|
125
|
+
decimalPoint = index
|
|
126
|
+
continue
|
|
127
|
+
}
|
|
128
|
+
if char < '0' || char > '9' {
|
|
129
|
+
return normalizedDecimalLiteral{}, false
|
|
130
|
+
}
|
|
131
|
+
digits = append(digits, char)
|
|
132
|
+
}
|
|
133
|
+
if decimalPoints > 1 || len(digits) == 0 {
|
|
134
|
+
return normalizedDecimalLiteral{}, false
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
significantDigits := strings.TrimLeft(string(digits), "0")
|
|
138
|
+
if significantDigits == "" {
|
|
139
|
+
return normalizedDecimalLiteral{}, true
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
var coefficient string
|
|
143
|
+
var baseMagnitude int
|
|
144
|
+
if decimalPoint < 0 {
|
|
145
|
+
trimmed := strings.TrimLeft(coefficientText, "0")
|
|
146
|
+
coefficient = strings.TrimRight(trimmed, "0")
|
|
147
|
+
baseMagnitude = len(trimmed) - 1
|
|
148
|
+
} else {
|
|
149
|
+
trimmed := strings.TrimLeft(coefficientText, "0")
|
|
150
|
+
point := strings.IndexByte(trimmed, '.')
|
|
151
|
+
if point == 0 {
|
|
152
|
+
coefficient = strings.TrimLeft(trimmed[1:], "0")
|
|
153
|
+
baseMagnitude = len(coefficient) - len(trimmed)
|
|
154
|
+
} else {
|
|
155
|
+
coefficient = strings.ReplaceAll(trimmed, ".", "")
|
|
156
|
+
baseMagnitude = point - 1
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
magnitude := new(big.Int).Add(exponent, big.NewInt(int64(baseMagnitude)))
|
|
161
|
+
return normalizedDecimalLiteral{
|
|
162
|
+
coefficient: coefficient,
|
|
163
|
+
magnitude: magnitude,
|
|
164
|
+
significantDigits: significantDigits,
|
|
165
|
+
}, true
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
func decimalLiteralValue(significantDigits string, magnitude int) *big.Rat {
|
|
169
|
+
numerator, _ := new(big.Int).SetString(significantDigits, 10)
|
|
170
|
+
scale := magnitude - len(significantDigits) + 1
|
|
171
|
+
if scale >= 0 {
|
|
172
|
+
numerator.Mul(numerator, decimalPower(scale))
|
|
173
|
+
return new(big.Rat).SetInt(numerator)
|
|
174
|
+
}
|
|
175
|
+
return new(big.Rat).SetFrac(numerator, decimalPower(-scale))
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
func roundFloatToDecimalPrecision(value float64, precision int) (string, int) {
|
|
179
|
+
exact := new(big.Rat).SetFloat64(value)
|
|
180
|
+
magnitude := decimalMagnitude(exact, value)
|
|
181
|
+
shift := precision - 1 - magnitude
|
|
182
|
+
numerator := new(big.Int).Set(exact.Num())
|
|
183
|
+
denominator := new(big.Int).Set(exact.Denom())
|
|
184
|
+
if shift >= 0 {
|
|
185
|
+
numerator.Mul(numerator, decimalPower(shift))
|
|
186
|
+
} else {
|
|
187
|
+
denominator.Mul(denominator, decimalPower(-shift))
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
quotient, remainder := new(big.Int), new(big.Int)
|
|
191
|
+
quotient.QuoRem(numerator, denominator, remainder)
|
|
192
|
+
if new(big.Int).Lsh(remainder, 1).Cmp(denominator) >= 0 {
|
|
193
|
+
quotient.Add(quotient, big.NewInt(1))
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
coefficient := quotient.String()
|
|
197
|
+
if len(coefficient) > precision {
|
|
198
|
+
quotient.Quo(quotient, big.NewInt(10))
|
|
199
|
+
coefficient = quotient.String()
|
|
200
|
+
magnitude++
|
|
201
|
+
}
|
|
202
|
+
if len(coefficient) < precision {
|
|
203
|
+
coefficient = strings.Repeat("0", precision-len(coefficient)) + coefficient
|
|
204
|
+
}
|
|
205
|
+
return coefficient, magnitude
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
func decimalMagnitude(exact *big.Rat, value float64) int {
|
|
209
|
+
magnitude := int(math.Floor(math.Log10(value)))
|
|
210
|
+
for exact.Cmp(decimalPowerRat(magnitude)) < 0 {
|
|
211
|
+
magnitude--
|
|
212
|
+
}
|
|
213
|
+
for exact.Cmp(decimalPowerRat(magnitude+1)) >= 0 {
|
|
214
|
+
magnitude++
|
|
215
|
+
}
|
|
216
|
+
return magnitude
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
func decimalPowerRat(exponent int) *big.Rat {
|
|
220
|
+
if exponent >= 0 {
|
|
221
|
+
return new(big.Rat).SetInt(decimalPower(exponent))
|
|
222
|
+
}
|
|
223
|
+
return new(big.Rat).SetFrac(big.NewInt(1), decimalPower(-exponent))
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
func decimalPower(exponent int) *big.Int {
|
|
227
|
+
return new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(exponent)), nil)
|
|
228
|
+
}
|