Skip to content
This repository was archived by the owner on Sep 29, 2025. It is now read-only.
Merged
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
11 changes: 11 additions & 0 deletions snippets/snippets/todo/lgci.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Same as before
type Todo = Readonly<{
id: number
text: string
done: boolean
}>

// Input is an array of Todo items: Todo[]
function completeAll(todos: Todo[]) {
// ...
}
4 changes: 4 additions & 0 deletions snippets/snippets/todo/mnmy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Output is an array of Todo items: Todo[]
function completeAll(todos: Todo[]): Todo[] {
// ...
}
8 changes: 8 additions & 0 deletions snippets/snippets/todo/mwrj.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// After declaring todos as: readonly Todo[],
// the following code WILL NOT compile:

// Compile error - modifies the array
todos[0] = { id: 1, text: '…', done: true }

// Compile error - push() modifies the array
todos.push({ id: 1, text: '…', done: true })
6 changes: 6 additions & 0 deletions snippets/snippets/todo/szan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Make input todos as readonly array
function completeAll(
todos: readonly Todo[]
): Todo[] {
// ...
}
5 changes: 5 additions & 0 deletions snippets/snippets/todo/tdbp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Takes an array of todo items and returns
// a new array where "done" is all true
function completeAll(todos) {
// ...
}
21 changes: 9 additions & 12 deletions src/components/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const CodeBlock = ({
compile,
shouldHighlightResult,
resultError,
tokenIndexIndentWorkaround
tokenIndexIndentWorkaround,
defaultErrorHighlight
}: {
snippet: string
shouldHighlight?: (lineIndex: number, tokenIndex: number) => boolean
Expand All @@ -33,6 +34,7 @@ const CodeBlock = ({
shouldHighlightResult?: (lineIndex: number, tokenIndex: number) => boolean
resultError?: boolean
tokenIndexIndentWorkaround?: number
defaultErrorHighlight?: boolean
}) => {
const [resultVisible, setResultVisible] = useState(defaultResultVisible)
const { radii, colors, ns, maxWidths, spaces, fontSizes } = useTheme()
Expand Down Expand Up @@ -88,21 +90,16 @@ const CodeBlock = ({
shouldHighlightResult(lineIndex, tokenIndex))) &&
css`
font-weight: bold;
background: ${shouldHighlightResult && resultVisible && resultError
background: ${((shouldHighlightResult && resultVisible) ||
defaultErrorHighlight) &&
resultError
? colors('white')
: colors('yellowHighlight')};
border-bottom: ${shouldHighlightResult &&
resultVisible &&
border-bottom: ${((shouldHighlightResult && resultVisible) ||
defaultErrorHighlight) &&
resultError
? 'none'
? `3px solid ${colors('red')}`
: `2px solid ${colors('darkOrange')}`};
text-decoration: ${shouldHighlightResult &&
resultVisible &&
resultError
? 'underline'
: 'none'};
text-decoration-style: wavy;
text-decoration-color: ${colors('red')};
`
}
language={noHighlight ? 'diff' : 'typescript'}
Expand Down
39 changes: 39 additions & 0 deletions src/lib/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,18 @@ function toggleTodo(
// ...
}`

export const lgci = `// Same as before
type Todo = Readonly<{
id: number
text: string
done: boolean
}>

// Input is an array of Todo items: Todo[]
function completeAll(todos: Todo[]) {
// ...
}`

export const lieq = `type Todo = {
id: number
text: string
Expand All @@ -547,6 +559,20 @@ console.log(\`{ id: 1, text: '…', done: false }\`)
console.log('Actual:')
console.log(result)`

export const mnmy = `// Output is an array of Todo items: Todo[]
function completeAll(todos: Todo[]): Todo[] {
// ...
}`

export const mwrj = `// After declaring todos as: readonly Todo[],
// the following code WILL NOT compile:

// Compile error - modifies the array
todos[0] = { id: 1, text: '…', done: true }

// Compile error - push() modifies the array
todos.push({ id: 1, text: '…', done: true })`

export const njgr = `function toggleTodo(todo: Todo): Todo {
// Little Duckling’s refactoring is a
// bad refactoring because it modifies
Expand Down Expand Up @@ -605,6 +631,19 @@ export const reel = `function toggleTodo(todo) {
}
}`

export const szan = `// Make input todos as readonly array
function completeAll(
todos: readonly Todo[]
): Todo[] {
// ...
}`

export const tdbp = `// Takes an array of todo items and returns
// a new array where "done" is all true
function completeAll(todos) {
// ...
}`

export const tgvw = `const bar: Todo = {
text: '…',
done: true
Expand Down
Loading