Skip to content

Commit 6d803c2

Browse files
authored
Support comments in tsconfig files (#24)
* Install json5 * Run `npm audit fix` * Add a functional test for resolving aliased imports * Add a comment to the tsconfig to demonstrate the error * Use json5 to fix the error * Clean up the aliased test to match the existing style * Fix: regex error that could create false matches For example, searching for `rCss` matched the ending of `regularCss`
1 parent c0f618d commit 6d803c2

File tree

6 files changed

+75
-39
lines changed

6 files changed

+75
-39
lines changed

package-lock.json

+41-38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"vitest": "^1.4.0"
3838
},
3939
"dependencies": {
40+
"json5": "^2.2.3",
4041
"lilconfig": "^3.1.1",
4142
"lodash.camelcase": "^4.3.0",
4243
"postcss": "^8.1.10",

src/spec/styles/tsconfig.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"paths": {
5+
// @spec-styles points to this directory:
6+
"@spec-styles/*": [
7+
"*"
8+
]
9+
}
10+
}
11+
}

src/spec/utils.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ import sCss from './styles.scss'
112112
import sass from './styles.sass'
113113
import styl from './styles.styl'
114114
115+
import aliasedRegularCss from '@spec-styles/regular.css'
116+
import aliasedNestedSass from '@spec-styles/nested.sass'
117+
115118
const rCss = require('./style.css')
116119
const rStyle = require('./style.css')
117120
const rStyles = require('./styles.css')
@@ -148,6 +151,18 @@ describe('findImportPath', () => {
148151
}),
149152
);
150153

154+
const realDirPath = path.join(__dirname, 'styles');
155+
156+
[
157+
['aliasedRegularCss', path.join(realDirPath, 'regular.css')],
158+
['aliasedNestedSass', path.join(realDirPath, 'nested.sass')],
159+
].forEach(([importName, expected]) => {
160+
it(`resolves aliased import path for ${importName}`, () => {
161+
const result = findImportPath(fileContent, importName, realDirPath);
162+
expect(result).toBe(expected);
163+
})
164+
});
165+
151166
it('returns an empty string when there is no import', () => {
152167
const simpleComponentFile = [
153168
"import React from 'react'",

src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function genImportRegExp(importName: string): RegExp {
2222
const file = '(.+\\.(styl|sass|scss|less|css))';
2323
const fromOrRequire = '(?:from\\s+|=\\s+require(?:<any>)?\\()';
2424
const requireEndOptional = '\\)?';
25-
const pattern = `${importName}\\s+${fromOrRequire}["']${file}["']${requireEndOptional}`;
25+
const pattern = `\\b${importName}\\s+${fromOrRequire}["']${file}["']${requireEndOptional}`;
2626

2727
return new RegExp(pattern);
2828
}

src/utils/resolveAliasedImport.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'fs';
22
import path from 'path';
3+
import JSON5 from 'json5';
34

45
import {lilconfigSync} from 'lilconfig';
56

@@ -49,6 +50,11 @@ export const resolveAliasedImport = ({
4950
}): string | null => {
5051
const searcher = lilconfigSync('', {
5152
searchPlaces: ['tsconfig.json', 'jsconfig.json'],
53+
loaders: {
54+
'.json': function(filepath, content) {
55+
return JSON5.parse(content);
56+
}
57+
}
5258
});
5359
const config = searcher.search(location);
5460

0 commit comments

Comments
 (0)