-
-
Notifications
You must be signed in to change notification settings - Fork 249
Syntax Lookup: Extension Points #444
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
Merged
ryyppy
merged 11 commits into
rescript-lang:master
from
kevanstannard:syntax-lookup-extension-points
Sep 9, 2021
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d55ae0f
Add Syntax Lookup Extension Points
kevanstannard 61b52a4
Add Syntax Lookup Private Let Binding
kevanstannard 8e23def
Rename Syntax Lookup Raw JS
kevanstannard 56118f8
Add top level raw import example
kevanstannard f367fa3
Update misc_docs/syntax/extension_identity.mdx
kevanstannard 2ac8a13
Update misc_docs/syntax/extension_regular_expression.mdx
kevanstannard 1eedc0b
Update misc_docs/syntax/extension_raw_top_level.mdx
kevanstannard 8af00e2
Update misc_docs/syntax/extension_raw_top_level.mdx
kevanstannard cd0318e
Update misc_docs/syntax/extension_raw_expression.mdx
kevanstannard 16eed95
Update misc_docs/syntax/extension_raw_expression.mdx
kevanstannard f2ca320
Rename raw top level expression file
kevanstannard 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,32 @@ | ||
--- | ||
id: "debugger" | ||
keywords: ["javascript", "embed", "raw", "debugger"] | ||
name: "%debugger" | ||
summary: "This is the `debugger` extension point." | ||
category: "extensionpoints" | ||
--- | ||
|
||
`%debugger` is used to insert a JavaScript `debugger` statement. | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
let f = (x, y) => { | ||
%debugger | ||
x + y | ||
} | ||
``` | ||
|
||
```js | ||
function f(x, y) { | ||
debugger; | ||
return (x + y) | 0; | ||
} | ||
``` | ||
|
||
</CodeTab> | ||
|
||
### References | ||
|
||
* [Embed Raw JavaScript: Debugger](/docs/manual/latest/embed-raw-javascript#debugger) | ||
* [Extension Point Attributes](/docs/manual/latest/attribute#extension-point) |
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,31 @@ | ||
--- | ||
id: "identity" | ||
keywords: ["identity", "external", "type", "convert"] | ||
name: "%identity" | ||
summary: "This is the `identity` extension point." | ||
category: "extensionpoints" | ||
--- | ||
|
||
`%identity` is used with `external` to do an **unsafe conversion** of a value from one type to another type. | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
external convertToFloat: int => float = "%identity" | ||
let age = 10 | ||
let gpa = 2.1 +. convertToFloat(age) | ||
``` | ||
|
||
```js | ||
var gpa = 2.1 + 10; | ||
var age = 10; | ||
``` | ||
|
||
</CodeTab> | ||
|
||
### References | ||
|
||
* [Type Escape Hatch](/docs/manual/latest/type#type-escape-hatch) | ||
* [Dangerous Type Cast](/docs/manual/latest/interop-cheatsheet#dangerous-type-cast) | ||
* [Extension Point Attributes](/docs/manual/latest/attribute#extension-point) | ||
|
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,43 @@ | ||
--- | ||
id: "private-let" | ||
keywords: ["private", "let"] | ||
name: "%%private" | ||
summary: "This is the `private let binding` extension point." | ||
category: "extensionpoints" | ||
--- | ||
|
||
`%%private` is used to make let bindings private. | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
module Calc = { | ||
%%private(let mult = (x, y) => x * y) | ||
|
||
let double = x => mult(x, 2) | ||
let triple = x => mult(x, 3) | ||
} | ||
``` | ||
|
||
```js | ||
function $$double(x) { | ||
return x << 1; | ||
} | ||
|
||
function triple(x) { | ||
return Math.imul(x, 3); | ||
} | ||
|
||
var Calc = { | ||
$$double: $$double, | ||
triple: triple, | ||
}; | ||
``` | ||
|
||
</CodeTab> | ||
|
||
### References | ||
|
||
* [Private Let Bindings](/docs/manual/latest/let-binding#private-let-bindings) | ||
* [Extension Point Attributes](/docs/manual/latest/attribute#extension-point) | ||
|
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,34 @@ | ||
--- | ||
id: "raw-expression" | ||
keywords: ["javascript", "raw", "expression"] | ||
name: "%raw" | ||
summary: "This is the `raw expression` extension point." | ||
category: "extensionpoints" | ||
--- | ||
|
||
`%raw` is used to embed "raw JavaScript expressions". | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
let canUseCanvas: unit => bool = %raw(` | ||
function canUseCanvas() { | ||
return !!document.createElement('canvas').getContext; | ||
} | ||
`) | ||
``` | ||
|
||
```js | ||
var canUseCanvas = function canUseCanvas() { | ||
return !!document.createElement("canvas").getContext; | ||
}; | ||
``` | ||
|
||
</CodeTab> | ||
|
||
See `%%raw` for embedding top level blocks of JavaScript code rather than expressions. | ||
|
||
### References | ||
|
||
* [Embed Raw JavaScript](/docs/manual/latest/embed-raw-javascript) | ||
* [Extension Point Attributes](/docs/manual/latest/attribute#extension-point) |
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,57 @@ | ||
--- | ||
id: "raw-top-level-expression" | ||
keywords: ["javascript", "raw"] | ||
name: "%%raw" | ||
summary: "This is the `raw top level expression` extension point." | ||
category: "extensionpoints" | ||
--- | ||
|
||
`%%raw` is used to embed top level JavaScript code. | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
%%raw(` | ||
const message = "hello"; | ||
|
||
function greet(m) { | ||
console.log(m) | ||
} | ||
|
||
greet(message) | ||
`) | ||
``` | ||
|
||
```js | ||
const message = "hello"; | ||
|
||
function greet(m) { | ||
console.log(m); | ||
} | ||
|
||
greet(message); | ||
``` | ||
|
||
</CodeTab> | ||
|
||
It's also very useful to do imports with side-effects like this: | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
%%raw(`import "main.css"`) | ||
``` | ||
|
||
```js | ||
import "main.css"; | ||
``` | ||
|
||
</CodeTab> | ||
|
||
See `%raw` for embedding JavaScript expressions rather than top level blocks of code. | ||
|
||
### References | ||
|
||
* [Embed Raw JavaScript](/docs/manual/latest/embed-raw-javascript) | ||
* [Extension Point Attributes](/docs/manual/latest/attribute#extension-point) | ||
* [Converting from JS](/docs/manual/latest/converting-from-js) |
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,29 @@ | ||
--- | ||
id: "regular-expression" | ||
keywords: ["regular", "expression", "re"] | ||
name: "%re" | ||
summary: "This is the `regular expression` extension point." | ||
category: "extensionpoints" | ||
--- | ||
|
||
`%re` is used to create JavaScript regular expressions. | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
let regex = %re("/^hello/") | ||
let result = regex->Js.Re.test_("hello world") | ||
``` | ||
|
||
```js | ||
var regex = /^hello/; | ||
var result = regex.test("hello world"); | ||
``` | ||
|
||
</CodeTab> | ||
|
||
### References | ||
|
||
* [Regular Expressions](/docs/manual/latest/primitive-types#regular-expression) | ||
* [Extension Point Attributes](/docs/manual/latest/attribute#extension-point) | ||
* [Js.Re API](/docs/manual/latest/api/js/re) |
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
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.
One last request: Can you rename this file to
extension_raw_top_level_expression.mdx
? Then we are ready to go!