scss-variable-extractor 1.1.0 → 1.4.0

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.
@@ -0,0 +1,184 @@
1
+ const { analyzeAngularPatterns, refactorAngularPatterns, generateAngularPatternReport } = require('../src/ng-refactorer');
2
+ const path = require('path');
3
+ const fs = require('fs');
4
+
5
+ describe('Angular Pattern Refactorer', () => {
6
+ const fixturesPath = path.join(__dirname, 'fixtures/apps/subapp/src/app/component-c');
7
+ const testFilePath = path.join(fixturesPath, 'component-c.component.scss');
8
+
9
+ describe('analyzeAngularPatterns', () => {
10
+ test('should detect ::ng-deep usage', () => {
11
+ const analysis = analyzeAngularPatterns([testFilePath]);
12
+
13
+ expect(analysis.summary.ngDeepCount).toBeGreaterThan(0);
14
+ expect(analysis.ngDeep.length).toBeGreaterThan(0);
15
+ expect(analysis.ngDeep.some(issue => issue.pattern === '::ng-deep')).toBe(true);
16
+ });
17
+
18
+ test('should detect deprecated /deep/ syntax', () => {
19
+ const analysis = analyzeAngularPatterns([testFilePath]);
20
+
21
+ expect(analysis.ngDeep.some(issue => issue.pattern === '/deep/')).toBe(true);
22
+ });
23
+
24
+ test('should detect deprecated >>> syntax', () => {
25
+ const analysis = analyzeAngularPatterns([testFilePath]);
26
+
27
+ expect(analysis.ngDeep.some(issue => issue.pattern === '>>>')).toBe(true);
28
+ });
29
+
30
+ test('should detect !important usage', () => {
31
+ const analysis = analyzeAngularPatterns([testFilePath]);
32
+
33
+ expect(analysis.summary.importantCount).toBeGreaterThan(0);
34
+ expect(analysis.important.length).toBeGreaterThan(0);
35
+ });
36
+
37
+ test('should provide line numbers for issues', () => {
38
+ const analysis = analyzeAngularPatterns([testFilePath]);
39
+
40
+ expect(analysis.ngDeep[0].line).toBeGreaterThan(0);
41
+ expect(analysis.important[0].line).toBeGreaterThan(0);
42
+ });
43
+
44
+ test('should provide suggestions', () => {
45
+ const analysis = analyzeAngularPatterns([testFilePath]);
46
+
47
+ expect(analysis.ngDeep[0].suggestion).toBeDefined();
48
+ expect(analysis.ngDeep[0].suggestion.length).toBeGreaterThan(0);
49
+ expect(analysis.important[0].suggestion).toBeDefined();
50
+ });
51
+
52
+ test('should handle files without issues', () => {
53
+ const cleanFile = path.join(__dirname, 'fixtures/apps/subapp/src/app/component-a/component-a.component.scss');
54
+ const analysis = analyzeAngularPatterns([cleanFile]);
55
+
56
+ expect(analysis.summary.ngDeepCount).toBe(0);
57
+ expect(analysis.summary.importantCount).toBe(0);
58
+ });
59
+ });
60
+
61
+ describe('refactorAngularPatterns', () => {
62
+ let tempFilePath;
63
+
64
+ beforeEach(() => {
65
+ // Create a temporary copy of the test file
66
+ tempFilePath = path.join(fixturesPath, 'temp-test.scss');
67
+ const content = fs.readFileSync(testFilePath, 'utf8');
68
+ fs.writeFileSync(tempFilePath, content, 'utf8');
69
+ });
70
+
71
+ afterEach(() => {
72
+ // Clean up
73
+ if (fs.existsSync(tempFilePath)) {
74
+ fs.unlinkSync(tempFilePath);
75
+ }
76
+ const globalStylesPath = './src/styles.scss';
77
+ if (fs.existsSync(globalStylesPath)) {
78
+ fs.unlinkSync(globalStylesPath);
79
+ }
80
+ });
81
+
82
+ test('should remove ::ng-deep', () => {
83
+ const results = refactorAngularPatterns([tempFilePath], {
84
+ removeNgDeep: true,
85
+ removeImportant: false,
86
+ createGlobalStyles: false,
87
+ dryRun: false
88
+ });
89
+
90
+ expect(results.modified.length).toBe(1);
91
+
92
+ const refactoredContent = fs.readFileSync(tempFilePath, 'utf8');
93
+ expect(refactoredContent).not.toContain('::ng-deep');
94
+ expect(refactoredContent).not.toContain('/deep/');
95
+ expect(refactoredContent).not.toContain('>>>');
96
+ });
97
+
98
+ test('should remove !important', () => {
99
+ const results = refactorAngularPatterns([tempFilePath], {
100
+ removeNgDeep: false,
101
+ removeImportant: true,
102
+ createGlobalStyles: false,
103
+ dryRun: false
104
+ });
105
+
106
+ expect(results.modified.length).toBe(1);
107
+
108
+ const refactoredContent = fs.readFileSync(tempFilePath, 'utf8');
109
+ expect(refactoredContent).not.toContain('!important');
110
+ });
111
+
112
+ test('should handle dry-run mode', () => {
113
+ const originalContent = fs.readFileSync(tempFilePath, 'utf8');
114
+
115
+ const results = refactorAngularPatterns([tempFilePath], {
116
+ removeNgDeep: true,
117
+ removeImportant: true,
118
+ createGlobalStyles: false,
119
+ dryRun: true
120
+ });
121
+
122
+ const contentAfter = fs.readFileSync(tempFilePath, 'utf8');
123
+ expect(contentAfter).toBe(originalContent);
124
+ expect(results.modified.length).toBeGreaterThan(0); // Should still report what would be changed
125
+ });
126
+
127
+ test('should provide warnings for !important removal', () => {
128
+ const results = refactorAngularPatterns([tempFilePath], {
129
+ removeNgDeep: false,
130
+ removeImportant: true,
131
+ createGlobalStyles: false,
132
+ dryRun: false
133
+ });
134
+
135
+ expect(results.warnings.length).toBeGreaterThan(0);
136
+ expect(results.warnings[0].message).toContain('!important');
137
+ });
138
+
139
+ test('should track changes made', () => {
140
+ const results = refactorAngularPatterns([tempFilePath], {
141
+ removeNgDeep: true,
142
+ removeImportant: true,
143
+ createGlobalStyles: false,
144
+ dryRun: false
145
+ });
146
+
147
+ expect(results.modified[0].changes.length).toBeGreaterThan(0);
148
+ expect(results.modified[0].changes.some(c => c.type === 'ng-deep-removed')).toBe(true);
149
+ expect(results.modified[0].changes.some(c => c.type === 'important-removed')).toBe(true);
150
+ });
151
+ });
152
+
153
+ describe('generateAngularPatternReport', () => {
154
+ test('should generate table report', () => {
155
+ const analysis = analyzeAngularPatterns([testFilePath]);
156
+ const report = generateAngularPatternReport(analysis, 'table');
157
+
158
+ expect(report).toContain('Angular Anti-Pattern Analysis');
159
+ expect(report).toContain('Summary');
160
+ expect(report).toContain('::ng-deep');
161
+ expect(report).toContain('!important');
162
+ });
163
+
164
+ test('should generate JSON report', () => {
165
+ const analysis = analyzeAngularPatterns([testFilePath]);
166
+ const report = generateAngularPatternReport(analysis, 'json');
167
+
168
+ const parsed = JSON.parse(report);
169
+ expect(parsed.summary).toBeDefined();
170
+ expect(parsed.ngDeep).toBeDefined();
171
+ expect(parsed.important).toBeDefined();
172
+ });
173
+
174
+ test('should generate Markdown report', () => {
175
+ const analysis = analyzeAngularPatterns([testFilePath]);
176
+ const report = generateAngularPatternReport(analysis, 'markdown');
177
+
178
+ expect(report).toContain('# Angular Anti-Pattern Analysis Report');
179
+ expect(report).toContain('## Summary');
180
+ expect(report).toContain('## ::ng-deep Issues');
181
+ expect(report).toContain('## !important Issues');
182
+ });
183
+ });
184
+ });