本文整理匯總了TypeScript中eslint.RuleTester類的典型用法代碼示例。如果您正苦於以下問題:TypeScript RuleTester類的具體用法?TypeScript RuleTester怎麽用?TypeScript RuleTester使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了RuleTester類的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: RuleTester
import { RuleTester } from "eslint";
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018 } });
import { rule } from "../../src/rules/code-eval";
ruleTester.run("Dynamically executing code is security-sensitive", rule, {
valid: [
{
code: `foo(x)`,
},
{
code: `function foo(x){}\n foo(x);`,
},
{
code: `eval()`,
},
{
code: `eval(42)`,
},
{
code: `eval("Hello")`,
},
{
code: `eval(\`Hello\`)`,
},
{
code: `Function()`,
},
{
code: `new Function(42)`,
},
示例2: RuleTester
import { RuleTester } from "eslint";
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018, sourceType: "module" } });
import { rule } from "../../src/rules/hashing";
ruleTester.run("Hashing data is security-sensitive: client side", rule, {
valid: [
{
// no call
code: `crypto.subtle.digest`,
},
{
// not "digest"
code: `crypto.subtle.encrypt()`,
},
{
// no "crypto.subtle"
code: `foo.digest()`,
},
],
invalid: [
{
code: `crypto.subtle.digest("SHA-256", buffer);`,
errors: [
{
message: "Make sure that hashing data is safe here.",
line: 1,
endLine: 1,
column: 1,
endColumn: 21,
},
示例3: RuleTester
import { RuleTester } from "eslint";
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018, sourceType: "module" } });
import { rule } from "../../src/rules/http-requests";
ruleTester.run("Sending HTTP requests is security-sensitive: client side", rule, {
valid: [
// no call to XMLHttpRequest constructor
{
code: `xmlhttp.open("GET", url, false);`,
},
{
code: `myFetch()`,
},
],
invalid: [
{
code: `window.fetch(url);`,
errors: [
{
message: "Make sure that this HTTP request is sent safely.",
line: 1,
endLine: 1,
column: 1,
endColumn: 13,
},
],
},
{
code: `fetch(url);`,
errors: 1,
示例4: RuleTester
import { RuleTester } from "eslint";
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018, sourceType: "module" } });
import { rule } from "../../src/rules/file-handling";
ruleTester.run("Handling files is security-sensitive", rule, {
valid: [
{
code: `
const fs = require('fs');
fs.createWriteStream('foo.txt');
`,
},
{
code: `
const fs = require('myFs');
fs.createWriteStream(userInput + ".txt");
`,
},
{
code: `
import { rename } from 'fs'
rename("foo.txt", "bar.txt")
`,
},
{
code: `
const fs = require('fs');
fs[getFunctionName()]('foo.txt');
`,
},
示例5: RuleTester
import { RuleTester } from "eslint";
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018, sourceType: "module" } });
import { rule } from "../../src/rules/os-command";
ruleTester.run("Executing OS commands is security-sensitive", rule, {
valid: [
{
code: `
const cp = require('child_process');
cp.fork('child.js');
`,
},
{
code: `
import { fork } from 'child_process';
fork('child.js');`,
},
{
code: `
const cp = require('child_process');
cp.exec('echo child_process.exec hardcoded >> output.txt');`,
},
{
code: `
const cp = require('child_process');
cp.spawn('echo child_process.exec hardcoded >> output.txt', { shell: true });`,
},
{
code: `
const cp = require('child_process');
示例6: RuleTester
import { RuleTester } from "eslint";
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018, sourceType: "module" } });
import { rule } from "../../src/rules/sql-queries";
ruleTester.run("Executing SQL queries is security-sensitive", rule, {
valid: [
{
code: `
const mysql = require('mysql');
conn.query(sql, [userInput], (err, res) => {});
`,
},
{
code: `
const pg = require('pg');
conn.query(sql, [userInput], (err, res) => {});
`,
},
{
code: `
const pg = require('pg');
conn.query("SELECT * FROM FOO", (err, res) => {});
`,
},
{
code: `
import { query } from 'myDB';
query("SELECT * FROM users WHERE id = ' + userId", (err, res) => {});
`,
},
示例7: RuleTester
import { RuleTester } from "eslint";
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018, sourceType: "module" } });
import { rule } from "../../src/rules/regular-expr";
ruleTester.run("Using regular expressions is security-sensitive", rule, {
valid: [
{
code: `str.replace("foo", str); str.replace('foo', str);`,
},
{
code: `let regex = /ab+c/; `,
},
{
code: `str.split();`,
},
{
code: `foo.test();`,
},
{
code: `foo.test(p1, p2);`,
},
{
code: `/abc/.test(p1);`,
},
{
code: `/abc/g.test(p1);`,
},
{
code: `/./.test(p1);`,
},
示例8: RuleTester
import { RuleTester } from "eslint";
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018 } });
import { rule } from "../../src/rules/process-argv";
ruleTester.run("Using command line arguments is security-sensitive", rule, {
valid: [
{
code: `foo.bar`,
},
{
code: `process.argvFoo`,
},
{
code: `processFoo.argv`,
},
{
code: `'process.argv'`,
},
],
invalid: [
{
code: `let x = process.argv;`,
errors: [
{
message: "Make sure that command line arguments are used safely here.",
line: 1,
endLine: 1,
column: 9,
endColumn: 21,
},
示例9: RuleTester
import { RuleTester } from "eslint";
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018 } });
import { rule } from "../../src/rules/cookies";
ruleTester.run("Using cookies is security-sensitive", rule, {
valid: [
{
code: `document.foo`,
},
{
code: `foo.cookie`,
},
{
code: `response.setHeader()`,
},
{
code: `response.setHeader('Content-Type', 'text/plain')`,
},
{
code: `response.foo('Set-Cookie', x)`,
},
{
code: `response.setHeader(SetCookie, x)`,
},
{
code: `res.cookie("foo", "bar");`,
},
{
code: `foo(req.cookies);`,
},
示例10: RuleTester
import { RuleTester } from "eslint";
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018, sourceType: "module" } });
import { rule } from "../../src/rules/http-endpoint";
ruleTester.run("Exposing HTTP endpoints is security-sensitive", rule, {
valid: [
{
code: `import * as express from 'foo'; app.listen(3000);`,
},
{
code: `import * as express from 'foo'; listen(3000);`,
},
{
code: `import * as exp from 'express'; app.use(foo);`,
},
{
code: `import * as express from 'express'; app.listen;`,
},
{
code: `foo('express'); app.listen(3000);`,
},
{
code: `require('express'); app.use(3000);`,
},
],
invalid: [
{
code: `import { foo } from "http"; bar.listen(3000);`,
errors: [
{