From f06641ec4875d318b274fa41741cbab88d18d79d Mon Sep 17 00:00:00 2001 From: Brody McKee Date: Sun, 26 May 2019 07:28:44 +0300 Subject: [PATCH 1/2] Fix LESS extension matching --- src/helpers/__tests__/cssSnapshots.test.ts | 10 +++++----- src/helpers/cssSnapshots.ts | 16 +++++++--------- src/index.ts | 11 +++++++---- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/helpers/__tests__/cssSnapshots.test.ts b/src/helpers/__tests__/cssSnapshots.test.ts index 5fb1c71..104df5c 100644 --- a/src/helpers/__tests__/cssSnapshots.test.ts +++ b/src/helpers/__tests__/cssSnapshots.test.ts @@ -12,12 +12,12 @@ const testFileNames = [ ]; describe('utils / cssSnapshots', () => { - testFileNames.forEach((filename) => { + testFileNames.forEach((fileName) => { let classes: IICSSExports; - const isLess = filename.includes('less'); - const fileType = isLess ? FileTypes.less : FileTypes.css; + const isLess = fileName.endsWith('less'); + const fileType = isLess ? FileTypes.less : FileTypes.scss; const testFile = readFileSync( - join(__dirname, 'fixtures', filename), + join(__dirname, 'fixtures', fileName), 'utf8', ); @@ -25,7 +25,7 @@ describe('utils / cssSnapshots', () => { classes = getClasses(testFile, fileType); }); - describe(`with file '${filename}'`, () => { + describe(`with file '${fileName}'`, () => { describe('getClasses', () => { it('should return an object matching expected CSS', () => { expect(classes).toMatchSnapshot(); diff --git a/src/helpers/cssSnapshots.ts b/src/helpers/cssSnapshots.ts index b3a2c7b..cc4da84 100644 --- a/src/helpers/cssSnapshots.ts +++ b/src/helpers/cssSnapshots.ts @@ -21,15 +21,11 @@ const flattenClassNames = ( ) => previousValue.concat(currentValue); export const enum FileTypes { - css = 'css', - sass = 'sass', + scss = 'scss', less = 'less', } -export const getClasses = ( - css: string, - fileType: FileTypes = FileTypes.css, -) => { +export const getClasses = (css: string, fileType: FileTypes) => { try { let transformedCss = ''; @@ -51,8 +47,8 @@ export const getClasses = ( export const createExports = (classes: IICSSExports, options: Options) => { const isCamelCase = (className: string) => - !NOT_CAMELCASE_REGEXP.test(className); -const isReservedWord = (className: string) => !reserved.check(className); + !NOT_CAMELCASE_REGEXP.test(className); + const isReservedWord = (className: string) => !reserved.check(className); const processedClasses = Object.keys(classes) .map(transformClasses(options.camelCase)) @@ -77,11 +73,13 @@ export default classes; export const getDtsSnapshot = ( ts: typeof ts_module, + fileName: string, scriptSnapshot: ts.IScriptSnapshot, options: Options, ) => { const css = scriptSnapshot.getText(0, scriptSnapshot.getLength()); - const classes = getClasses(css); + const fileType = fileName.endsWith('less') ? FileTypes.less : FileTypes.scss; + const classes = getClasses(css, fileType); const dts = createExports(classes, options); return ts.ScriptSnapshot.fromString(dts); }; diff --git a/src/index.ts b/src/index.ts index d3884d8..5c285cb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,8 +15,6 @@ function init({ typescript: ts }: { typescript: typeof ts_module }) { const { isCSS, isRelativeCSS } = createMatchers(options); _isCSS = isCSS; - info.project.projectService.logger.info(`******** isCSS: ${isCSS}`); - // Creates new virtual source files for the CSS modules. const _createLanguageServiceSourceFile = ts.createLanguageServiceSourceFile; ts.createLanguageServiceSourceFile = ( @@ -25,7 +23,7 @@ function init({ typescript: ts }: { typescript: typeof ts_module }) { ...rest ): ts.SourceFile => { if (isCSS(fileName)) { - scriptSnapshot = getDtsSnapshot(ts, scriptSnapshot, options); + scriptSnapshot = getDtsSnapshot(ts, fileName, scriptSnapshot, options); } const sourceFile = _createLanguageServiceSourceFile( fileName, @@ -46,7 +44,12 @@ function init({ typescript: ts }: { typescript: typeof ts_module }) { ...rest ): ts.SourceFile => { if (isCSS(sourceFile.fileName)) { - scriptSnapshot = getDtsSnapshot(ts, scriptSnapshot, options); + scriptSnapshot = getDtsSnapshot( + ts, + sourceFile.fileName, + scriptSnapshot, + options, + ); } sourceFile = _updateLanguageServiceSourceFile( sourceFile, From aedfdb99d891af9e20f7e81d791fea8298ebeba6 Mon Sep 17 00:00:00 2001 From: Brody McKee Date: Sun, 26 May 2019 07:40:36 +0300 Subject: [PATCH 2/2] Move fileType check --- src/helpers/__tests__/cssSnapshots.test.ts | 10 +++++++--- src/helpers/cssSnapshots.ts | 5 ++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/helpers/__tests__/cssSnapshots.test.ts b/src/helpers/__tests__/cssSnapshots.test.ts index 104df5c..cea0c34 100644 --- a/src/helpers/__tests__/cssSnapshots.test.ts +++ b/src/helpers/__tests__/cssSnapshots.test.ts @@ -1,7 +1,12 @@ import { readFileSync } from 'fs'; import { IICSSExports } from 'icss-utils'; import { join } from 'path'; -import { createExports, getClasses, FileTypes } from '../cssSnapshots'; +import { + createExports, + getClasses, + FileTypes, + getFileType, +} from '../cssSnapshots'; const testFileNames = [ 'test.module.css', @@ -14,8 +19,7 @@ const testFileNames = [ describe('utils / cssSnapshots', () => { testFileNames.forEach((fileName) => { let classes: IICSSExports; - const isLess = fileName.endsWith('less'); - const fileType = isLess ? FileTypes.less : FileTypes.scss; + const fileType = getFileType(fileName); const testFile = readFileSync( join(__dirname, 'fixtures', fileName), 'utf8', diff --git a/src/helpers/cssSnapshots.ts b/src/helpers/cssSnapshots.ts index cc4da84..f174b13 100644 --- a/src/helpers/cssSnapshots.ts +++ b/src/helpers/cssSnapshots.ts @@ -25,6 +25,9 @@ export const enum FileTypes { less = 'less', } +export const getFileType = (fileName: string) => + fileName.endsWith('less') ? FileTypes.less : FileTypes.scss; + export const getClasses = (css: string, fileType: FileTypes) => { try { let transformedCss = ''; @@ -78,7 +81,7 @@ export const getDtsSnapshot = ( options: Options, ) => { const css = scriptSnapshot.getText(0, scriptSnapshot.getLength()); - const fileType = fileName.endsWith('less') ? FileTypes.less : FileTypes.scss; + const fileType = getFileType(fileName); const classes = getClasses(css, fileType); const dts = createExports(classes, options); return ts.ScriptSnapshot.fromString(dts);