generated from bywhitebird/starter-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from bywhitebird/53-add-support-for-onmount-in…
…struction 53 add support for onmount instruction
- Loading branch information
Showing
30 changed files
with
257 additions
and
3 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
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
16 changes: 16 additions & 0 deletions
16
packages/kaz-ast/src/features/lifecycle-instruction/contexts/LifecycleInstructionContext.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,16 @@ | ||
import { Context } from '../../../lib/voltair' | ||
import { ArrowFunctionBodyToken, ArrowToken, LeftParenthesisToken, RightParenthesisToken, WhitespaceToken } from '../../../shared' | ||
import { EndInstructionToken } from '../../instruction' | ||
|
||
export const LifecycleInstructionContext: Context<'LifecycleInstructionContext'> = new Context({ | ||
$name: 'LifecycleInstructionContext', | ||
breakingPatterns: [/\s+/, /^\($/, /^\)$/, /^=>$/, /^,$/], | ||
availableTokens: [ | ||
() => LeftParenthesisToken, | ||
() => RightParenthesisToken, | ||
() => ArrowToken, | ||
() => ArrowFunctionBodyToken, | ||
() => EndInstructionToken, | ||
() => WhitespaceToken, | ||
], | ||
}) |
3 changes: 3 additions & 0 deletions
3
packages/kaz-ast/src/features/lifecycle-instruction/fixtures/LifecycleInstructionFixtures.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,3 @@ | ||
export const LifecycleInstructionFixtures = [ | ||
{}, | ||
] |
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 @@ | ||
export * from './contexts/LifecycleInstructionContext' | ||
|
||
export * from './tokens/LifecycleInstructionToken' | ||
|
||
export * from './sequences/LifecycleInstructionSequence' |
16 changes: 16 additions & 0 deletions
16
...ages/kaz-ast/src/features/lifecycle-instruction/sequences/LifecycleInstructionSequence.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,16 @@ | ||
import { LifecycleInstructionContext, LifecycleInstructionToken } from '..' | ||
import { g, gp, s } from '../../../lib/voltair' | ||
import { ArrowFunctionBodyToken, ArrowToken, LeftParenthesisToken, RightParenthesisToken } from '../../../shared' | ||
|
||
export const LifecycleInstructionSequence = gp( | ||
'LifecycleEventInstruction', | ||
s( | ||
g('event', () => LifecycleInstructionToken), | ||
LeftParenthesisToken, | ||
RightParenthesisToken, | ||
ArrowToken, | ||
() => g('callbackExpression', ArrowFunctionBodyToken.extends({ | ||
endContexts: [LifecycleInstructionContext], | ||
})), | ||
), | ||
) |
16 changes: 16 additions & 0 deletions
16
packages/kaz-ast/src/features/lifecycle-instruction/tokens/LifecycleInstructionToken.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,16 @@ | ||
import { LifecycleInstructionContext } from '..' | ||
import { Token } from '../../../lib/voltair' | ||
import { InstructionContext } from '../../instruction' | ||
|
||
export const LifecycleInstructionToken = new Token({ | ||
$name: 'LifecycleInstructionToken', | ||
validator: /^on((?:Mount))$/, | ||
startContexts: [() => LifecycleInstructionContext], | ||
inContexts: [() => InstructionContext], | ||
getValue(rawValue) { | ||
return rawValue.slice(2).toLowerCase() | ||
}, | ||
semantic: { | ||
type: 'keyword', | ||
}, | ||
}) |
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
48 changes: 48 additions & 0 deletions
48
packages/kaz-ast/tests/fixtures/parse/lifecycle-instructions.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,48 @@ | ||
import type { JsonValue } from '../../../src/lib/voltair/types/JsonValue' | ||
|
||
export const lifecycleInstructionsFixtures: ( | ||
{ | ||
name: string | ||
input: string | ||
} & ( | ||
| { expectedTree: JsonValue } | ||
| { expectError: boolean } | ||
) | ||
)[] = [ | ||
{ | ||
name: 'When I use a onMount instruction with a function, onMount is validated', | ||
input: ` | ||
- onMount () => { | ||
console.log('Hello') | ||
console.log('Mounted') | ||
} | ||
`, | ||
expectedTree: { | ||
$type: 'Kaz', | ||
instructions: [ | ||
{ | ||
$type: 'LifecycleEventInstruction', | ||
event: 'mount', | ||
callbackExpression: 'console.log(\'Hello\')\n console.log(\'Mounted\')', | ||
}, | ||
], | ||
}, | ||
}, | ||
{ | ||
name: 'When I use a onMount instruction without an arrow, onMount is not validated', | ||
input: ` | ||
- onMount () { | ||
console.log('Hello') | ||
console.log('Mounted') | ||
} | ||
`, | ||
expectError: true, | ||
}, | ||
{ | ||
name: 'When I use only the onMount keyword, onMount is not validated', | ||
input: ` | ||
- onMount | ||
`, | ||
expectError: true, | ||
}, | ||
] |
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
33 changes: 33 additions & 0 deletions
33
packages/kaz-ast/tests/fixtures/tokenize/lifecycle-instructions.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,33 @@ | ||
import { EndInstructionToken, StartInstructionToken } from '../../../src/features/instruction' | ||
import { LifecycleInstructionToken } from '../../../src/features/lifecycle-instruction' | ||
import type { Token } from '../../../src/lib/voltair' | ||
import { ArrowFunctionBodyToken, ArrowToken, LeftParenthesisToken, RightParenthesisToken } from '../../../src/shared' | ||
|
||
export const lifecycleInstructionsFixtures: ({ | ||
name: string | ||
input: string | ||
expectedTokenCheckers: { | ||
checker: Token | ||
rawValue?: string | ||
value?: unknown | ||
}[] | ||
})[] = [ | ||
{ | ||
name: 'When I use a onMount instruction with a function, onMount is correctly tokenized', | ||
input: ` | ||
- onMount () => { | ||
console.log('Hello') | ||
console.log('Mounted') | ||
} | ||
`, | ||
expectedTokenCheckers: [ | ||
{ checker: StartInstructionToken, rawValue: '-' }, | ||
{ checker: LifecycleInstructionToken, rawValue: 'onMount' }, | ||
{ checker: LeftParenthesisToken, rawValue: '(' }, | ||
{ checker: RightParenthesisToken, rawValue: ')' }, | ||
{ checker: ArrowToken, rawValue: '=>' }, | ||
{ checker: ArrowFunctionBodyToken, value: 'console.log(\'Hello\')\n console.log(\'Mounted\')' }, | ||
{ checker: EndInstructionToken, rawValue: '' }, | ||
], | ||
}, | ||
] |
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
11 changes: 11 additions & 0 deletions
11
packages/test-web-transformer/src/fixtures/onmount/README.md
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,11 @@ | ||
# onmount scenario | ||
|
||
### Tests | ||
|
||
- Renders a text "not mounted" | ||
- Registers a timeout that changes the text to "mounted" after 1 second | ||
|
||
### Screenshots | ||
|
||
[onmount-before.png](./screenshots/onmount-before.png) | ||
[onmount-after-1-second.png](./screenshots/onmount-after-1-second.png) |
27 changes: 27 additions & 0 deletions
27
packages/test-web-transformer/src/fixtures/onmount/onmount-fixture.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,27 @@ | ||
/* eslint-disable no-template-curly-in-string */ // NOTE: Need to disable this to use `${}` with dedent | ||
import dedent from 'dedent' | ||
|
||
import { createTestWebTransformerFixture } from '../../utils/create-test-web-transformer-fixture' | ||
|
||
export const onmountFixture = createTestWebTransformerFixture({ | ||
fixtureDirectory: __dirname, | ||
input: { | ||
Index: dedent` | ||
- state mounted: boolean = false | ||
- onMount () => { | ||
setTimeout(() => { | ||
mounted = true | ||
}, 1000) | ||
} | ||
div() { | ||
${'${mounted ? "mounted" : "not mounted"}'} | ||
} | ||
`, | ||
}, | ||
scenario: async (page) => { | ||
await page.screenshot({ path: 'onmount-before.png' }) | ||
await page.waitForTimeout(1000) | ||
await page.screenshot({ path: 'onmount-after-1-second.png' }) | ||
}, | ||
}) |
Binary file added
BIN
+6.74 KB
...est-web-transformer/src/fixtures/onmount/screenshots/onmount-after-1-second.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.81 KB
packages/test-web-transformer/src/fixtures/onmount/screenshots/onmount-before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
23 changes: 23 additions & 0 deletions
23
...sformer-react/transform/transform-service/functions/handlers/lifecycleEventInstruction.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,23 @@ | ||
import { Effect } from 'effect' | ||
|
||
import { TransformService } from '../../transform-service' | ||
import type { Handle } from '../handle' | ||
|
||
export const handleLifecycleEventInstruction: Handle<'lifecycleEventInstruction', string> = lifecycleEventInstruction => | ||
Effect.gen(function* (_) { | ||
const transformService = yield * _(TransformService) | ||
|
||
yield * _(transformService.addImport('namedImport', { name: 'useEffect', path: 'react' })) | ||
|
||
const { event, callbackExpression } = lifecycleEventInstruction | ||
|
||
switch (event.$value) { | ||
case 'mount': { | ||
return String.prototype.concat( | ||
'useEffect(() => {', | ||
yield * _(transformService.transformExpression(callbackExpression)), | ||
'}, [])', | ||
) | ||
} | ||
} | ||
}) |
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
11 changes: 11 additions & 0 deletions
11
packages/transformer-typescript/src/handlers/lifecycleEventInstruction.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,11 @@ | ||
import type { IHandler } from '../transformer-typescript' | ||
|
||
export const handleLifecycleEventInstruction: IHandler<'lifecycleEventInstruction'> = async (lifecycleEvent, { addGeneratedContent }) => { | ||
switch (lifecycleEvent.event.$value) { | ||
case 'mount': { | ||
addGeneratedContent('(function (onMount: () => void) {})(() => {') | ||
addGeneratedContent(lifecycleEvent.callbackExpression) | ||
addGeneratedContent('})') | ||
} | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
packages/transformer-vue/src/handlers/lifecycleEventInstruction.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,13 @@ | ||
import type { IHandler } from '../transformer-vue' | ||
import { transformVueExpression } from '../utils/transform-vue-expression' | ||
|
||
export const handleLifecycleEventInstruction: IHandler<'lifecycleEventInstruction'> = (lifecycleEvent, { addImport }) => { | ||
addImport({ | ||
namedImports: [ | ||
{ name: 'onMounted' }, | ||
], | ||
path: 'vue', | ||
}) | ||
|
||
return `onMounted(() => {${transformVueExpression(lifecycleEvent.callbackExpression.$value)}})` | ||
} |
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
Oops, something went wrong.