-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable type check for test, start, build commands in CI (#606)
* disable type check for test, start, build in CI * remove check on tscCompileOnError * add changeset * tsc api to print flat for non CI or pretty for CI * add additional changeset * add silent option * added comments and moved getPackageMetadata * moved reporttsdiagnostics into utils * update changeset to minor * add typecheck to workflow * remove unnecessary typecheck step * add unit tests and fixture and update logger * Add more comments for emitDiagnostics * remove silent flag Co-authored-by: Cang Truong <[email protected]>
- Loading branch information
1 parent
2a95b96
commit 21efc22
Showing
18 changed files
with
267 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'modular-scripts': minor | ||
--- | ||
|
||
Disable typechecking for modular start, test, build commands in CI environments |
This file contains 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,5 @@ | ||
--- | ||
'modular-scripts': minor | ||
--- | ||
|
||
Added `modular typecheck` command to programatically type check the project |
This file contains 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 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 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 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
12 changes: 12 additions & 0 deletions
12
packages/modular-scripts/src/__tests__/__fixtures__/typecheck/InvalidTyping.ts
This file contains 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,12 @@ | ||
/* eslint-disable */ | ||
//@ts-nocheck | ||
|
||
import foo from 'foo'; | ||
|
||
function convertToCelcius(temp: number): number { | ||
const result = (temp - 32) * (5 / 9); | ||
} | ||
|
||
window.__invalid__ = foo.bar; | ||
|
||
convertToCelcius('75'); |
105 changes: 105 additions & 0 deletions
105
packages/modular-scripts/src/__tests__/typecheck.test.ts
This file contains 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,105 @@ | ||
import * as path from 'path'; | ||
import * as fs from 'fs-extra'; | ||
import execa from 'execa'; | ||
|
||
jest.setTimeout(10 * 60 * 1000); | ||
|
||
const fixturesFolder = path.join(__dirname, '__fixtures__'); | ||
|
||
describe('Modular typecheck', () => { | ||
describe('when there are type errors', () => { | ||
beforeEach(() => { | ||
fs.writeFileSync( | ||
path.join(fixturesFolder, 'typecheck', 'InvalidTyping.ts'), | ||
fs | ||
.readFileSync( | ||
path.join(fixturesFolder, 'typecheck', 'InvalidTyping.ts'), | ||
'utf-8', | ||
) | ||
.replace('//@ts-nocheck', '//'), | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
fs.writeFileSync( | ||
path.join(fixturesFolder, 'typecheck', 'InvalidTyping.ts'), | ||
fs | ||
.readFileSync( | ||
path.join(fixturesFolder, 'typecheck', 'InvalidTyping.ts'), | ||
'utf-8', | ||
) | ||
.replace('//', '//@ts-nocheck'), | ||
); | ||
}); | ||
|
||
describe('when in CI', () => { | ||
beforeEach(() => { | ||
process.env.CI = 'true'; | ||
}); | ||
afterEach(() => { | ||
process.env.CI = undefined; | ||
}); | ||
it('should display truncated errors', async () => { | ||
let tsc = ''; | ||
try { | ||
await execa('tsc', ['--noEmit', '--pretty', 'false'], { | ||
all: true, | ||
cleanup: true, | ||
}); | ||
} catch ({ stdout }) { | ||
tsc = stdout as string; | ||
} | ||
let modularStdErr = ''; | ||
try { | ||
await execa('yarnpkg', ['modular', 'typecheck'], { | ||
all: true, | ||
cleanup: true, | ||
}); | ||
} catch ({ stderr }) { | ||
modularStdErr = stderr as string; | ||
} | ||
const tscErrors = tsc.split('\n'); | ||
const modularErrors = modularStdErr.split('\n'); | ||
tscErrors.forEach((errorMessage: string, i: number) => { | ||
expect(modularErrors[i]).toMatch(errorMessage); | ||
}); | ||
}); | ||
}); | ||
describe('when not in CI', () => { | ||
it('should match display full error logs', async () => { | ||
let tsc = ''; | ||
try { | ||
await execa('tsc', ['--noEmit'], { | ||
all: true, | ||
cleanup: true, | ||
}); | ||
} catch ({ stdout }) { | ||
tsc = stdout as string; | ||
} | ||
let modularStdErr = ''; | ||
try { | ||
await execa('yarnpkg', ['modular', 'typecheck'], { | ||
all: true, | ||
cleanup: true, | ||
}); | ||
} catch ({ stderr }) { | ||
modularStdErr = stderr as string; | ||
} | ||
const tscErrors = tsc.split('\n'); | ||
const modularErrors = modularStdErr.split('\n'); | ||
tscErrors.forEach((errorMessage: string, i: number) => { | ||
expect(modularErrors[i]).toMatch(errorMessage); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('when there are no type errors', () => { | ||
it('should print a one line success message', async () => { | ||
const result = await execa('yarnpkg', ['modular', 'typecheck'], { | ||
all: true, | ||
cleanup: true, | ||
}); | ||
expect(result.stdout).toMatch('\u2713 Typecheck passed'); | ||
}); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
packages/modular-scripts/src/buildPackage/getPackageEntryPoints.ts
This file contains 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 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 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 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 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 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 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,90 @@ | ||
import isCI from 'is-ci'; | ||
import path from 'path'; | ||
import ts from 'typescript'; | ||
import chalk from 'chalk'; | ||
import getPackageMetadata from './utils/getPackageMetadata'; | ||
import * as logger from './utils/logger'; | ||
import getModularRoot from './utils/getModularRoot'; | ||
|
||
export async function typecheck(): Promise<void> { | ||
const { typescriptConfig } = await getPackageMetadata(); | ||
|
||
const { _compilerOptions, ...rest } = typescriptConfig; | ||
|
||
const tsConfig = { | ||
...rest, | ||
exclude: [ | ||
'node_modules', | ||
'bower_components', | ||
'jspm_packages', | ||
'tmp', | ||
'**/dist-types', | ||
'**/dist-cjs', | ||
'**/dist-es', | ||
'dist', | ||
], | ||
compilerOptions: { | ||
noEmit: true, | ||
}, | ||
}; | ||
|
||
const diagnosticHost = { | ||
getCurrentDirectory: (): string => getModularRoot(), | ||
getNewLine: (): string => ts.sys.newLine, | ||
getCanonicalFileName: (file: string): string => | ||
ts.sys.useCaseSensitiveFileNames ? file : toFileNameLowerCase(file), | ||
}; | ||
|
||
// Parse all config except for compilerOptions | ||
const configParseResult = ts.parseJsonConfigFileContent( | ||
tsConfig, | ||
ts.sys, | ||
path.dirname('tsconfig.json'), | ||
); | ||
|
||
if (configParseResult.errors.length > 0) { | ||
logger.error('Failed to parse your tsconfig.json'); | ||
throw new Error( | ||
ts.formatDiagnostics(configParseResult.errors, diagnosticHost), | ||
); | ||
} | ||
|
||
const program = ts.createProgram( | ||
configParseResult.fileNames, | ||
configParseResult.options, | ||
); | ||
|
||
// Pulled from typescript's getCanonicalFileName logic | ||
// eslint-disable-next-line no-useless-escape | ||
const fileNameLowerCaseRegExp = /[^\u0130\u0131\u00DFa-z0-9\\/:\-_\. ]+/g; | ||
|
||
function toFileNameLowerCase(x: string) { | ||
return fileNameLowerCaseRegExp.test(x) | ||
? x.replace(fileNameLowerCaseRegExp, x.toLowerCase()) | ||
: x; | ||
} | ||
|
||
// Does not emit files or typings but will add declaration diagnostics to our errors | ||
// This will ensure that makeTypings will be successful in CI before actually attempting to build | ||
const emitResult = program.emit(); | ||
|
||
const diagnostics = ts | ||
.getPreEmitDiagnostics(program) | ||
.concat(emitResult.diagnostics); | ||
|
||
if (diagnostics.length) { | ||
if (isCI) { | ||
// formatDiagnostics will return a readable list of error messages, each with its own line | ||
throw new Error(ts.formatDiagnostics(diagnostics, diagnosticHost)); | ||
} | ||
|
||
// formatDiagnosticsWithColorAndContext will return a list of errors, each with its own line | ||
// and provide an expanded snapshot of the line with the error | ||
throw new Error( | ||
ts.formatDiagnosticsWithColorAndContext(diagnostics, diagnosticHost), | ||
); | ||
} | ||
|
||
// "✓ Typecheck passed" | ||
logger.log(chalk.green('\u2713 Typecheck passed')); | ||
} |
File renamed without changes.
Oops, something went wrong.