diff --git a/src/app/converter/converter.test.ts b/src/app/converter/converter.test.ts index e752571..b5eb299 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'); @@ -122,15 +122,19 @@ 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', () => { + 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..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) => { - mapDeclaration.compiledValue = this.renderPropertyValue(content, mapDeclaration, true); + this.compileMapStructure(mapDeclaration); return mapDeclaration; }); } @@ -80,6 +80,29 @@ export class Converter { return this.checkForMixins(structuredDeclaration); } + private compileMapStructure(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.compileMapStructure(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..e58ea46 100644 --- a/test/scss/_with-import.scss +++ b/test/scss/_with-import.scss @@ -1,3 +1,9 @@ @import "breakpoints"; +@import "maps"; -$imported-value: $bp-desktop; \ No newline at end of file +$imported-value: $bp-desktop; + +$imported-maps: ( + "container-map-copy": $container-map, + "bps-copy": $bps +);