@@ -28,13 +28,11 @@ import {
2828type Program = TSESTree . Program
2929
3030export const MUST_AVOID_IMPERATIVE_LOOP = factory `
31- Avoid use of for loops with this practice exercise.
31+ Avoid use of for loops with this exercise.
3232
33- According to the instructions, there are build-in methods on the JavaScript
34- Array global object that are geared towards analysis.
33+ According to the introduction, there are built-in methods on the JavaScript Array object that are geared towards analysis.
3534
36- Take a look at the instructions and/or the MDN docs on what methods are
37- available to use.
35+ Take a look at the introduction and/or the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#instance_methods) on what methods are available to use.
3836` (
3937 'javascript.elyses-analytic-enchantments.must_avoid_imperative_loop' ,
4038 CommentType . Essential
@@ -63,116 +61,130 @@ export class ElysesAnalyticEnchantmentsAnalyzer extends IsolatedAnalyzerImpl {
6361 output . finish ( )
6462 }
6563
66- if ( ! this . solution . cardPosition . isOptimal ) {
67- if ( ! this . solution . cardPosition . usesIndexOf ) {
68- output . add (
69- PREFER_BUILT_IN_METHOD ( {
70- type : 'Array' ,
71- method : 'indexOf' ,
72- } )
73- )
64+ this . checkCorrectMethodUsed ( output )
65+
66+ this . checkForOptimalSolutions ( output )
67+
68+ output . finish ( )
69+ }
70+
71+ private checkStructure (
72+ program : Readonly < Program > ,
73+ source : Readonly < string > ,
74+ output : WritableOutput
75+ ) : ElysesAnalyticEnchantmentsSolution | never {
76+ try {
77+ return new ElysesAnalyticEnchantmentsSolution ( program , source )
78+ } catch ( error ) {
79+ if ( error instanceof NoMethodError ) {
80+ output . add ( NO_METHOD ( { 'method.name' : error . method } ) )
7481 output . finish ( )
7582 }
83+
84+ if ( error instanceof NoExportError ) {
85+ output . add ( NO_NAMED_EXPORT ( { 'export.name' : error . namedExport } ) )
86+ }
87+
88+ throw error
89+ }
90+ }
91+
92+ private checkCorrectMethodUsed ( output : WritableOutput ) : void {
93+ if ( ! this . solution . stackIncludesCard . usesIncludes ) {
94+ output . add (
95+ PREFER_BUILT_IN_METHOD ( {
96+ type : 'Array' ,
97+ method : 'includes' ,
98+ } )
99+ )
100+ output . finish ( )
101+ }
102+
103+ if ( ! this . solution . cardPosition . usesIndexOf ) {
104+ output . add (
105+ PREFER_BUILT_IN_METHOD ( {
106+ type : 'Array' ,
107+ method : 'indexOf' ,
108+ } )
109+ )
110+ output . finish ( )
111+ }
112+
113+ if ( ! this . solution . firstEvenCard . usesFindIndex ) {
114+ output . add (
115+ PREFER_BUILT_IN_METHOD ( {
116+ type : 'Array' ,
117+ method : 'findIndex' ,
118+ } )
119+ )
120+ output . finish ( )
121+ }
122+
123+ if ( ! this . solution . firstOddCard . usesFind ) {
124+ output . add (
125+ PREFER_BUILT_IN_METHOD ( {
126+ type : 'Array' ,
127+ method : 'find' ,
128+ } )
129+ )
130+ output . finish ( )
131+ }
132+
133+ if ( ! this . solution . stackIncludesOdd . usesSome ) {
134+ output . add (
135+ PREFER_BUILT_IN_METHOD ( {
136+ type : 'Array' ,
137+ method : 'some' ,
138+ } )
139+ )
140+ output . finish ( )
141+ }
142+
143+ if ( ! this . solution . cardsAreEven . usesEvery ) {
144+ output . add (
145+ PREFER_BUILT_IN_METHOD ( {
146+ type : 'Array' ,
147+ method : 'every' ,
148+ } )
149+ )
150+ output . finish ( )
151+ }
152+ }
153+
154+ private checkForOptimalSolutions ( output : WritableOutput ) : void {
155+ if ( ! this . solution . cardPosition . isOptimal ) {
76156 output . add ( FUNCTION_NOT_OPTIMAL ( { function : GET_CARD_POSITION } ) )
77157 output . finish ( )
78158 }
79159
80160 if ( ! this . solution . stackIncludesCard . isOptimal ) {
81- if ( ! this . solution . stackIncludesCard . usesIncludes ) {
82- output . add (
83- PREFER_BUILT_IN_METHOD ( {
84- type : 'Array' ,
85- method : 'includes' ,
86- } )
87- )
88- output . finish ( )
89- }
90161 output . add ( FUNCTION_NOT_OPTIMAL ( { function : DOES_STACK_INCLUDE_CARD } ) )
91162 output . finish ( )
92163 }
93164
94165 if ( ! this . solution . cardsAreEven . isOptimal ) {
95- if ( ! this . solution . cardsAreEven . usesEvery ) {
96- output . add (
97- PREFER_BUILT_IN_METHOD ( {
98- type : 'Array' ,
99- method : 'every' ,
100- } )
101- )
102- output . finish ( )
103- }
104166 output . add ( FUNCTION_NOT_OPTIMAL ( { function : IS_EACH_CARD_EVEN } ) )
105167 output . finish ( )
106168 }
107169
108170 if ( ! this . solution . stackIncludesOdd . isOptimal ) {
109- if ( ! this . solution . stackIncludesOdd . usesSome ) {
110- output . add (
111- PREFER_BUILT_IN_METHOD ( {
112- type : 'Array' ,
113- method : 'some' ,
114- } )
115- )
116- output . finish ( )
117- }
118171 output . add (
119172 FUNCTION_NOT_OPTIMAL ( { function : DOES_STACK_INCLUDE_ODD_CARD } )
120173 )
121174 output . finish ( )
122175 }
123176
124177 if ( ! this . solution . firstOddCard . isOptimal ) {
125- if ( ! this . solution . firstOddCard . usesFind ) {
126- output . add (
127- PREFER_BUILT_IN_METHOD ( {
128- type : 'Array' ,
129- method : 'find' ,
130- } )
131- )
132- output . finish ( )
133- }
134178 output . add ( FUNCTION_NOT_OPTIMAL ( { function : GET_FIRST_ODD_CARD } ) )
135179 output . finish ( )
136180 }
137181
138182 if ( ! this . solution . firstEvenCard . isOptimal ) {
139- if ( ! this . solution . firstEvenCard . usesFindIndex ) {
140- output . add (
141- PREFER_BUILT_IN_METHOD ( {
142- type : 'Array' ,
143- method : 'findIndex' ,
144- } )
145- )
146- output . finish ( )
147- }
148183 output . add (
149184 FUNCTION_NOT_OPTIMAL ( { function : GET_FIRST_EVEN_CARD_POSITION } )
150185 )
151186 output . finish ( )
152187 }
153-
154- output . finish ( )
155- }
156-
157- private checkStructure (
158- program : Readonly < Program > ,
159- source : Readonly < string > ,
160- output : WritableOutput
161- ) : ElysesAnalyticEnchantmentsSolution | never {
162- try {
163- return new ElysesAnalyticEnchantmentsSolution ( program , source )
164- } catch ( error ) {
165- if ( error instanceof NoMethodError ) {
166- output . add ( NO_METHOD ( { 'method.name' : error . method } ) )
167- output . finish ( )
168- }
169-
170- if ( error instanceof NoExportError ) {
171- output . add ( NO_NAMED_EXPORT ( { 'export.name' : error . namedExport } ) )
172- }
173-
174- throw error
175- }
176188 }
177189}
178190export default ElysesAnalyticEnchantmentsAnalyzer
0 commit comments