Skip to content

Disallow var statements #310

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
merged 1 commit into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
173 changes: 173 additions & 0 deletions src/__tests__/__snapshots__/disallowed-syntax.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11476,6 +11476,179 @@ typeof x;",
}
`;

exports[`no var statements - verbose: expectParsedError 1`] = `
Object {
"alertResult": Array [],
"code": "\\"enable verbose\\";
var x = 1;",
"displayResult": Array [],
"errors": Array [
NoVarError {
"node": Node {
"declarations": Array [
Node {
"end": 28,
"id": Node {
"end": 24,
"loc": SourceLocation {
"end": Position {
"column": 6,
"line": 2,
},
"start": Position {
"column": 5,
"line": 2,
},
},
"name": "x",
"start": 23,
"type": "Identifier",
},
"init": Node {
"end": 28,
"loc": SourceLocation {
"end": Position {
"column": 10,
"line": 2,
},
"start": Position {
"column": 9,
"line": 2,
},
},
"raw": "1",
"start": 27,
"type": "Literal",
"value": 1,
},
"loc": SourceLocation {
"end": Position {
"column": 10,
"line": 2,
},
"start": Position {
"column": 5,
"line": 2,
},
},
"start": 23,
"type": "VariableDeclarator",
},
],
"end": 29,
"kind": "var",
"loc": SourceLocation {
"end": Position {
"column": 11,
"line": 2,
},
"start": Position {
"column": 1,
"line": 2,
},
},
"start": 19,
"type": "VariableDeclaration",
},
"severity": "Error",
"type": "Syntax",
},
],
"parsedErrors": "Line 2, Column 1: Variable declaration using \\"var\\" is not allowed.
Use keyword \\"let\\" instead, to declare a variable:

let x = 1;
",
"result": undefined,
"resultStatus": "error",
"visualiseListResult": Array [],
}
`;

exports[`no var statements: expectParsedError 1`] = `
Object {
"alertResult": Array [],
"code": "var x = 1;",
"displayResult": Array [],
"errors": Array [
NoVarError {
"node": Node {
"declarations": Array [
Node {
"end": 9,
"id": Node {
"end": 5,
"loc": SourceLocation {
"end": Position {
"column": 5,
"line": 1,
},
"start": Position {
"column": 4,
"line": 1,
},
},
"name": "x",
"start": 4,
"type": "Identifier",
},
"init": Node {
"end": 9,
"loc": SourceLocation {
"end": Position {
"column": 9,
"line": 1,
},
"start": Position {
"column": 8,
"line": 1,
},
},
"raw": "1",
"start": 8,
"type": "Literal",
"value": 1,
},
"loc": SourceLocation {
"end": Position {
"column": 9,
"line": 1,
},
"start": Position {
"column": 4,
"line": 1,
},
},
"start": 4,
"type": "VariableDeclarator",
},
],
"end": 10,
"kind": "var",
"loc": SourceLocation {
"end": Position {
"column": 10,
"line": 1,
},
"start": Position {
"column": 0,
"line": 1,
},
},
"start": 0,
"type": "VariableDeclaration",
},
"severity": "Error",
"type": "Syntax",
},
],
"parsedErrors": "Line 1: Variable declaration using \\"var\\" is not allowed.",
"result": undefined,
"resultStatus": "error",
"visualiseListResult": Array [],
}
`;

exports[`while needs braces - verbose: expectParsedError 1`] = `
Object {
"alertResult": Array [],
Expand Down
25 changes: 25 additions & 0 deletions src/__tests__/disallowed-syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,31 @@ For instance, to assign 20 to x, you can write:
`)
})

test('no var statements', () => {
return expectParsedError(
stripIndent`
var x = 1;
`,
{ chapter: 100 }
).toMatchInlineSnapshot(`"Line 1: Variable declaration using \\"var\\" is not allowed."`)
})

test('no var statements - verbose', () => {
return expectParsedError(
stripIndent`
"enable verbose";
var x = 1;
`,
{ chapter: 100 }
).toMatchInlineSnapshot(`
"Line 2, Column 1: Variable declaration using \\"var\\" is not allowed.
Use keyword \\"let\\" instead, to declare a variable:

let x = 1;
"
`)
})

test('Cannot use update statements', () => {
return expectParsedError(
stripIndent`
Expand Down
2 changes: 2 additions & 0 deletions src/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import noNull from './noNull'
import noUnspecifiedLiteral from './noUnspecifiedLiteral'
import noUnspecifiedOperator from './noUnspecifiedOperator'
import noUpdateAssignment from './noUpdateAssignment'
import noVar from './noVar'
import singleVariableDeclaration from './singleVariableDeclaration'

const rules: Array<Rule<es.Node>> = [
Expand All @@ -34,6 +35,7 @@ const rules: Array<Rule<es.Node>> = [
noUnspecifiedLiteral,
noUnspecifiedOperator,
noUpdateAssignment,
noVar,
singleVariableDeclaration,
noEval
]
Expand Down
42 changes: 42 additions & 0 deletions src/rules/noVar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { generate } from 'astring'
import * as es from 'estree'

import { ErrorSeverity, ErrorType, Rule, SourceError } from '../types'

export class NoVarError implements SourceError {
public type = ErrorType.SYNTAX
public severity = ErrorSeverity.ERROR

constructor(public node: es.VariableDeclaration) {}

get location() {
return this.node.loc!
}

public explain() {
return 'Variable declaration using "var" is not allowed.'
}

public elaborate() {
const name = (this.node.declarations[0].id as es.Identifier).name
const value = generate(this.node.declarations[0].init)

return `Use keyword "let" instead, to declare a variable:\n\n\tlet ${name} = ${value};`
}
}

const noVar: Rule<es.VariableDeclaration> = {
name: 'no-var',

checkers: {
VariableDeclaration(node: es.VariableDeclaration, ancestors: [es.Node]) {
if (node.kind === 'var') {
Copy link
Member

Choose a reason for hiding this comment

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

I think it might be better to have a rule to allow const and let only rather than to disallow var, and we can merge with noDeclareMutable

Copy link
Member

Choose a reason for hiding this comment

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

Nevermind, I take this back. Not currently sane to have a whitelist using rules.

return [new NoVarError(node)]
} else {
return []
}
}
}
}

export default noVar