-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Atif Afzal
committed
May 12, 2018
1 parent
3c76fce
commit 4cde598
Showing
7 changed files
with
379 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"singleQuote": true, | ||
"trailingComma": "all" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "typescript-plugin-transform-react-jsx-source", | ||
"version": "1.0.0", | ||
"description": | ||
"Adds source file, line number and column number to JSX elements", | ||
"main": "dist/index.js", | ||
"repository": | ||
"[email protected]:atfzl/typescript-plugin-transform-react-jsx-source.git", | ||
"author": "Atif Afzal <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"typescript": "^2.8.3" | ||
}, | ||
"devDependencies": { | ||
"prettier": "^1.12.1", | ||
"tslint": "^5.10.0", | ||
"tslint-config-prettier": "^1.12.0" | ||
}, | ||
"files": ["dist"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import * as ts from 'typescript'; | ||
|
||
const getUpdatedAttributesWithSource = ( | ||
node: ts.JsxSelfClosingElement | ts.JsxOpeningElement | ||
): ts.JsxAttributeLike[] => { | ||
const file = node.getSourceFile(); | ||
|
||
const { line, character } = file.getLineAndCharacterOfPosition( | ||
node.getStart() | ||
); | ||
|
||
const fileName = file.fileName; | ||
/** | ||
* line numbers in editors are 1 indexed, | ||
* so adding 1 to line and column to be in sync with editors | ||
*/ | ||
const lineNumber = line + 1; | ||
const columnNumber = character + 1; | ||
|
||
const newAttributes: ts.JsxAttributeLike[] = []; | ||
|
||
/** | ||
* copy old attributes | ||
*/ | ||
node.attributes.forEachChild((attribute: ts.JsxAttributeLike) => { | ||
newAttributes.push(attribute); | ||
}); | ||
|
||
const sourceAttribute = ts.createJsxAttribute( | ||
ts.createIdentifier('__source'), | ||
ts.createJsxExpression( | ||
undefined, | ||
ts.createObjectLiteral([ | ||
ts.createPropertyAssignment( | ||
ts.createLiteral('fileName'), | ||
ts.createLiteral(fileName) | ||
), | ||
ts.createPropertyAssignment( | ||
ts.createLiteral('lineNumber'), | ||
ts.createLiteral(lineNumber) | ||
), | ||
ts.createPropertyAssignment( | ||
ts.createLiteral('columnNumber'), | ||
ts.createLiteral(columnNumber) | ||
) | ||
]) | ||
) | ||
); | ||
|
||
/** | ||
* add __source to attributes | ||
*/ | ||
newAttributes.push(sourceAttribute); | ||
|
||
return newAttributes; | ||
}; | ||
|
||
const transformer: ts.TransformerFactory<ts.SourceFile> = context => { | ||
const visitor: ts.Visitor = n => { | ||
switch (n.kind) { | ||
case ts.SyntaxKind.JsxOpeningElement: { | ||
const node = n as ts.JsxOpeningElement; | ||
const attributes = getUpdatedAttributesWithSource(node); | ||
|
||
const result = ts.updateJsxOpeningElement( | ||
node, | ||
node.tagName, | ||
ts.createJsxAttributes(attributes) | ||
); | ||
|
||
return result; | ||
} | ||
|
||
case ts.SyntaxKind.JsxSelfClosingElement: { | ||
const node = n as ts.JsxSelfClosingElement; | ||
const attributes = getUpdatedAttributesWithSource(node); | ||
|
||
const result = ts.updateJsxSelfClosingElement( | ||
node, | ||
node.tagName, | ||
ts.createJsxAttributes(attributes) | ||
); | ||
|
||
return result; | ||
} | ||
} | ||
|
||
return ts.visitEachChild(n, visitor, context); | ||
}; | ||
|
||
return n => ts.visitNode(n, visitor); | ||
}; | ||
|
||
export default transformer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"compilerOptions": { | ||
"sourceMap": true, | ||
"target": "es5", | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"declaration": true, | ||
"noImplicitAny": true, | ||
"noImplicitReturns": true, | ||
"noUnusedParameters": true, | ||
"noUnusedLocals": true, | ||
"removeComments": true, | ||
"strictNullChecks": true, | ||
"skipLibCheck": true, | ||
"baseUrl": "src", | ||
"outDir": "dist" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": ["tslint:latest", "tslint-config-prettier"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|
||
ansi-regex@^2.0.0: | ||
version "2.1.1" | ||
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-2.1.1.tgz#c3b33ab5ee360d86e0e628f0468ae7ef27d654df" | ||
|
||
ansi-styles@^2.2.1: | ||
version "2.2.1" | ||
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-2.2.1.tgz#b432dd3358b634cf75e1e4664368240533c1ddbe" | ||
|
||
ansi-styles@^3.2.1: | ||
version "3.2.1" | ||
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d" | ||
dependencies: | ||
color-convert "^1.9.0" | ||
|
||
argparse@^1.0.7: | ||
version "1.0.10" | ||
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911" | ||
dependencies: | ||
sprintf-js "~1.0.2" | ||
|
||
babel-code-frame@^6.22.0: | ||
version "6.26.0" | ||
resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b" | ||
dependencies: | ||
chalk "^1.1.3" | ||
esutils "^2.0.2" | ||
js-tokens "^3.0.2" | ||
|
||
balanced-match@^1.0.0: | ||
version "1.0.0" | ||
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" | ||
|
||
brace-expansion@^1.1.7: | ||
version "1.1.11" | ||
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd" | ||
dependencies: | ||
balanced-match "^1.0.0" | ||
concat-map "0.0.1" | ||
|
||
builtin-modules@^1.1.1: | ||
version "1.1.1" | ||
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f" | ||
|
||
chalk@^1.1.3: | ||
version "1.1.3" | ||
resolved "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98" | ||
dependencies: | ||
ansi-styles "^2.2.1" | ||
escape-string-regexp "^1.0.2" | ||
has-ansi "^2.0.0" | ||
strip-ansi "^3.0.0" | ||
supports-color "^2.0.0" | ||
|
||
chalk@^2.3.0: | ||
version "2.4.1" | ||
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.1.tgz#18c49ab16a037b6eb0152cc83e3471338215b66e" | ||
dependencies: | ||
ansi-styles "^3.2.1" | ||
escape-string-regexp "^1.0.5" | ||
supports-color "^5.3.0" | ||
|
||
color-convert@^1.9.0: | ||
version "1.9.1" | ||
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.1.tgz#c1261107aeb2f294ebffec9ed9ecad529a6097ed" | ||
dependencies: | ||
color-name "^1.1.1" | ||
|
||
color-name@^1.1.1: | ||
version "1.1.3" | ||
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25" | ||
|
||
commander@^2.12.1: | ||
version "2.15.1" | ||
resolved "https://registry.yarnpkg.com/commander/-/commander-2.15.1.tgz#df46e867d0fc2aec66a34662b406a9ccafff5b0f" | ||
|
||
[email protected]: | ||
version "0.0.1" | ||
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" | ||
|
||
diff@^3.2.0: | ||
version "3.5.0" | ||
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12" | ||
|
||
escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5: | ||
version "1.0.5" | ||
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" | ||
|
||
esprima@^4.0.0: | ||
version "4.0.0" | ||
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.0.tgz#4499eddcd1110e0b218bacf2fa7f7f59f55ca804" | ||
|
||
esutils@^2.0.2: | ||
version "2.0.2" | ||
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b" | ||
|
||
fs.realpath@^1.0.0: | ||
version "1.0.0" | ||
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" | ||
|
||
glob@^7.1.1: | ||
version "7.1.2" | ||
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" | ||
dependencies: | ||
fs.realpath "^1.0.0" | ||
inflight "^1.0.4" | ||
inherits "2" | ||
minimatch "^3.0.4" | ||
once "^1.3.0" | ||
path-is-absolute "^1.0.0" | ||
|
||
has-ansi@^2.0.0: | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/has-ansi/-/has-ansi-2.0.0.tgz#34f5049ce1ecdf2b0649af3ef24e45ed35416d91" | ||
dependencies: | ||
ansi-regex "^2.0.0" | ||
|
||
has-flag@^3.0.0: | ||
version "3.0.0" | ||
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd" | ||
|
||
inflight@^1.0.4: | ||
version "1.0.6" | ||
resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9" | ||
dependencies: | ||
once "^1.3.0" | ||
wrappy "1" | ||
|
||
inherits@2: | ||
version "2.0.3" | ||
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" | ||
|
||
js-tokens@^3.0.2: | ||
version "3.0.2" | ||
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b" | ||
|
||
js-yaml@^3.7.0: | ||
version "3.11.0" | ||
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.11.0.tgz#597c1a8bd57152f26d622ce4117851a51f5ebaef" | ||
dependencies: | ||
argparse "^1.0.7" | ||
esprima "^4.0.0" | ||
|
||
minimatch@^3.0.4: | ||
version "3.0.4" | ||
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" | ||
dependencies: | ||
brace-expansion "^1.1.7" | ||
|
||
once@^1.3.0: | ||
version "1.4.0" | ||
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1" | ||
dependencies: | ||
wrappy "1" | ||
|
||
path-is-absolute@^1.0.0: | ||
version "1.0.1" | ||
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f" | ||
|
||
path-parse@^1.0.5: | ||
version "1.0.5" | ||
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.5.tgz#3c1adf871ea9cd6c9431b6ea2bd74a0ff055c4c1" | ||
|
||
prettier@^1.12.1: | ||
version "1.12.1" | ||
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.12.1.tgz#c1ad20e803e7749faf905a409d2367e06bbe7325" | ||
|
||
resolve@^1.3.2: | ||
version "1.7.1" | ||
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.7.1.tgz#aadd656374fd298aee895bc026b8297418677fd3" | ||
dependencies: | ||
path-parse "^1.0.5" | ||
|
||
semver@^5.3.0: | ||
version "5.5.0" | ||
resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.0.tgz#dc4bbc7a6ca9d916dee5d43516f0092b58f7b8ab" | ||
|
||
sprintf-js@~1.0.2: | ||
version "1.0.3" | ||
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" | ||
|
||
strip-ansi@^3.0.0: | ||
version "3.0.1" | ||
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-3.0.1.tgz#6a385fb8853d952d5ff05d0e8aaf94278dc63dcf" | ||
dependencies: | ||
ansi-regex "^2.0.0" | ||
|
||
supports-color@^2.0.0: | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7" | ||
|
||
supports-color@^5.3.0: | ||
version "5.4.0" | ||
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.4.0.tgz#1c6b337402c2137605efe19f10fec390f6faab54" | ||
dependencies: | ||
has-flag "^3.0.0" | ||
|
||
tslib@^1.8.0, tslib@^1.8.1: | ||
version "1.9.0" | ||
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.0.tgz#e37a86fda8cbbaf23a057f473c9f4dc64e5fc2e8" | ||
|
||
tslint-config-prettier@^1.12.0: | ||
version "1.12.0" | ||
resolved "https://registry.yarnpkg.com/tslint-config-prettier/-/tslint-config-prettier-1.12.0.tgz#bc8504f286ecf42b906f3d1126a093114f5729cc" | ||
|
||
tslint@^5.10.0: | ||
version "5.10.0" | ||
resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.10.0.tgz#11e26bccb88afa02dd0d9956cae3d4540b5f54c3" | ||
dependencies: | ||
babel-code-frame "^6.22.0" | ||
builtin-modules "^1.1.1" | ||
chalk "^2.3.0" | ||
commander "^2.12.1" | ||
diff "^3.2.0" | ||
glob "^7.1.1" | ||
js-yaml "^3.7.0" | ||
minimatch "^3.0.4" | ||
resolve "^1.3.2" | ||
semver "^5.3.0" | ||
tslib "^1.8.0" | ||
tsutils "^2.12.1" | ||
|
||
tsutils@^2.12.1: | ||
version "2.26.2" | ||
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-2.26.2.tgz#a9f9f63434a456a5e0c95a45d9a59181cb32d3bf" | ||
dependencies: | ||
tslib "^1.8.1" | ||
|
||
typescript@^2.8.3: | ||
version "2.8.3" | ||
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.8.3.tgz#5d817f9b6f31bb871835f4edf0089f21abe6c170" | ||
|
||
wrappy@1: | ||
version "1.0.2" | ||
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" |