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

add new rule: jsx-whitespace-literal #243

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/rules/jsxWhitespaceLiteralRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @license
* Copyright 2017 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as Lint from "tslint";
import { isJsxText } from "tsutils/typeguard/3.0";
import * as ts from "typescript";

const RESERVED_ENTITY = " ";

export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
description: Lint.Utils.dedent
`Warn if ' ' is used in JXS markup. Prefered {" "} over ' '`,
tanmoyopenroot marked this conversation as resolved.
Show resolved Hide resolved
optionExamples: ["true"],
options: null,
optionsDescription: "",
ruleName: "jsx-whitespace-literal",
type: "functionality",
typescriptOnly: false,
};

public static FAILURE_STRING = `Prefered '{" "}' over ' ' in JSX markup.`;
tanmoyopenroot marked this conversation as resolved.
Show resolved Hide resolved

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
}
}

function walk(ctx: Lint.WalkContext<void>): void {
return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
if (isJsxText(node)) {
if (node.getText().indexOf(RESERVED_ENTITY) > -1) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
}
}

return ts.forEachChild(node, cb);
});
}
29 changes: 29 additions & 0 deletions test/rules/jsx-whitespace-literal/test.tsx.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<div>
<p>Some Text</p>
<hr></hr>
Some Text&nbsp;&nbsp;
tanmoyopenroot marked this conversation as resolved.
Show resolved Hide resolved
~~~~~~~~~~~~~~~~~~~~
<select>
~~~~ [0]
<option></option>
</select>
<button type="button">Some Text</button>
<br/>
Some Text&nbsp;
~~~~~~~~~~~~~~~
tanmoyopenroot marked this conversation as resolved.
Show resolved Hide resolved
<select>
~~~~ [0]
<option></option>
</select>
<button type="button">Some Text</button>
<br/>
</div>

<p>
<img src="images/img1.gif" width="50" height="50"/>&nbsp;
tanmoyopenroot marked this conversation as resolved.
Show resolved Hide resolved
~~~~~~
<img src="images/img2.gif" width="50" height="50"/>
~~~~ [0]
</p>

[0]: Prefered '{" "}' over '&nbsp;' in JSX markup.
5 changes: 5 additions & 0 deletions test/rules/jsx-whitespace-literal/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"jsx-whitespace-literal": true
}
}