-
Notifications
You must be signed in to change notification settings - Fork 68
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
Close #605: Disable type check for test, start, build commands in CI #606
Merged
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
bf114b8
disable type check for test, start, build in CI
a7313f7
remove check on tscCompileOnError
7f57346
add changeset
10b0a27
tsc api to print flat for non CI or pretty for CI
da3d26e
add additional changeset
d4415c9
add silent option
4b5ab73
Merge branch 'main' into feature/modular-typecheck
db576f8
added comments and moved getPackageMetadata
c6265e0
Merge remote-tracking branch 'origin/main' into feature/modular-typec…
c9511e3
moved reporttsdiagnostics into utils
c18abdc
update changeset to minor
1b243cc
add typecheck to workflow
b6f0b3d
remove unnecessary typecheck step
d424f09
add unit tests and fixture and update logger
1255d6f
Add more comments for emitDiagnostics
812156d
remove silent flag
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 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'); |
119 changes: 119 additions & 0 deletions
119
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,119 @@ | ||
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 run silently', () => { | ||
it('should print a one line error message', async () => { | ||
let output = ''; | ||
try { | ||
await execa('yarnpkg', ['modular', 'typecheck', '--silent'], { | ||
all: true, | ||
cleanup: true, | ||
}); | ||
} catch ({ stderr }) { | ||
output = stderr as string; | ||
} | ||
expect(output).toMatch('\u0078 Typecheck did not pass'); | ||
}); | ||
}); | ||
}); | ||
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,95 @@ | ||
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(silent = false): Promise<void> { | ||
const { typescriptConfig } = await getPackageMetadata(); | ||
LukeSheard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 (silent) { | ||
// "x Typecheck did not pass" | ||
throw new Error('\u0078 Typecheck did not pass'); | ||
LukeSheard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if (isCI) { | ||
// formatDiagnostics will return a readable list of error messages, each with its own line | ||
throw new Error(ts.formatDiagnostics(diagnostics, diagnosticHost)); | ||
cangarugula marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// 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.
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.
What's the use case for this? I would avoid adding any unnecessary APIs to this until we've got a better idea of when we'll need them.
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.
I added this as a way to silence error messages and provide a green/red light on typechecking. I'm impartial so I will remove for simplicity's sake.