-
Notifications
You must be signed in to change notification settings - Fork 28.7k
fix: improve tsconfig extends checks #61413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6fed2e1
fix configuration defaults
ryan-nauman 0155479
safely preserve `include`
ryan-nauman 2976020
Merge branch 'canary' into canary
ryan-nauman 7288fcd
test readability
ryan-nauman 13400f4
add snapshot
ryan-nauman 0d82aa5
Merge branch 'canary' into canary
ryan-nauman bd1f3cb
lint fix
ryan-nauman 829d1ca
Merge branch 'canary' of https://github.com/ryan-nauman/next.js into …
ryan-nauman 30e441d
update integration snapshots
ryan-nauman cf41528
Merge branch 'canary' into canary
ryan-nauman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -223,10 +223,33 @@ export async function writeConfigurationDefaults( | |
) | ||
) | ||
} else if (hasAppDir && !rawConfig.include.includes(nextAppTypes)) { | ||
userTsConfig.include.push(nextAppTypes) | ||
suggestedActions.push( | ||
cyan('include') + ' was updated to add ' + bold(`'${nextAppTypes}'`) | ||
) | ||
if (!Array.isArray(userTsConfig.include)) { | ||
userTsConfig.include = [] | ||
} | ||
// 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. | ||
|
@@ -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`. | ||
if ( | ||
hasPagesDir && | ||
hasAppDir && | ||
userTsConfig.compilerOptions && | ||
!userTsConfig.compilerOptions.strict && | ||
!('strictNullChecks' in userTsConfig.compilerOptions) | ||
!tsOptions.strict && | ||
!('strictNullChecks' in tsOptions) | ||
Comment on lines
+301
to
+302
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. favoring |
||
) { | ||
userTsConfig.compilerOptions.strictNullChecks = true | ||
suggestedActions.push( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* 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() | ||
}) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated this comment to reflect the checks