-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Respect tsconfig.json extends when validating config #5537
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,25 +19,6 @@ function writeJson(fileName, object) { | |
fs.writeFileSync(fileName, JSON.stringify(object, null, 2) + os.EOL); | ||
} | ||
|
||
const compilerOptions = { | ||
// These are suggested values and will be set when not present in the | ||
// tsconfig.json | ||
target: { suggested: 'es5' }, | ||
allowJs: { suggested: true }, | ||
skipLibCheck: { suggested: true }, | ||
esModuleInterop: { suggested: true }, | ||
allowSyntheticDefaultImports: { suggested: true }, | ||
strict: { suggested: true }, | ||
|
||
// These values are required and cannot be changed by the user | ||
module: { value: 'esnext', reason: 'for import() and import/export' }, | ||
moduleResolution: { value: 'node', reason: 'to match webpack resolution' }, | ||
resolveJsonModule: { value: true, reason: 'to match webpack loader' }, | ||
isolatedModules: { value: true, reason: 'implementation limitation' }, | ||
noEmit: { value: true }, | ||
jsx: { value: 'preserve', reason: 'JSX is compiled by Babel' }, | ||
}; | ||
|
||
function verifyTypeScriptSetup() { | ||
let firstTimeSetup = false; | ||
|
||
|
@@ -86,8 +67,44 @@ function verifyTypeScriptSetup() { | |
process.exit(1); | ||
} | ||
|
||
const compilerOptions = { | ||
// These are suggested values and will be set when not present in the | ||
// tsconfig.json | ||
// 'parsedValue' matches the output value from ts.parseJsonConfigFileContent() | ||
target: { | ||
parsedValue: ts.ScriptTarget.ES5, | ||
suggested: 'es5', | ||
}, | ||
allowJs: { suggested: true }, | ||
skipLibCheck: { suggested: true }, | ||
esModuleInterop: { suggested: true }, | ||
allowSyntheticDefaultImports: { suggested: true }, | ||
strict: { suggested: true }, | ||
|
||
// These values are required and cannot be changed by the user | ||
module: { | ||
parsedValue: ts.ModuleKind.ESNext, | ||
value: 'esnext', | ||
reason: 'for import() and import/export', | ||
}, | ||
moduleResolution: { | ||
parsedValue: ts.ModuleResolutionKind.NodeJs, | ||
value: 'node', | ||
reason: 'to match webpack resolution', | ||
}, | ||
resolveJsonModule: { value: true, reason: 'to match webpack loader' }, | ||
isolatedModules: { value: true, reason: 'implementation limitation' }, | ||
noEmit: { value: true }, | ||
jsx: { | ||
parsedValue: ts.JsxEmit.Preserve, | ||
value: 'preserve', | ||
reason: 'JSX is compiled by Babel', | ||
}, | ||
}; | ||
|
||
const messages = []; | ||
let tsconfig; | ||
let parsedOptions; | ||
try { | ||
const { config, error } = ts.readConfigFile( | ||
paths.appTsConfig, | ||
|
@@ -99,6 +116,21 @@ function verifyTypeScriptSetup() { | |
} | ||
|
||
tsconfig = config; | ||
|
||
// Get TS to parse and resolve any "extends" | ||
// Calling this function also mutates the tsconfig above, | ||
// adding in "include" and "exclude", but the compilerOptions remain untouched | ||
const result = ts.parseJsonConfigFileContent( | ||
config, | ||
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. This should probably be 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. Actually, we can't mutate this here. If we write out the 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. Ah yes good point about it getting written out! Definitely need to fix that. Yep we can deep clone it. We will likely need to create a couple extra variables then to hold on to the Sound good? 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. I'm not sure I follow. We should be fine to clone the entire thing without special behavior. All checks are going to be made against this new 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. Hmm yeah let me think through this a bit... So currently
If we wanted to clone I hope that makes sense. What do you think would be the best approach here? |
||
ts.sys, | ||
path.dirname(paths.appTsConfig) | ||
); | ||
|
||
if (result.errors && result.errors.length) { | ||
throw result.errors[0]; | ||
} | ||
|
||
parsedOptions = result.options; | ||
} catch (_) { | ||
console.error( | ||
chalk.red.bold( | ||
|
@@ -116,17 +148,20 @@ function verifyTypeScriptSetup() { | |
} | ||
|
||
for (const option of Object.keys(compilerOptions)) { | ||
const { value, suggested, reason } = compilerOptions[option]; | ||
const { parsedValue, value, suggested, reason } = compilerOptions[option]; | ||
|
||
const valueToCheck = parsedValue === undefined ? value : parsedValue; | ||
|
||
if (suggested != null) { | ||
if (tsconfig.compilerOptions[option] === undefined) { | ||
if (parsedOptions[option] === undefined) { | ||
tsconfig.compilerOptions[option] = suggested; | ||
messages.push( | ||
`${chalk.cyan('compilerOptions.' + option)} to be ${chalk.bold( | ||
'suggested' | ||
)} value: ${chalk.cyan.bold(suggested)} (this can be changed)` | ||
); | ||
} | ||
} else if (tsconfig.compilerOptions[option] !== value) { | ||
} else if (parsedOptions[option] !== valueToCheck) { | ||
tsconfig.compilerOptions[option] = value; | ||
messages.push( | ||
`${chalk.cyan('compilerOptions.' + option)} ${chalk.bold( | ||
|
@@ -137,6 +172,7 @@ function verifyTypeScriptSetup() { | |
} | ||
} | ||
|
||
// tsconfig will have the merged "include" and "exclude" by this point | ||
if (tsconfig.include == null) { | ||
tsconfig.include = ['src']; | ||
messages.push( | ||
|
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.
Why was this object moved? Seems like unnecessary diff noise.
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.
It was done to add in the
parsedValue
properties, which is using the typescript compiler reference. Alternatively we could leave it in place and hard code the values from typescript. Here's an example:What do you think?
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.
Let's not hard code. I'm fine with this move.