Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions compiler/apps/playground/__tests__/e2e/page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,27 @@
);
});

test('typescript `as const` does not produce a JS-service error squiggle', async ({
page,
}) => {
const store: Store = {
source: ["let noError = '' as const;", 'let error = '].join('\n'),
config: defaultConfig,
showInternals: false,
};
const hash = encodeStore(store);
await page.goto(`/#${hash}`, {waitUntil: 'networkidle'});
await page.waitForFunction(isMonacoLoaded);

const editorInput = page.locator('.monaco-editor-input');
await expect(editorInput).toBeVisible();
await editorInput.click();

await expect(page.locator('.squiggly-error')).toHaveCount(1, {

Check failure on line 332 in compiler/apps/playground/__tests__/e2e/page.spec.ts

View workflow job for this annotation

GitHub Actions / Test playground

[chromium] › __tests__/e2e/page.spec.ts:316:5 › typescript `as const` does not produce a JS-service error squiggle

1) [chromium] › __tests__/e2e/page.spec.ts:316:5 › typescript `as const` does not produce a JS-service error squiggle Error: expect(locator).toHaveCount(expected) failed Locator: locator('.squiggly-error') Expected: 1 Received: 0 Timeout: 10000ms Call log: - Expect "toHaveCount" with timeout 10000ms - waiting for locator('.squiggly-error') 14 × locator resolved to 0 elements - unexpected value "0" 330 | await editorInput.click(); 331 | > 332 | await expect(page.locator('.squiggly-error')).toHaveCount(1, { | ^ 333 | timeout: 10_000, 334 | }); 335 | }); at /home/runner/work/react/react/compiler/apps/playground/__tests__/e2e/page.spec.ts:332:49
timeout: 10_000,
});
});

TEST_CASE_INPUTS.forEach((t, idx) =>
test(`playground compiles: ${t.name}`, async ({page}) => {
const store: Store = {
Expand Down
28 changes: 16 additions & 12 deletions compiler/apps/playground/components/Editor/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ export default function Input({errors, language}: Props): JSX.Element {
const store = useStore();
const dispatchStore = useStoreDispatch();

const isTypeScript = language === 'typescript';
const editorPath = isTypeScript ? 'index.tsx' : 'index.js';
const editorLanguage = isTypeScript ? 'typescript' : 'javascript';

// Set tab width to 2 spaces for the selected input file.
useEffect(() => {
if (!monaco) return;
const uri = monaco.Uri.parse(`file:///index.js`);
const uri = monaco.Uri.parse(`file:///${editorPath}`);
const model = monaco.editor.getModel(uri);
invariant(model, 'Model must exist for the selected input file.');
renderReactCompilerMarkers({
Expand All @@ -51,13 +55,14 @@ export default function Input({errors, language}: Props): JSX.Element {
details: errors,
source: store.source,
});
}, [monaco, errors, store.source]);
}, [monaco, errors, store.source, editorPath]);

useEffect(() => {
/**
* Ignore "can only be used in TypeScript files." errors, since
* we want to support syntax highlighting for Flow (*.js) files
* and Flow is not a built-in language.
* For Flow (*.js) files, suppress the "can only be used in TypeScript
* files." diagnostics emitted by Monaco's JS service so Flow type syntax
* doesn't squiggle. TS sources use a *.tsx model instead, which lets
* Monaco's TS service parse TS syntax natively without these diagnostics.
*/
if (!monaco) return;
monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
Expand All @@ -81,6 +86,10 @@ export default function Input({errors, language}: Props): JSX.Element {
// Monaco can't validate Flow component syntax
noSyntaxValidation: language === 'flow',
});
monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
noSemanticValidation: true,
noSyntaxValidation: false,
});
}, [monaco, language]);

const handleChange: (value: string | undefined) => void = async value => {
Expand Down Expand Up @@ -141,13 +150,8 @@ export default function Input({errors, language}: Props): JSX.Element {

const editorContent = (
<MonacoEditor
path={'index.js'}
/**
* .js and .jsx files are specified to be TS so that Monaco can actually
* check their syntax using its TS language service. They are still JS files
* due to their extensions, so TS language features don't work.
*/
language={'javascript'}
path={editorPath}
language={editorLanguage}
value={store.source}
onMount={handleMount}
onChange={handleChange}
Expand Down
Loading