Skip to content
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

Identify JavaScript function parameters #1446

Merged
merged 7 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion components/prism-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
]
});
Prism.languages.flow['function-variable'].pattern = /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=\s*(?:function\b|(?:\([^()]*\)(?:\s*:\s*\w+)?|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i;
delete Prism.languages.flow['parameter'];

Prism.languages.insertBefore('flow', 'operator', {
'flow-punctuation': {
Expand All @@ -31,4 +32,4 @@
lookbehind: true
}
);
}(Prism));
}(Prism));
2 changes: 1 addition & 1 deletion components/prism-flow.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,30 @@ Prism.languages.insertBefore('javascript', 'keyword', {
},
// This must be declared before keyword because we use "function" inside the look-forward
'function-variable': {
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=\s*(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=\s*(?:async(?:(?:\s*(?=\())|\s+))?(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
alias: 'function'
},
'parameter': [
{
pattern: /(function(?:\s+[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*\(\s*)\S[^()]*(?=\s*\))/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 Problems here:

  1. You probably don't want trailing spaces, so please make it [^()]*?. Otherwise (?=\s* won't match anything.
  2. This also matches function a()). Not valid JS, I know, but still. Replace \S with [^\s()] and your good to go.

lookbehind: true,
inside: Prism.languages.javascript
},
{
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=>)/,
inside: Prism.languages.javascript
},
{
pattern: /(\()[^()]+(?=\)\s*=>)/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leading and trailing spaces might not be wanted.

lookbehind: true,
inside: Prism.languages.javascript
},
{
pattern: /(\b(?!await|delete|export|for|if|new|return|switch|throw|typeof|while|yield)(?:[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*)\(\s*)\S[^()]*(?=\s*\)\s*\{)/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. There might be problems with \S again.
  2. Spaces?
  3. awaitSomething(a){} is not matched. Neither is $await(a){} (\b might not have been the best choice).

lookbehind: true,
inside: Prism.languages.javascript
}
],
'constant': /\b[A-Z][A-Z\d_]*\b/
});

Expand Down
2 changes: 1 addition & 1 deletion components/prism-javascript.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,30 @@ Prism.languages.insertBefore('javascript', 'keyword', {
},
// This must be declared before keyword because we use "function" inside the look-forward
'function-variable': {
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=\s*(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=\s*(?:async(?:(?:\s*(?=\())|\s+))?(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
alias: 'function'
},
'parameter': [
{
pattern: /(function(?:\s+[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*\(\s*)\S[^()]*(?=\s*\))/,
lookbehind: true,
inside: Prism.languages.javascript
},
{
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=>)/,
inside: Prism.languages.javascript
},
{
pattern: /(\()[^()]+(?=\)\s*=>)/,
lookbehind: true,
inside: Prism.languages.javascript
},
{
pattern: /(\b(?!await|delete|export|for|if|new|return|switch|throw|typeof|while|yield)(?:[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*)\(\s*)\S[^()]*(?=\s*\)\s*\{)/,
lookbehind: true,
inside: Prism.languages.javascript
}
],
'constant': /\b[A-Z][A-Z\d_]*\b/
});

Expand Down
52 changes: 52 additions & 0 deletions tests/languages/javascript/class-method_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Test {
foo(x, y = 0) {}
async bar(x, y = 0) {}
_ ( ) {}
}

----------------------------------------------------

[
["keyword", "class"],
["class-name", ["Test"]],
["punctuation", "{"],

["function", "foo"],
["punctuation", "("],
["parameter", [
"x",
["punctuation", ","],
" y ",
["operator", "="],
["number", "0"]
]],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "async"],
["function", "bar"],
["punctuation", "("],
["parameter", [
"x",
["punctuation", ","],
" y ",
["operator", "="],
["number", "0"]
]],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["function", "_"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["punctuation", "}"]
]

----------------------------------------------------

Checks for class methods.
97 changes: 83 additions & 14 deletions tests/languages/javascript/function-variable_feature.test
Original file line number Diff line number Diff line change
@@ -1,24 +1,93 @@
foo = function () {}
bar = function baz () {}
foo = function ( x, y) {}
bar = async function baz (x ) {}
baz = async(x) => x
fooBar = x => x
fooBar = (x, y) => x
ಠ_ಠ = () => {}
Ƞȡ_҇ = (ಠ, Ƞ = 2) => {}
Ƞȡ_҇ = async (ಠ, Ƞ = 2) => {}

----------------------------------------------------

[
["function-variable", "foo"], ["operator", "="], ["keyword", "function"],
["punctuation", "("], ["punctuation", ")"], ["punctuation", "{"], ["punctuation", "}"],
["function-variable", "bar"], ["operator", "="], ["keyword", "function"], ["function", "baz"],
["punctuation", "("], ["punctuation", ")"], ["punctuation", "{"], ["punctuation", "}"],
["function-variable", "fooBar"], ["operator", "="], " x ", ["operator", "=>"], " x\r\n",
["function-variable", "ಠ_ಠ"], ["operator", "="], ["punctuation", "("], ["punctuation", ")"],
["operator", "=>"], ["punctuation", "{"], ["punctuation", "}"],
["function-variable", "Ƞȡ_҇"], ["operator", "="],
["punctuation", "("], "ಠ", ["punctuation", ","], " Ƞ ", ["operator", "="], ["number", "2"], ["punctuation", ")"],
["operator", "=>"], ["punctuation", "{"], ["punctuation", "}"]
["function-variable", "foo"],
["operator", "="],
["keyword", "function"],
["punctuation", "("],
["parameter", [
"x",
["punctuation", ","],
" y"
]],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["function-variable", "bar"],
["operator", "="],
["keyword", "async"],
["keyword", "function"],
["function", "baz"],
["punctuation", "("],
["parameter", [
"x "
]],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["function-variable", "baz"],
["operator", "="],
["keyword", "async"],
["punctuation", "("],
["parameter", [
"x"
]],
["punctuation", ")"],
["operator", "=>"], " x\r\n",

["function-variable", "fooBar"],
["operator", "="],
["parameter", [
"x"
]],
["operator", "=>"], " x\r\n",

["function-variable", "fooBar"],
["operator", "="],
["punctuation", "("],
["parameter", [
"x",
["punctuation", ","],
" y"
]],
["punctuation", ")"],
["operator", "=>"], " x\r\n",

["function-variable", "ಠ_ಠ"],
["operator", "="],
["punctuation", "("],
["punctuation", ")"],
["operator", "=>"],
["punctuation", "{"],
["punctuation", "}"],

["function-variable", "Ƞȡ_҇"],
["operator", "="],
["keyword", "async"],
["punctuation", "("],
["parameter", [
"ಠ",
["punctuation", ","],
" Ƞ ",
["operator", "="],
["number", "2"]
]],
["punctuation", ")"],
["operator", "=>"],
["punctuation", "{"],
["punctuation", "}"]
]

----------------------------------------------------

Checks for variables obviously containing functions.
Checks for variables obviously containing functions.
4 changes: 3 additions & 1 deletion tests/languages/javascript/function_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ _()
$()
ಠ_ಠ()
Ƞȡ_҇()
if(notAFunction)

----------------------------------------------------

Expand All @@ -19,7 +20,8 @@ $()
["function", "_"], ["punctuation", "("], ["punctuation", ")"],
["function", "$"], ["punctuation", "("], ["punctuation", ")"],
["function", "ಠ_ಠ"], ["punctuation", "("], ["punctuation", ")"],
["function", "Ƞȡ_҇"], ["punctuation", "("], ["punctuation", ")"]
["function", "Ƞȡ_҇"], ["punctuation", "("], ["punctuation", ")"],
["keyword", "if"], ["punctuation", "("], "notAFunction", ["punctuation", ")"]
]

----------------------------------------------------
Expand Down
6 changes: 4 additions & 2 deletions tests/languages/jsx/issue1294.test
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export default () => (
[
["keyword", "export"],
["keyword", "default"],
["punctuation", "("], ["punctuation", ")"], ["operator", "=>"], ["punctuation", "("],
["punctuation", "("],
["punctuation", ")"],
["operator", "=>"], ["punctuation", "("],
["tag", [
["tag", [
["punctuation", "<"],
Expand Down Expand Up @@ -66,4 +68,4 @@ export default () => (

----------------------------------------------------

See #1294.
See #1294.
6 changes: 4 additions & 2 deletions tests/languages/jsx/issue1335.test
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
["script-punctuation", "="],
["punctuation", "{"],
["punctuation", "("],
"e",
["parameter", [
"e"
]],
["punctuation", ")"],
["operator", "=>"],
["keyword", "this"],
Expand Down Expand Up @@ -121,4 +123,4 @@

----------------------------------------------------

Handles nested pairs of curly braces inside tag attribute. See #1335
Handles nested pairs of curly braces inside tag attribute. See #1335
2 changes: 1 addition & 1 deletion tests/languages/jsx/issue1421.test
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ class Columns extends React.Component {
]

----------------------------------------------------
Checks for fragments short syntax. See #1421
Checks for fragments short syntax. See #1421
2 changes: 1 addition & 1 deletion tests/languages/tsx/tag_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ class Test extends Component {

----------------------------------------------------

Checks for TSX tags.
Checks for TSX tags.