Unverified Commit c09ce24a authored by Ryan Nauman's avatar Ryan Nauman Committed by GitHub

fix: improve tsconfig extends checks (#61413)

Co-authored-by: 's avatarSebastian Silbermann <silbermann.sebastian@gmail.com>
parent 3fa9cb7b
...@@ -223,10 +223,33 @@ export async function writeConfigurationDefaults( ...@@ -223,10 +223,33 @@ export async function writeConfigurationDefaults(
) )
) )
} else if (hasAppDir && !rawConfig.include.includes(nextAppTypes)) { } else if (hasAppDir && !rawConfig.include.includes(nextAppTypes)) {
userTsConfig.include.push(nextAppTypes) if (!Array.isArray(userTsConfig.include)) {
suggestedActions.push( userTsConfig.include = []
cyan('include') + ' was updated to add ' + bold(`'${nextAppTypes}'`) }
) // rawConfig will resolve all extends and include paths (ex: tsconfig.json, tsconfig.base.json, etc.)
// if it doesn't match userTsConfig then update the userTsConfig to add the
// rawConfig's includes in addition to nextAppTypes
if (
rawConfig.include.length !== userTsConfig.include.length ||
JSON.stringify(rawConfig.include.sort()) !==
JSON.stringify(userTsConfig.include.sort())
) {
userTsConfig.include.push(...rawConfig.include, nextAppTypes)
suggestedActions.push(
cyan('include') +
' was set to ' +
bold(
`[${[...rawConfig.include, nextAppTypes]
.map((i) => `'${i}'`)
.join(', ')}]`
)
)
} else {
userTsConfig.include.push(nextAppTypes)
suggestedActions.push(
cyan('include') + ' was updated to add ' + bold(`'${nextAppTypes}'`)
)
}
} }
// Enable the Next.js typescript plugin. // Enable the Next.js typescript plugin.
...@@ -270,14 +293,13 @@ export async function writeConfigurationDefaults( ...@@ -270,14 +293,13 @@ export async function writeConfigurationDefaults(
) )
} }
// If `strict` is set to `false` or `strictNullChecks` is set to `false`, // If `strict` is set to `false` and `strictNullChecks` is set to `false`,
// then set `strictNullChecks` to `true`. // then set `strictNullChecks` to `true`.
if ( if (
hasPagesDir && hasPagesDir &&
hasAppDir && hasAppDir &&
userTsConfig.compilerOptions && !tsOptions.strict &&
!userTsConfig.compilerOptions.strict && !('strictNullChecks' in tsOptions)
!('strictNullChecks' in userTsConfig.compilerOptions)
) { ) {
userTsConfig.compilerOptions.strictNullChecks = true userTsConfig.compilerOptions.strictNullChecks = true
suggestedActions.push( suggestedActions.push(
......
...@@ -695,8 +695,7 @@ import path from 'path' ...@@ -695,8 +695,7 @@ import path from 'path'
"{ "{
"extends": "./tsconfig.base.json", "extends": "./tsconfig.base.json",
"compilerOptions": { "compilerOptions": {
"target": "ES2017", "target": "ES2017"
"strictNullChecks": true
} }
} }
" "
...@@ -764,8 +763,7 @@ import path from 'path' ...@@ -764,8 +763,7 @@ import path from 'path'
"extends": "./tsconfig.base.json", "extends": "./tsconfig.base.json",
"compilerOptions": { "compilerOptions": {
"target": "ES2017", "target": "ES2017",
"incremental": true, "incremental": true
"strictNullChecks": true
} }
} }
" "
......
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import { writeConfigurationDefaults } from 'next/dist/lib/typescript/writeConfigurationDefaults'
import * as ts from 'typescript'
const fixtureDir = join(__dirname, 'fixtures/config-ts')
const tsconfigFile = join(fixtureDir, 'tsconfig.json')
const tsconfigBaseFile = join(fixtureDir, 'tsconfig.base.json')
const distDir = '.next'
const nextAppTypes = `${distDir}/types/**/*.ts`
describe('tsconfig.base.json', () => {
beforeEach(async () => {
await fs.ensureDir(fixtureDir)
})
afterEach(async () => {
await fs.remove(tsconfigFile)
await fs.remove(tsconfigBaseFile)
})
describe('appDir', () => {
it('should support empty includes when base provides it', async () => {
const include = ['**/*.ts', '**/*.tsx', nextAppTypes]
const content = {
extends: './tsconfig.base.json',
}
const baseContent = {
include,
}
await fs.writeFile(tsconfigFile, JSON.stringify(content, null, 2))
await fs.writeFile(tsconfigBaseFile, JSON.stringify(baseContent, null, 2))
await expect(
writeConfigurationDefaults(ts, tsconfigFile, false, true, distDir, true)
).resolves.not.toThrow()
const output = await fs.readFile(tsconfigFile, 'utf8')
const parsed = JSON.parse(output)
expect(parsed.include).toBeUndefined()
})
it('should replace includes when base is missing appTypes', async () => {
const include = ['**/*.ts', '**/*.tsx']
const content = {
extends: './tsconfig.base.json',
}
const baseContent = {
include,
}
await fs.writeFile(tsconfigFile, JSON.stringify(content, null, 2))
await fs.writeFile(tsconfigBaseFile, JSON.stringify(baseContent, null, 2))
await expect(
writeConfigurationDefaults(ts, tsconfigFile, false, true, distDir, true)
).resolves.not.toThrow()
const output = await fs.readFile(tsconfigFile, 'utf8')
const parsed = JSON.parse(output)
expect(parsed.include.sort()).toMatchInlineSnapshot(`
[
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
]
`)
})
it('should not add strictNullChecks if base provides it', async () => {
const content = {
extends: './tsconfig.base.json',
}
const baseContent = {
compilerOptions: {
strictNullChecks: true,
strict: true,
},
}
await fs.writeFile(tsconfigFile, JSON.stringify(content, null, 2))
await fs.writeFile(tsconfigBaseFile, JSON.stringify(baseContent, null, 2))
await writeConfigurationDefaults(
ts,
tsconfigFile,
false,
true,
distDir,
true
)
const output = await fs.readFile(tsconfigFile, 'utf8')
const parsed = JSON.parse(output)
expect(parsed.compilerOptions.strictNullChecks).toBeUndefined()
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment