From 0fed72066a40534fc619f37407905a8ea7f46944 Mon Sep 17 00:00:00 2001 From: Jeremiah Brannon Date: Thu, 12 Dec 2024 20:04:01 -0700 Subject: [PATCH 1/3] Parse nested imported map variables --- src/app/converter/converter.test.ts | 12 +++++++++++- src/app/converter/converter.ts | 25 ++++++++++++++++++++++++- src/app/parser/parser.ts | 4 ++-- test/scss/_maps.scss | 6 ++++++ test/scss/_with-import.scss | 7 ++++++- 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/app/converter/converter.test.ts b/src/app/converter/converter.test.ts index e752571..31b92a9 100644 --- a/src/app/converter/converter.test.ts +++ b/src/app/converter/converter.test.ts @@ -82,7 +82,7 @@ describe('Converter class', () => { results = converter.getArray(); }); - it('should include variables form both files', () => { + it('should include variables from both files', () => { let foundFirst = Utils.getDeclarationByName(results, '$brand-solitude'); expect(foundFirst.value).to.equal('#ebeff2'); @@ -131,6 +131,16 @@ describe('Converter class', () => { expect(structured.variables[0]).to.have.property('compiledValue'); }); + + it('should parse map imports from other files', () => { + let opts = { inputFiles: [path.resolve('./test/scss/_with-import.scss')], includePaths: [] }; + opts.includePaths = [path.resolve('./test/scss/')]; + let converter = new Converter(opts); + let structured = converter.getStructured(); + + expect(structured.variables[1].mapValue[0].mapValue[0]).to.have.property('compiledValue'); + expect(structured.variables[1].mapValue[0].mapValue[0].name).to.be.equal('breakpoints'); + }); }); describe('path patterns support', () => { diff --git a/src/app/converter/converter.ts b/src/app/converter/converter.ts index ab540b0..567d4ef 100644 --- a/src/app/converter/converter.ts +++ b/src/app/converter/converter.ts @@ -68,7 +68,7 @@ export class Converter { if (declaration.mapValue) { declaration.mapValue.map((mapDeclaration) => { - mapDeclaration.compiledValue = this.renderPropertyValue(content, mapDeclaration, true); + this.compiledMapStructure(mapDeclaration); return mapDeclaration; }); } @@ -80,6 +80,29 @@ export class Converter { return this.checkForMixins(structuredDeclaration); } + private compiledMapStructure(structuredDeclaration: IDeclaration) + { + let content = this.getContent(); + var parser = new Parser(content); + + // set compiledValue + structuredDeclaration.compiledValue = this.renderPropertyValue(content, structuredDeclaration, true); + + // set mapValue + let map = parser.extractMapDeclarations(structuredDeclaration.compiledValue); + if (map.length) { + structuredDeclaration.mapValue = map.map((declaration) => { + const singleDeclaration = parser.parseSingleDeclaration( + `$${declaration};`, + true + ); + this.compiledMapStructure(singleDeclaration); + + return singleDeclaration; + }); + } + } + public getContent(): string { let inputFiles = []; diff --git a/src/app/parser/parser.ts b/src/app/parser/parser.ts index e6e09ae..9176ea9 100644 --- a/src/app/parser/parser.ts +++ b/src/app/parser/parser.ts @@ -96,7 +96,7 @@ export class Parser { return matches as any; } - private extractMapDeclarations(content: string): [any] { + public extractMapDeclarations(content: string): [any] { const matches = content.match(new RegExp(MAP_DECLARATIOM_REGEX, 'g')); if (!matches) { @@ -107,7 +107,7 @@ export class Parser { } - private parseSingleDeclaration(matchDeclaration: string, isMap: boolean = false): IDeclaration { + public parseSingleDeclaration(matchDeclaration: string, isMap: boolean = false): IDeclaration { let matches = matchDeclaration .replace(/\s*!(default|global)\s*;/, ';') .match(new RegExp(this.getDeclarationPattern(isMap))); diff --git a/test/scss/_maps.scss b/test/scss/_maps.scss index d220474..45216b9 100644 --- a/test/scss/_maps.scss +++ b/test/scss/_maps.scss @@ -42,3 +42,9 @@ $levels: ( 500: 0, 900: 80% ); + +$container-map: ( + 'breakpoints': $bps, + 'icons': $icons, + 'levels': $levels +); diff --git a/test/scss/_with-import.scss b/test/scss/_with-import.scss index e5ab9ba..457128c 100644 --- a/test/scss/_with-import.scss +++ b/test/scss/_with-import.scss @@ -1,3 +1,8 @@ @import "breakpoints"; +@import "maps"; -$imported-value: $bp-desktop; \ No newline at end of file +$imported-value: $bp-desktop; + +$nested-map-import: ( + "imported-map": $container-map +); From dff6dd2c65a276c3463aa0172008cd676eead249 Mon Sep 17 00:00:00 2001 From: Jeremiah Brannon Date: Thu, 12 Dec 2024 22:53:19 -0700 Subject: [PATCH 2/3] Move common test variables to describe block level --- src/app/converter/converter.test.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/app/converter/converter.test.ts b/src/app/converter/converter.test.ts index 31b92a9..b5eb299 100644 --- a/src/app/converter/converter.test.ts +++ b/src/app/converter/converter.test.ts @@ -122,22 +122,16 @@ describe('Converter class', () => { }); describe('includePaths support', () => { + const opts = { inputFiles: [path.resolve('./test/scss/_with-import.scss')], includePaths: [] }; + opts.includePaths = [path.resolve('./test/scss/')]; + const converter = new Converter(opts); + const structured = converter.getStructured(); it('should import variables from other files', () => { - let opts = { inputFiles: [path.resolve('./test/scss/_with-import.scss')], includePaths: [] }; - opts.includePaths = [path.resolve('./test/scss/')]; - let converter = new Converter(opts); - let structured = converter.getStructured(); - expect(structured.variables[0]).to.have.property('compiledValue'); }); it('should parse map imports from other files', () => { - let opts = { inputFiles: [path.resolve('./test/scss/_with-import.scss')], includePaths: [] }; - opts.includePaths = [path.resolve('./test/scss/')]; - let converter = new Converter(opts); - let structured = converter.getStructured(); - expect(structured.variables[1].mapValue[0].mapValue[0]).to.have.property('compiledValue'); expect(structured.variables[1].mapValue[0].mapValue[0].name).to.be.equal('breakpoints'); }); From 091900fbd001c1d9324f421087a2d0e66b6798c7 Mon Sep 17 00:00:00 2001 From: Jeremiah Brannon Date: Sun, 15 Dec 2024 12:17:23 -0700 Subject: [PATCH 3/3] Update test import scss and rename private method --- src/app/converter/converter.ts | 6 +++--- test/scss/_with-import.scss | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/app/converter/converter.ts b/src/app/converter/converter.ts index 567d4ef..2ff524c 100644 --- a/src/app/converter/converter.ts +++ b/src/app/converter/converter.ts @@ -68,7 +68,7 @@ export class Converter { if (declaration.mapValue) { declaration.mapValue.map((mapDeclaration) => { - this.compiledMapStructure(mapDeclaration); + this.compileMapStructure(mapDeclaration); return mapDeclaration; }); } @@ -80,7 +80,7 @@ export class Converter { return this.checkForMixins(structuredDeclaration); } - private compiledMapStructure(structuredDeclaration: IDeclaration) + private compileMapStructure(structuredDeclaration: IDeclaration) { let content = this.getContent(); var parser = new Parser(content); @@ -96,7 +96,7 @@ export class Converter { `$${declaration};`, true ); - this.compiledMapStructure(singleDeclaration); + this.compileMapStructure(singleDeclaration); return singleDeclaration; }); diff --git a/test/scss/_with-import.scss b/test/scss/_with-import.scss index 457128c..e58ea46 100644 --- a/test/scss/_with-import.scss +++ b/test/scss/_with-import.scss @@ -3,6 +3,7 @@ $imported-value: $bp-desktop; -$nested-map-import: ( - "imported-map": $container-map +$imported-maps: ( + "container-map-copy": $container-map, + "bps-copy": $bps );