Skip to content

Commit

Permalink
Tests: Added new pattern check for octal escapes (#2189)
Browse files Browse the repository at this point in the history
This adds a new pattern check to disallow octal character escapes (e.g. `\12`).
  • Loading branch information
RunDevelopment authored Jan 18, 2020
1 parent 5450e24 commit 81e1c3d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 23 deletions.
4 changes: 2 additions & 2 deletions components/prism-bash.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@
'string': [
// Support for Here-documents https://en.wikipedia.org/wiki/Here_document
{
pattern: /((?:^|[^<])<<-?\s*)(\w+?)\s*(?:\r?\n|\r)(?:[\s\S])*?(?:\r?\n|\r)\2/,
pattern: /((?:^|[^<])<<-?\s*)(\w+?)\s*(?:\r?\n|\r)[\s\S]*?(?:\r?\n|\r)\2/,
lookbehind: true,
greedy: true,
inside: insideString
},
// Here-document with quotes around the tag
// → No expansion (so no “inside”).
{
pattern: /((?:^|[^<])<<-?\s*)(["'])(\w+)\2\s*(?:\r?\n|\r)(?:[\s\S])*?(?:\r?\n|\r)\3/,
pattern: /((?:^|[^<])<<-?\s*)(["'])(\w+)\2\s*(?:\r?\n|\r)[\s\S]*?(?:\r?\n|\r)\3/,
lookbehind: true,
greedy: true
},
Expand Down
2 changes: 1 addition & 1 deletion components/prism-bash.min.js

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

59 changes: 40 additions & 19 deletions components/prism-shell-session.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
Prism.languages['shell-session'] = {
'command': {
pattern: /\$(?:[^\r\n'"<]|(["'])(?:\\[\s\S]|\$\([^)]+\)|`[^`]+`|(?!\1)[^\\])*\1|(?:^|[^<])<<\s*["']?(?:\w+?)["']?\s*(?:\r\n?|\n)(?:[\s\S])*?(?:\r\n?|\n)\3)+/,
inside: {
'bash': {
pattern: /(\$\s*)[\s\S]+/,
lookbehind: true,
alias: 'language-bash',
inside: Prism.languages.bash
},
'sh': {
pattern: /^\$/,
alias: 'important'
(function (Prism) {

// CAREFUL!
// The following patterns are concatenated, so the group referenced by a back reference is non-obvious!

var strings = [
// normal string
// 1 capturing group
/(["'])(?:\\[\s\S]|\$\([^)]+\)|`[^`]+`|(?!\1)[^\\])*\1/.source,

// here doc
// 1 capturing group
/<<-?\s*(\w+?)\s*(?:\r?\n|\r)[\s\S]*?(?:\r?\n|\r)\2/.source,

// here doc quoted
// 2 capturing group
/<<-?\s*(["'])(\w+)\3\s*(?:\r?\n|\r)[\s\S]*?(?:\r?\n|\r)\4/.source
].join('|');

Prism.languages['shell-session'] = {
'command': {
pattern: RegExp(/\$(?:[^\r\n'"<]|<<str>>)+/.source.replace(/<<str>>/g, strings)),
inside: {
'bash': {
pattern: /(\$\s*)[\s\S]+/,
lookbehind: true,
alias: 'language-bash',
inside: Prism.languages.bash
},
'sh': {
pattern: /^\$/,
alias: 'important'
}
}
},
'output': {
pattern: /.(?:.*(?:\r\n?|\n|.$))*/
// output highlighting?
}
},
'output': {
pattern: /.(?:.*(?:\r\n?|\n|.$))*/
// output highlighting?
}
}
};

}(Prism));
2 changes: 1 addition & 1 deletion components/prism-shell-session.min.js

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

34 changes: 34 additions & 0 deletions tests/languages/shell-session/command_string_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ $ echo 'Foo
$ echo "Foo
> Bar"

$ echo <<- STRING_END
foo
bar
STRING_END

$ echo <<- "STRING_END"
foo
bar
STRING_END

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

[
Expand All @@ -24,6 +34,30 @@ $ echo "Foo
"\"Foo\n> Bar\""
]]
]]
]],

["command", [
["sh", "$"],
["bash", [
["builtin", "echo"],
["operator", [
"<<-"
]],
["string", [
"STRING_END\nfoo\nbar\nSTRING_END"
]]
]]
]],

["command", [
["sh", "$"],
["bash", [
["builtin", "echo"],
["operator", [
"<<-"
]],
["string", "\"STRING_END\"\nfoo\nbar\nSTRING_END"]
]]
]]
]

Expand Down
12 changes: 12 additions & 0 deletions tests/pattern-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,16 @@ function testPatterns(Prism) {
});
});

it('- should not use octal escapes', function () {
forEachPattern(({ ast, tokenPath, reportError }) => {
visitRegExpAST(ast.pattern, {
onCharacterEnter(node) {
if (/^\\(?:[1-9]|\d{2,})$/.test(node.raw)) {
reportError(`Token ${tokenPath}: Octal escape ${node.raw}.`);
}
}
});
});
});

}

0 comments on commit 81e1c3d

Please sign in to comment.